开发者

Horizontal Histogram in OpenCV

I am newbie to OpenCV,now I am making a senior project related Image processin开发者_如何学Pythong. I have a question: Can I make a horizontal or vertical histogram with some functions of OpenCV? Thanks,

Truong


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.


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


Updating carnieri answer (some cv functions are not working today)

import numpy as np
import cv2

def verticalProjection(img):
    "Return a list containing the sum of the pixels in each column"
    (h, w) = img.shape[:2]
    sumCols = []
    for j in range(w):
        col = img[0:h, j:j+1] # y1:y2, x1:x2
        sumCols.append(np.sum(col))
    return sumCols

Regards.


An example of using cv2.reduce with OpenCV 3 in Python :

import numpy as np
import cv2

img = cv2.imread("test_1.png") 

x_sum = cv2.reduce(img, 0, cv2.REDUCE_SUM, dtype=cv2.CV_32S) 
y_sum = cv2.reduce(img, 1, cv2.REDUCE_SUM, dtype=cv2.CV_32S) 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜