1
使用HSV顏色,當我試圖執行基於顏色與cv2.inRange對象檢測困惑(Python 2.7版)。使用BGR顏色時,一切看起來都很好。然而,當我映射BGR顏色HSV,我不能得到正確的面具。見下面的例子:在HSVOpenCV的 - 蟒蛇 - 在cv2.inRange
# first convert the img, and the associated lower and upper bound to HSV
hsv_img_test = cv2.cvtColor(img_test, cv2.COLOR_BGR2HSV)
lower_hsv = cv2.cvtColor(np.uint8([[[b-step,g-step,r-step]]]), cv2.COLOR_BGR2HSV)
upper_hsv = cv2.cvtColor(np.uint8([[[b+step,g+step,r+step]]]), cv2.COLOR_BGR2HSV)
plt.figure(figsize=(20,10))
plt.subplot(1,2,1)
plt.imshow(cv2.cvtColor(hsv_img_test, cv2.COLOR_BGR2RGB))
# apply threshold on hsv image
mask = cv2.inRange(hsv_img_test, lower_hsv, upper_hsv)
plt.subplot(1,2,2)
plt.imshow(mask, cmap='gray')
在BGR
img_test = cv2.imread("test_img/mario.jpeg")
#define color range for object detection
step = 10
r,g,b = 203, 31, 25 #red
lower_bgr = np.uint8([b-step, g-step, r-step])
upper_bgr = np.uint8([b + step, g + step, r + step])
# plot mario in BGR and corresponding mask
plt.figure(figsize=(20,10))
plt.subplot(1,2,1)
plt.imshow(cv2.cvtColor(img_test, cv2.COLOR_BGR2RGB))
mask = cv2.inRange(img_test, lower_bgr, upper_bgr)
plt.subplot(1,2,2)
plt.imshow(mask, cmap='gray')
2)閾值(不正常
1)閾值)..這顯然不正確。我無法弄清楚代碼中的錯誤,任何幫助都將不勝感激!
這工作如果'target_color_h'是一個符號類型。要在保存方面,也許用'最大(公差,target_color_h) - tolerance' – Kjell