Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Horizontal Histogram in OpenCV

Tags:

opencv

I am newbie to OpenCV,now I am making a senior project related Image processing. I have a question: Can I make a horizontal or vertical histogram with some functions of OpenCV? Thanks,

Truong

like image 222
user522659 Avatar asked Jan 22 '23 00:01

user522659


2 Answers

The most efficient way to do this is by using the cvReduce function. There's a parameter to allow to select if you want an horizontal or vertical projection.

You can also do it by hand with the functions cvGetCol and cvGetRow combined with cvSum.

like image 148
rold2007 Avatar answered Jan 31 '23 00:01

rold2007


Based on the link you provided in a comment, this is what I believe you're trying to do.

You want to create an array with n elements, where n is the number of columns in the input image. The value of the nth element of the array is the sum of all the pixels in the nth column.

You can calculate this array by looping over the columns of the input image, using cvGetSubRect to access the pixels in that column, and cvSum to sum those pixels.

Here is some Python code that does that, assuming a grayscale image:

import cv

def verticalProjection(img):
    "Return a list containing the sum of the pixels in each column"
    (w,h) = cv.GetSize(img)
    sumCols = []
    for j in range(w):
        col = cv.GetSubRect(img, (j,0,1,h))
        sumCols.append(cv.Sum(col)[0])
    return sumCols
like image 26
carnieri Avatar answered Jan 30 '23 23:01

carnieri