2
我目前正在嘗試手動爲圖像中繪製的幾個矩形創建圖例。但我對着色感到困惑。着色問題 - 打開cv與matplotlib圖例的組合
color_step = 255/(len(contours)+1)
patch =[]
for cnt_idx, cnt in enumerate(contours):
cnt_color = color_step * (cnt_idx+1)
cv2.rectangle(img,(cnt['x'],cnt['y']), \
(cnt['x']+cnt['w'], \
cnt['y']+cnt['h']),cnt_color,thickness)
patch.append(mpatches.Patch(color = (cnt_color/255.0,cnt_color/255.0,cnt_color/255.0), label='%d' %cnt_idx))
到底只需要調用:
patch = np.array(patch)
plt.legend(handles=patch)
plt.imshow(img, interpolation = 'none', cmap='gray')
我發現,該方法cv2.rectangle剛好分配在矩形的具體位置在cnt_color定義的值。不幸的是我不知道如何定義補丁的顏色。
您可以使用離散的顏色條作爲此答案:(http://stackoverflow.com/questions/14777066/matplotlib-discrete-colorbar)而不是構建圖例。 –