Given a generated histogram that I got from an image I was wondering if there was any optimized way to generate a mask. Below I have added in 3 different images: the reference to use, the histogram data of the reference, and the main image that I would like to mask. I know that I could do this by each pixel and vary the color information by a certain percentage so that I would be able to get colors with lighting changes as well.
The basic idea is to find a color, given by the histogram data and within a certain range, and if it finds anything then to make it black. If it doesn't find anything then the color will be white.
Any advice would be greatly appreciated.
Reference image:
His开发者_运维技巧togram values:
Image to mask:
What you want is to mask a color in a certain range, this is what you should try with the code i posted here : In my example, it is used to make it transparent, if you want to make it black, just skip the cvNot() step...
Making a color completely transparent in OpenCV
Hope it helped, Julien
PS : i've just seen that you were the one who asked the question I answered about how to make a color transparent: the problem here is exactly the same... just adapt a lil bit the answer..
- 1) Convert your image RGB -> HSV : cvtColor()
- 2) Generate your histogram : calcHist()
- 3) Find the maximum in your Hue histogram : minMaxLoc()
- 4) Select thresholds around this maximum : your function
- 5) Use them to select only the color you want : inRange()
- 6) Put this mask in black : your function (a very simple way would be to remove all the RGB components on the mask) : your function
Try to use template matching approaches instead of histogram, for example, normalized cross correlation http://www.mathworks.com/products/demos/image/cross_correlation/imreg.html.
精彩评论