我有一個 BGR 顏色格式的車牌影像。我將此影像翻譯成 HSV。在這里,我可以將顏色通道分別拆分為 H、S 和 V。現在我只想從 Value 影像中選擇紅色,并將其他所有顏色設定為白色。我已經找到了使用蒙版的下限和上限以及使用 cv2.inRange 從經典影像中選擇紅色的方法,但我還沒有找到針對此特定任務的解決方案。
輸入影像:

轉換為 HSV 并根據通道拆分:

代碼:
import cv2
import matplotlib.pyplot as plt
import numpy as np
img = cv2.imread('zg4515ah.png')
cv2_imshow(img)
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
fig, ax = plt.subplots(1, 3, figsize=(15,5))
ax[0].imshow(img_hsv[:,:,0], cmap='hsv')
ax[0].set_title('Hue',fontsize=15)
ax[1].imshow(img_hsv[:,:,1], cmap='hsv')
ax[1].set_title('Saturation',fontsize=15)
ax[2].imshow(img_hsv[:,:,2], cmap='hsv')
ax[2].set_title('Value',fontsize=15)
plt.show()
lower_red = np.array([155,25,0])
upper_red = np.array([179,255,255])
mask = cv2.inRange(img_hsv, lower_red, upper_red)
# or your HSV image, which I *believe* is what you want
output_hsv = img_hsv.copy()
output_hsv[np.where(mask==0)] = 0
imgplot = plt.imshow(output_hsv)
plt.show()
我知道我正在嘗試在整個 HSV 影像上應用蒙版,但是當我嘗試僅應用到值部分時,由于陣列維度不匹配,我收到錯誤訊息。這是我目前得到的結果:

uj5u.com熱心網友回復:
一個簡單的掩碼就可以作業,不需要,cv2.inRange因為value大于 40 的值不是紅色的:
img = cv2.imread('image.png')
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
fig, ax = plt.subplots(1, 3, figsize=(15,5))
ax[0].imshow(img_hsv[:,:,0], cmap='hsv')
ax[0].set_title('Hue',fontsize=15)
ax[1].imshow(img_hsv[:,:,1], cmap='hsv')
ax[1].set_title('Saturation',fontsize=15)
ax[2].imshow(img_hsv[:,:,2], cmap='hsv')
ax[2].set_title('Value',fontsize=15)
plt.show()
# or your HSV image, which I *believe* is what you want
output_hsv = img_hsv.copy()
# Using this threshhold
output_hsv[np.where(output_hsv[:,:,2]>40)] = 255
imgplot = plt.imshow(output_hsv)

或與 output_hsv[np.where(output_hsv[:,:,2]<40)] = 255

轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/367622.html
