(See EDIT at the end in case I misunderstood the question):
If you want to draw histograms, I sent one python sample to OpenCV and you can get it from here:
http://code.opencv.org/projects/opencv/repository/entry/trunk/opencv/samples/python2/hist.py
Used to draw two kinds of histograms. The first, applicable to both color and grayscale images, as shown here: http://opencvpython.blogspot.in/2012/04/drawing-histogram-in-opencv-python.html
The second option is exclusive to the grayscale image that matches your image in question.
I will show the second and its modification.
Consider the full image as shown below:
We need to draw a histogram, as you showed. Check out the code below:
import cv2 import numpy as np img = cv2.imread('messi5.jpg') mask = cv2.imread('mask.png',0) ret,mask = cv2.threshold(mask,127,255,0) def hist_lines(im,mask): h = np.zeros((300,256,3)) if len(im.shape)!=2: print "hist_lines applicable only for grayscale images"
And below is a histogram. Remember, this is a full image histogram. For this, we provided None
for the mask.
Now I want to find a histogram of some part of the image. The OpenCV histogram function has a masking tool for this. For a normal histogram, you must set it to None
. Otherwise, you must specify a mask.
A mask is an 8-bit image where white means this area should be used for histogram calculations, and black means it should not.
So, I used a mask as shown below (created using paint, you need to create your own mask for your purposes).
I changed the last line of code as shown below:
histogram = hist_lines(img,mask)
Now see the difference below:
(Remember that the values ββare normalized, so the values ββshown are not the actual number of pixels, normalized to 255. Change it as you like.
EDIT:
I think I misunderstood your question. You need to compare the histograms, right?
If this is what you wanted, you can use the cv2.compareHist
function.
There is an official C ++ tutorial. You can find the corresponding Python code here.