Peak value of a histogram

Asked

Viewed 311 times

0

My code reads an image and transforms the RGB color model to HSV, and then I make a frequency histogram of each channel (H, S and V), with H varying from 0-179 and the rest from 0-255. With histogram plotting it is possible to check peaks in channels H, S and V. This histogram shows through the vertical axis the amount of pixels and the horizontal axis the channels H ,S and V. I want to return the values of H, S and V in which the amount of pixels is higher, that is, the peak histogram of each channel.inserir a descrição da imagem aqui How to do this? I have already used the np.amax() method but it returns the maximum values of each channel (H = 179, S = 255 and V =255).

def show_hsv_hist(image):
    # Hue
    plt.figure(figsize=(20, 3))
    histr0 = cv2.calcHist([hsv], [0], None, [180], [0, 180])
    plt.plot(histr0)
    plt.xlim([0, 180])
    plt.title('Hue')

    # Saturation
    plt.figure(figsize=(20, 3))
    histr1 = cv2.calcHist([hsv], [1], None, [255], [0, 255])
    plt.xlim([0, 255])
    plt.plot(histr1)
    plt.title('Saturation')

    # Value
    plt.figure(figsize=(20, 3))
    histr2 = cv2.calcHist([hsv], [2], None, [255], [0, 255])
    plt.xlim([0,255])
    plt.plot(histr2)
    plt.title('Value')

    max_v = np.amax(v)
    print (max_v)

Fashion can find the most frequent value for each channel. Do you have any method for this? I know it exists in the medium, medium numpy.

  • Fashion can find the most frequent value for each channel. Do you have any method for this? I know it exists in the medium, medium numpy.

1 answer

0


The value of maximum Y and maximum X can be found with the function max() and numpy.Where

ymax = max(histr)
xmax, _ = np.where(histr == ymax)

print(xmax, ymax)

Code

import cv2
import numpy as np
import urllib.request
import matplotlib.pyplot as plt

def show_hsv_hist(hsv):
    HSV = [['Hue', 0, 180], ['Saturation', 1, 255], ['Value', 2, 255]]
    for nome, canal, valor in HSV:
        # Gráfico
        plt.figure(figsize=(20, 3))
        hist = cv2.calcHist([hsv], [canal], None, [valor], [0, valor])
        plt.plot(hist)
        plt.xlim([0, valor])
        plt.title(nome)
        #Máximo
        ymax = max(hist)
        xmax, _ = np.where(hist == ymax)
        print(ymax, xmax)

resp = urllib.request.urlopen("https://upload.wikimedia.org/wikipedia/commons/3/32/Carrots_of_many_colors.jpg")
img = np.asarray(bytearray(resp.read()), dtype="uint8")
img = cv2.imdecode(img, cv2.IMREAD_COLOR)

cv2.imshow('Original', img)
cv2.waitKey(0)

hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

show_hsv_hist(hsv)
plt.show()

Browser other questions tagged

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