I only briefly skimmed the comments… Just going to respond based on my experience.
Bowdu (my Shiba) did this for a while, mostly trying to pee on other dogs, but occasionally trying to pee on people too. He started when he was about two years old (well after he was neutered). I managed to fix the habit fairly quickly, I think.
The first few times he did it to dogs, it seemed exactly because the other dog was acting "tree-like." After the initial greeting, if the other dog would stiffen or look away, not moving, then Bowdu would take the opportunity to move up to the dog's side and pee on them. It would only take a second. Very embarrassing.
I learned to watch for his body signals -- you CAN catch them, but you have to watch carefully and quickly. For Bowdu, it was the way he approached, strutting hard and fast, then sniffing at a certain angle, and the way he moved away from the dog he was sniffing. The sniff ALWAYS comes first. And if he looked like he was about to move into position to aim where he just sniffed, I would make a sharp sound to distract him or swoop in there and shoo him away. Not sure how conscious he was of his actions, but he definitely seemed to "snap out" of some kind of reverie when I went in there with an AH-AH or clapped my hands.
I only needed to correct him a few times over the course of several consecutive days before he immediately stopped doing it as much. And then he tried it maybe once a week. To once every few months. To less than that, now. He still has it in him, but I no longer feel like I have to be hyper-vigilant about it. I've usually got an eye on him anyway.
When he marks other dogs, I do think he's being a jerk with some awareness of what he's doing. I'm not calling it dominance because I'm not sure that's what it is, and that seems like too easy of a way to label the behavior.
Whatever's going through his mind, I don't think it's the same thing as when he's marking humans. When he's marked humans in the past, he seems to be more fixated on a smell, not the person who's underneath the pants. He's never tried to pee on a human wearing shorts with their legs exposed, for example. So, knowing his quirks, I just watch him carefully and try to be quick enough to catch him in the act, or with that intensely focused look on his face, and snap him out of it. He DOES knows better, he just needs to be reminded of that sometimes.
Long anecdotal comment, but hopefully it helps.