Thoughts by themselves do not count as hayir (good deeds) or sher (bad deeds). To illustrate for example: its not enough to think about the suras when you are performing the salah. If the imam is not leading the salah you have to move your lips in recitation and be able to hear yourself in recitation.
So idle thoughts don' t count either way. That being said special consideration and caution must be exercised in taming your nefs. An explanation of the nefs and its role is beyond the scope of this answer space, but briefly:
Nefs directly translated means breath but it generally symbolizes all biological needs and desires of the person. Without our nefs we could not survive. Without hunger and thirst we would not strive to make a living to put food and drink on the table Without the desire to have sex with the opposite gender, reproduction would not happen and none of us would exist.
For this, Allah maintains a very powerful and potentially explosive attraction between these opposites. Just like gasoline and oxygen can either cause a destructive explosion or a very useful controlled slow burn to power our vehicles; likewise an unregulated interaction between the genders can lead to explosion (adultery, prostitution, fatherless children, immorality, promiscuity, family corruption, etc) while the regulated and harnessed 'controlled slow burn' will yield the blessed results of peaceful and happy marriage structure and children.
Such volatile mixtures are difficult to control if uncovered. If we think of men as oxygen and women as gasoline, leaving the latter uncovered can lead to explosion; Allah Muhafaza. For this reason, Allah commanded women to cover themselves in public and in front of non-mehram men (Quran 24:31). And men (and women) are commanded to 'lower their gaze'(Quran 24:30).
This 'lower your gaze' applies for today also for media and Internet. It is hard to control yourself when the display of forbidden fruit is just clicks away.
It's when you let your nefs take over your heart and let love of the ephemoral dunya take preference over the love of Allah that may result in sinful action and spiritual corruption.
The nefs has to be kept restrained on a leash of self restraint through faith and taqwa (Quran 91:7-10). This concept of taqwa and self-restraint is alien to the west where it is encouraged to follow your lusts and desires, instant gratification, and let your nefs run wild.
So how do we tame our nefs and keep it on a leash when our senses are bombarded by inappropriate and un-Islamic promiscuity?
The other answers already covered fasting as one technique.
Another effective technique could be neuro-linguistic programming. Please see works on this topic by Anthony Robbins. The way it would work is associating a negative sensation with the sexually provocative image or fantasy. For example associating the smell of very bad breath every time you see the image. After some time and practice you will be disgusted with the image instead of having pleasure fantasies.
Each time the sexual fantasy pops into your mind, make dua to Allah to present this person in perfect form and personality in Jennah and for sabr to wait until then. As promised in the Quran the real jewels are in Jennah, in this dunya what looks like jewels are just broken glass and you will likely cut your yourself if you touch or wear them.