This happens to a lot of people. Your brain has sort of become addicted to these sort of thoughts which is why you may find that they're happening more often than not.
Take me for instance: I used to always picture people naked... didn't matter who it was, I always saw a naked person in front of me. This stopped when I was around 19 and had really good sex for the first time.
I also thought many times of killing myself (how I will do it, where, etc) and then I got to thinking about the aftermath of my suicide, like WHO is going to clean up my splattered bits of brain or clean up my poo and pee (well, they say you lose control of your bowels when you kill yourself). Think about being buried forever and think that life will eventually go on without you and you will miss out on a ton of cool stuff.
So if you think about killing yourself, let your mind go, start thinking about the aftermath and all the rest. It will turn your brain right off whole idea soon enough.
The thing is, you're brain is hooked, so get it unhooked and allow it to create new synapses and forget those old ones. Bad thoughts are like bad habits, you just have to break them.