Horizontal Histogram in OpenCV

前端 未结 4 1240
太阳男子
太阳男子 2020-12-19 14:15

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 Open

相关标签:
4条回答
  • 2020-12-19 14:48

    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.

    0 讨论(0)
  • 2020-12-19 14:49

    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
    
    0 讨论(0)
  • 2020-12-19 14:56

    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 讨论(0)
  • 2020-12-19 14:57

    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.

    0 讨论(0)
提交回复
热议问题