Histogram Python Opencv

Asked

Viewed 1,579 times

0

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

That’s the job of.

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 it to print out the histogram

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

But I didn’t print, as I do a histogram of just the above function ?

1 answer

0

CalcHist function

You are using the function calcHist that has these input parameters:

cv2.calcHist(images, channels, mask, histSize, ranges[, hist[, accumulate]])

  1. images : is the source image of type uint8 or float32. Must be passed between brackets ,ex.: [img].
  2. 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.
  3. 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.
  4. histSize : represents the BIN count. Needs to be passed between brackets. For full scale, [256] is past.
  5. 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:

Logo SOpt

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.