Хочу сделать свою функцию inRange, которая позволяла бы в hsv_max кидать значения, меньшие, чем в hsv_min. Зачем это нужно, рассказано
здесь.
код:
import cv2
import numpy as np
def my_inRange(img_hsv, hsv_max, hsv_min):
img_h = np.shape(img_hsv)[0]
img_w = np.shape(img_hsv)[1]
img_result = np.array([[255 for i in range(img_w)] for j in range(img_h)])
for i in range(img_h):
for j in range(img_w):
now_hsv = img_hsv[i, j]
for n in range(3):
if hsv_max[n] > hsv_min[n]:
if now_hsv[n] < hsv_min[n] or now_hsv[n] >= hsv_max[n]:
img_result[i, j] = 0
break
else:
if now_hsv[n] < hsv_min[n] and now_hsv[n] >= hsv_max[n]:
img_result[i, j] = 0
break
return img_result
def nothing(*arg):
pass
cv2.namedWindow("result")
cv2.namedWindow("pic")
cv2.namedWindow("settings")
img = cv2.imread("roof.png")
cv2.createTrackbar('h1', 'settings', 0, 255, nothing)
cv2.createTrackbar('s1', 'settings', 0, 255, nothing)
cv2.createTrackbar('v1', 'settings', 0, 255, nothing)
cv2.createTrackbar('h2', 'settings', 255, 255, nothing)
cv2.createTrackbar('s2', 'settings', 255, 255, nothing)
cv2.createTrackbar('v2', 'settings', 255, 255, nothing)
while True:
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
h1 = cv2.getTrackbarPos('h1', 'settings')
s1 = cv2.getTrackbarPos('s1', 'settings')
v1 = cv2.getTrackbarPos('v1', 'settings')
h2 = cv2.getTrackbarPos('h2', 'settings')
s2 = cv2.getTrackbarPos('s2', 'settings')
v2 = cv2.getTrackbarPos('v2', 'settings')
h_min = np.array((h1, s1, v1), np.uint8)
h_max = np.array((h2, s2, v2), np.uint8)
thresh = my_inRange(hsv, h_min, h_max)
cv2.imshow('result', thresh)
cv2.imshow('pic', img)
ch = cv2.waitKey(5)
if ch == 27:
break
print(type(img))
cv2.destroyAllWindows()
выдает ошибку
Traceback (most recent call last):
File "D:/.../final6_podbor_1.py", line 52, in <module>
cv2.imshow('result', thresh)
cv2.error: OpenCV(4.0.0) c:\projects\opencv-python\opencv\modules\imgproc\src\color.hpp:261: error: (-2:Unspecified error) in function '__cdecl cv::CvtHelper<struct cv::Set<1,-1,-1>,struct cv::Set<3,4,-1>,struct cv::Set<0,2,5>,2>::CvtHelper(const class cv::_InputArray &,const class cv::_OutputArray &,int)'
> Unsupported depth of input image:
> 'VDepth::contains(depth)'
> where
> 'depth' is 4 (CV_32S)
Что сделать, чтобы все заработало?