2013-03-18 40 views
0

我是Python新手。我找到了一个代码来在线计算图像的直方图。 我想计算一个图像的局部区域的直方图,所以我试图使用一个面具。 这是我的代码:如何在Python中提供直方图掩码

i_rgb1 = cv2.imread(im1) 
    img1 = cv2.cvtColor(i_rgb1, cv.CV_BGR2HSV) 
    hist2 = np.zeros(img2.shape) 

bins = np.arange(256).reshape(256, 1) 
color = [(255, 0, 0), (0, 255, 0), (0, 0, 255)] 

for labelx in xrange(len(label)): 
    temp = labels_map.copy() 
    temp[labels_map != label[labelx]] = 0 
    temp[labels_map == label[labelx]] = 255 
    cv2.imwrite('mask.png', temp) 
    for ch, col in enumerate(color): 
     hist_item1 = cv2.calcHist([img1], [ch], temp, [256], [0, 255]) 

其中labels_map是由每个像素的标签分配的图像矩阵。 但是当我运行此代码,我得到一个错误说

OpenCV Error: Assertion failed (!mask.data || mask.type() == CV_8UC1) in unknown function 

请帮我解决这个错误。

回答

1

我想这是因为温度不是uint8遮罩阵列,你可以把它转换:

cv2.calcHist([img1], [ch], temp.astype(np.uint8), [256], [0, 255]) 

或者,当你创建:

temp = (labels_map == label[labelx]).astype(np.uint8) 
+0

是的,它现在正..谢谢:) – Khushboo 2013-03-18 12:33:18