python中opencv中inrange用法_python – 如何使用OpenCV和掩码从图像中选择...

你的颜色范围还不是很正确. inRange()函数中的变量也是错误的顺序.它是从 – 到,所以深色必须是第一个.将您的代码更改为cv2.inRange(hsv_nucl,green2hsv,greenhsv)您可以使用/调整下面代码中的值,这是有效的.

结果:

白色背景:

import numpy as np

import cv2

# load image

img = cv2.imread("Eding.png")

# convert to HSV

hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

# set lower and upper color limits

lower_val = np.array([50,100,170])

upper_val = np.array([70,255,255])

# Threshold the HSV image to get only green colors

mask = cv2.inRange(hsv, lower_val, upper_val)

# apply mask to original image - this shows the green with black blackground

only_green = cv2.bitwise_and(img,img, mask= mask)

# create a black image with the dimensions of the input image

background = np.zeros(img.shape, img.dtype)

# invert to create a white image

background = cv2.bitwise_not(background)

# invert the mask that blocks everything except green -

# so now it only blocks the green area's

mask_inv = cv2.bitwise_not(mask)

# apply the inverted mask to the white image,

# so it now has black where the original image had green

masked_bg = cv2.bitwise_and(background,background, mask= mask_inv)

# add the 2 images together. It adds all the pixel values,

# so the result is white background and the the green from the first image

final = cv2.add(only_green, masked_bg)

#show image

cv2.imshow("img", final)

cv2.waitKey(0)

cv2.destroyAllWindows()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值