Histogram Python Opencv

0

Hello, I would like to define a histogram of a function that I created to detect an image region.

The function is this.

def points (imagem, pontosFaciais):
p68 =[[47, 35, False],
      [35, 12, False],
      [12, 13, False],
      [13, 14, False],
      [14, 47, False],
      [41, 31, False], #buchecha esquerda
      [31, 4, False],
      [4, 3, False],
      [3, 2, False],
      [2, 41, False]]
for k in range(0, len(p68)):
    pontos = []
    ponto = [pontosFaciais.part(p68[k][0]).x, pontosFaciais.part(p68[k][0]).y]

    pontos.append(ponto)

    ponto = [pontosFaciais.part(p68[k][1]).x, pontosFaciais.part(p68[k][1]).y]

    pontos.append(ponto)

    pontos = np.array(pontos, dtype=np.int32)

    cv2.polylines(imagem, [pontos], p68[k][2], (255, 0, 0), 2)

And I did this to print the histogram

red = cv2.calcHist([imagem], [2], points(), [256], [0, 256])

But I did not print, how do I make a histogram just from the above function?

    
asked by anonymous 11.09.2018 / 07:34

1 answer

0

calcHist function

You are using the calcHist function 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 enclosed in brackets, eg [img] .
  • channels : Also passed in brackets. It is the index of the channel on 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 blue, green, or red histogram (BGR) respectively.
  • mask : image mask. To find the histogram of the entire image, it is passed as None . But if you want to find the histogram of a particular region of the image, the image mask must be created and passed as a mask parameter.
  • histSize : represents the BIN count. It needs to be passed between brackets. For full scale, [256] is passed.
  • ranges : This is the histogram interval. It is usually passed [0,256] , that is, full scale.
  • Therefore, red in your code is a vector 256x1, which has the following parameters:

    red = cv2.calcHist([imagem], [2], points(), [256], [0, 256])

    Then you should be using a color image. To load a color image with imread the flag cv2.IMREAD_UNCHANGED or -1 should be used:

    imagem = cv2.imread('caminho\para\imagem_exemplo.jpg',-1)

    or leave blank for the "discover" image type:

    imagem = cv2.imread('caminho\para\imagem_exemplo.jpg')

    With [2] the red color channel (BGR) will be extracted.

    With [256] and [0,256] the complete histSize and range scales are passed.

    And the mask is passed with the function points() , but without any input parameter in the function it is not doing anything.

    In the mask parameter an 8-bit (CV_8U) array of the same image size must be passed.

    Example

    Using this image:

    Code

    Commentsinthecode

    importcv2importnumpyasnpfrommatplotlibimportpyplotasplt#Carregaaimagemcomseusdadosdealtura,larguraecanais#Criaatuplacolors(BGR)ecriaalistafeaturesimg=cv2.imread('C:\Users\usuario\Desktop\SOpt.png',-1)height,width,channels=img.shapecolors=('b','g','r')features=[]#Criaamáscaramask=np.zeros(img.shape[:2],np.uint8)mask[int(height*0.1):int(height*0.9),0:int(width*0.6)]=255masked_img=cv2.bitwise_and(img,img,mask=mask)#Mostraaimagemoriginal,amáscaraeaimagemcomadiçãodamáscaraplt.subplot(221),plt.imshow(img)plt.subplot(222),plt.imshow(mask,'gray')plt.subplot(223),plt.imshow(masked_img)plt.show()#ExemploPYImageSearch#CarregaoHistogramadaimageminteirachans=cv2.split(img)for(chan,color)inzip(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()#ExemploDocOpenCV#Carregaaimagemcomaadiçãodamáscarafori,colinenumerate(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 Using Color Histograms for Computer Vision and Image Search Engines

        
    11.09.2018 / 15:12