How to get histogram in CV?
We can get histogram simply by using:
cv2.calcHist(images, channels, mask, hitSize, ranges) where,
- images : The source image of type uint8 or float32. it should be given in square brackets, ie, “[img]”.
- channels : Also given in square brackets. It is the index of channel for which we calculate histogram. For example, if input is grayscale image, its value is [0]. For color image, you can pass [0], [1] or [2] to calculate histogram of blue, green or red channel respectively.
- mask : To find histogram of full image, it is given as “None”. But if you want to find histogram of particular region of image, you have to create a mask image for that and give it as mask.
- hitSize : Array of histogram sizes in each dimension.
- ranges : Array of the dims arrays of the histogram bin boundaries in each dimension. Normally, it is [0,256].
Result
Obtain the dominant colors in image by K-Means Clustering
We can fit our image by KMeans to get classified values. By those values, in my implementation, I made a bar shaped histogram that can get the dominant colors in the image.