Ask Your Question

Revision history [back]

Can't get histogram generation to work.

Greetings everyone !.

So, I've been trying to get this simple function to work for almost a day now. I'm just trying to show a RGB histogram of an image (an area thereof) and show the bins as rectangles on the screen.

I've failed, i've tried a million tutorials, but somehow the answer escapes me. All I am getting from cv.calcHist() at the moment is a bunch of 0 and a couple of "meaningful" values in the first and last positions.

I hope you can give me a hand with this one.

current_main_img = cv.imread("TestPattern.jpeg")

def show_histogram(img):

scale = 32
bins_number = 256/scale
bin_length = 256/bins_number
bins = np.arange(bins_number).reshape(bins_number,1)
print(bins)
finalHist = np.zeros((300,240,3))

iInfo = [ (255,0,0),(0,255,0),(0,0,255) ]

for iChannel, iColor in enumerate(iInfo):

    histElement = cv.calcHist([img],[iChannel],None,[bins_number],[0,255])

    cv.normalize(histElement,histElement,0,255,cv.NORM_MINMAX)

    tmpHist=np.int32(np.around(histElement))
    points = np.int32(np.column_stack((bins,tmpHist)))

    print(points)

    for posx, intensity in points:
        subbin_length=bin_length/3
        displacement = posx*bin_length+iChannel*subbin_length
        cv.rectangle(finalHist,(displacement+posx*subbin_length, 0),
                               (displacement+(posx+1)*subbin_length, 
                                intensity), iColor, 2)

flippedHist = np.flipud(finalHist)

cv.namedWindow("Histogram", cv.CV_WINDOW_AUTOSIZE )
cv.imshow("Histogram", flippedHist )

And the following is the result:

Result:

Result

Thanks in advance !.

Can't get simple histogram generation to work.

Greetings everyone !.

So, I've been trying to get this simple function to work for almost a day now. I'm just trying to show a RGB histogram of an image (an area thereof) and show the bins as rectangles on the screen.

I've failed, i've tried a million tutorials, but somehow the answer escapes me. All I am getting from cv.calcHist() at the moment is a bunch of 0 and a couple of "meaningful" values in the first and last positions.

I hope you can give me a hand with this one.

current_main_img = cv.imread("TestPattern.jpeg")

def show_histogram(img):

scale = 32
bins_number = 256/scale
bin_length = 256/bins_number
bins = np.arange(bins_number).reshape(bins_number,1)
print(bins)
finalHist = np.zeros((300,240,3))

iInfo = [ (255,0,0),(0,255,0),(0,0,255) ]

for iChannel, iColor in enumerate(iInfo):

    histElement = cv.calcHist([img],[iChannel],None,[bins_number],[0,255])

    cv.normalize(histElement,histElement,0,255,cv.NORM_MINMAX)

    tmpHist=np.int32(np.around(histElement))
    points = np.int32(np.column_stack((bins,tmpHist)))

    print(points)

    for posx, intensity in points:
        subbin_length=bin_length/3
        displacement = posx*bin_length+iChannel*subbin_length
        cv.rectangle(finalHist,(displacement+posx*subbin_length, 0),
                               (displacement+(posx+1)*subbin_length, 
                                intensity), iColor, 2)

flippedHist = np.flipud(finalHist)

cv.namedWindow("Histogram", cv.CV_WINDOW_AUTOSIZE )
cv.imshow("Histogram", flippedHist )

And the following is the result:

Result:

Result

Thanks in advance !.