【发布时间】: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