定位到目标图像区域
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) # 转化为 HSV 格式
thresh1 = np.array([190, 0.35, 0.3]) # 目标的低阈值
thresh2 = np.array([245, 1, 1]) # 目标的高阈值
img_1 = cv2.inRange(img_hsv, lowerb = thresh1, upperb = thresh2)
cv2.imshow('原始图像', img) # 显示图像
cv2.imshow('截取图像', img_1)
cv2.waitKey(0)
cv2.destroyAllWindows()
Traceback (most recent call last):
File "D:/Program Files/pythonProject233/6.py", line 12, in
img_1 = cv2.inRange(img_hsv, lowerb = thresh1, upperb = thresh2)
cv2.error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\core\src\arithm.cpp:1782: error: (-215:Assertion failed) lb.type() == ub.type() in function 'cv::inRange'
thresh1 = np.array([190, 0.35, 0.3]) # 目标的低阈值
thresh2 = np.array([245, 1, 1]) # 目标的高阈值 这2行代码的问题,thresh1.dtype为float64,但是thresh2.dtype为int32,类型不一致导致的错误。要么把thresh1里的元素都改成整数型,要么把thresh2里的某一个元素改成浮点型,比如thresh2 = np.array([245, 1, 1.0])
hsv颜色阈值应该是H:0-180,S:0-255,V:0-255,
你现在给的这个色域空间有问题,H全在范围之外了,S和V等比例都*255就是实际的范围
你参考一下吧,看看你到底要什么颜色