I've had the same problem for well over a decade and have finally managed to improve the situation by writing something down that actually sounded the way I heard it in my head (see neighboring thread, "my first composition"). I don't totally know why it worked this time, but I'll tell you as much as I can about how I did it, and maybe that'll help.
In the past, and also recently, when I got an idea in my head I would sit down and play it out. If I had my wits sufficiently about me, I'd record it too -- at least the guts of it, so I wouldn't forget it, but usually a whole improvisation. Sometimes I'd go back later and write down precise minute-second marks where something particularly interesting happened, the idea being that when I someday actually composed something, I'd have a catalog of ideas at the ready. Of course, I didn't use it this time. This time, a couple ideas occurred to me -- as always, I have no idea where they came from -- and while I didn't immediately love them, experimenting showed that a lot could be done with them, and that was enough for me.
Once I decided to start on the piece, I carried around staff paper with me at all times. Most of the time I was thinking about the thematic material in an instrumental vacuum, doing pure reasoning about counterpoint and such without regard for who or how. Here and there inspiration would strike and I'd jot down a phrase or a few measures, as with recording improvisations, simply to make sure I didn't lose the idea forever. Eventually I had a bunch of scraps and a looming deadline, so I allowed the piano back into the equation -- but only partly. I would play what I had on paper so far, imagine what I would want to hear next, and improvise a few measures in that direction, usually uselessly, but after several tries something good enough would come out. Then I would play it a few more times to get it loaded into memory and get the heck away from that seductive instrument (otherwise I'd never stop improvising
) and sit at my desk to write it down. Then back to the piano to gradually feel my way forward, back to the desk to transcribe it, lather, rinse, repeat. (Actually, there wasn't a lot of showering near the deadline. :-p)
Improvising is relatively unconstrained. While composing, I kept in mind at all times my two themes, and tried to have at least one of them be expressed in every little improvisation. As a result, even though the piece is essentially a premeditated improvisation with stunted sonata-allegro aspirations, it gains some degree of coherence due to the eternally present thematic material. No matter what's going on, it's always related to something you've heard before.
I'm finding that there's a difference in the way I think about improvising depending on whether it's to blow off some steam or to add up to something more permanent. With the former, I don't want to pay too much attention to what I'm doing or why, and I don't want to stop for any reason; with the latter, there's no such thing as too much self-awareness, and I must constantly work to keep my freewheeling pianistic impulses under partial control. The former is more immediately satisfying, the latter more permanently so.
How well do you hear yourself during improvising? after? How are you on functional harmony? Like Pete said, that's a good mental shorthand, if you can hear yourself that way.