使用Python和&找到红色OpenCV的(Finding red color using Python & OpenCV)

http://www.it1352.com/896060.html

Edit. I came with not much elegant, but working solution:

image_result = np.zeros((image_height,image_width,3),np.uint8)

for i in range(image_height):  #those are set elsewhere
    for j in range(image_width): #those are set elsewhere
        if img_hsv[i][j][1]>=50 \
            and img_hsv[i][j][2]>=50 \
            and (img_hsv[i][j][0] <= 10 or img_hsv[i][j][0]>=170):
            image_result[i][j]=img_hsv[i][j]

I would just add the masks together, and use np.where to mask the original image.

img=cv2.imread("img.bmp")
img_hsv=cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

# lower mask (0-10)
lower_red = np.array([0,50,50])
upper_red = np.array([10,255,255])
mask0 = cv2.inRange(img_hsv, lower_red, upper_red)

# upper mask (170-180)
lower_red = np.array([170,50,50])
upper_red = np.array([180,255,255])
mask1 = cv2.inRange(img_hsv, lower_red, upper_red)

# join my masks
mask = mask0+mask1

# set my output img to zero everywhere except my mask
output_img = img.copy()
output_img[np.where(mask==0)] = 0

# or your HSV image, which I *believe* is what you want
output_hsv = img_hsv.copy()
output_hsv[np.where(mask==0)] = 0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值