CalcHist function
You are using the function calcHist that has these input parameters:
cv2.calcHist(images, channels, mask, histSize, ranges[, hist[, accumulate]])
images
: is the source image of type uint8 or float32. Must be passed between brackets ,ex.: [img]
.
channels
: It is also passed between square brackets. It is the index of the channel in which the histogram is calculated. For example, if an input is grayscale, this value is [0]. For a color image, [0]
, [1]
or [2]
can be passed to calculate the histogram of blue, green or red (BGR) respectively.
mask
: image mask. To find the entire image histogram, this is passed as None
. But if you want to find the histogram of a particular region of the image, the image mask should be created and passed as a parameter mask
.
histSize
: represents the BIN count. Needs to be passed between brackets. For full scale, [256]
is past.
ranges
: This is the histogram interval. It is usually passed [0,256]
, that is, the full scale.
Therefore, red
in its code, it is a 256x1 vector, which has the following parameters:
red = cv2.calcHist([imagem], [2], points(), [256], [0, 256])
Then you must be using a color image. To upload a color image with imread
the flag cv2.IMREAD_UNCHANGED
or -1
must be used:
imagem = cv2.imread('caminho\\para\\imagem_exemplo.jpg',-1)
or leave blank for the "discover" function the image type:
imagem = cv2.imread('caminho\\para\\imagem_exemplo.jpg')
With [2]
the red channel (BGR) shall be extracted.
With [256]
and [0,256]
full range and range scales are passed.
And the mask is passed with the function points()
, but with no input parameter in the function it is doing nothing.
In the parameter mask
an 8-bit array (CV_8U) of the same image size should be passed.
Example
Using this image:
Code
Comments on the code
import cv2
import numpy as np
from matplotlib import pyplot as plt
# Carrega a imagem com seus dados de altura, largura e canais
# Cria a tupla colors (BGR) e cria a lista features
img = cv2.imread('C:\\Users\\usuario\\Desktop\\SOpt.png',-1)
height, width, channels = img.shape
colors = ('b', 'g', 'r')
features=[]
#Cria a máscara
mask = np.zeros(img.shape[:2], np.uint8)
mask[int(height*0.1):int(height*0.9), 0:int(width*0.6)] = 255
masked_img = cv2.bitwise_and(img,img,mask = mask)
#Mostra a imagem original, a máscara e a imagem com adição da máscara
plt.subplot(221), plt.imshow(img)
plt.subplot(222), plt.imshow(mask, 'gray')
plt.subplot(223), plt.imshow(masked_img)
plt.show()
#Exemplo PYImageSearch
#Carrega o Histograma da imagem inteira
chans = cv2.split(img)
for (chan, color) in zip(chans, colors):
hist_full = cv2.calcHist([chan], [0], None, [256], [0, 256])
features.extend(hist_full)
plt.plot(hist_full, color=color)
plt.xlim([0, 256])
plt.show()
#Exemplo Doc OpenCV
#Carrega a imagem com a adição da máscara
for i,col in enumerate(colors):
hist_mask = cv2.calcHist([img], [i], mask, [256], [0, 256])
plt.plot(hist_mask, color=col)
plt.xlim([0, 256])
plt.show()
References:
Histograms - 1 : Find, Plot, Analyze !!!
Clever Girl: A Guide to Utilizing Color Histograms for Computer Vision and Image Search Engines