【问题标题】:OpenCV Python : how to use a contour as a mask in calcHist?OpenCV Python:如何在 calcHist 中使用轮廓作为掩码?
【发布时间】:2020-01-01 11:51:57
【问题描述】:

我正在尝试获取图片上某个区域的 RGB 直方图。我已经通过对图片进行阈值化来隔离我的区域(背景是明亮的,而我的隔离区域是黑暗的)。我知道如何通过在 calcHist OpenCV 函数中使用我的区域的轮廓作为掩码来制作我的整个图片的颜色直方图,而不是我的区域的 RGB 直方图。

我实际上做的是:

#I threshlod my picture to obtain my objects of interest
threshold = threshold3(img, param['thresh_red_low'], param['thresh_red_high'], param['thresh_green_low'], param['thresh_green_high'], param['thresh_blue_low'])

#I find contours of my objects
contours = cv2.findContours(threshold , cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[0]

#For each of my objects
for indexx, contour in enumerate(contours):

    #If I directly try to put contour as a mask in calcHist, I got an error
    #I convert the contour into a mask
    mask = cv2.drawContours(image_color, contour, -1, (255, 255, 255), 2)

    #I calculate histograms for BGR channel, on ten ranges, from 5 to 256
    b_hist = cv2.calcHist([image_color],[0],mask,[10],[5,256])                        
    g_hist = cv2.calcHist([image_color],[1],mask,[10],[5,256])
    r_hist = cv2.calcHist([image_color],[2],mask,[10],[5,256])

   #Then I save results into a csv

但是我在每个直方图范围内得到了太多的值。例如,我的第一个区域的面积为 6371 px,其直方图值为:

Number of red pixels per range : 388997,500656,148124,97374,198893,793015,894672,1232693,674721,105807

Number of green pixels per range :
123052,478714,349357,153624,117838,105738,84656,1205018,1356478,1064373

Number of blue pixels per range :
1590057,702532,547988,430238,320658,103876,15366,7629,1527,2645

这更像是整个图片的直方图而不是区域的。我对 calcHist 函数中的蒙版和轮廓有什么不明白的地方?

【问题讨论】:

    标签: python-2.7 opencv histogram mask contour


    【解决方案1】:

    很抱歉这么晚才回复,但我希望这可能对其他人有所帮助。

    总的来说,您的代码是正确的,只是您可能只需要添加一两行并稍微修改一行。

    #I threshlod my picture to obtain my objects of interest
    threshold = threshold3(img, param['thresh_red_low'], param['thresh_red_high'], param['thresh_green_low'], param['thresh_green_high'], param['thresh_blue_low'])
    
    #I find contours of my objects
    contours = cv2.findContours(threshold , cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[0]
    #For each of my objects
    for indexx, contour in enumerate(contours):
    #If I directly try to put contour as a mask in calcHist, I got an error
    #I convert the contour into a mask
    
    w, h = img.shape
    mask = np.zeros((h, w), dtype="uint8")
    cv2.drawContours(mask, contours, indexx, 255, cv2.FILLED)
    
    
    #I calculate histograms for BGR channel, on ten ranges, from 5 to 256
    b_hist = cv2.calcHist([image_color],[0],mask,[10],[5,256])                        
    g_hist = cv2.calcHist([image_color],[1],mask,[10],[5,256])
    r_hist = cv2.calcHist([image_color],[2],mask,[10],[5,256])
    
    #Then I save results into a csv
    

    此解决方案假定您对轮廓内的所有内容感兴趣。

    【讨论】:

      猜你喜欢
      • 2015-11-30
      • 2017-05-26
      • 2016-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-11
      • 2015-07-31
      • 2021-06-16
      相关资源
      最近更新 更多