Я работаю над программой, которая берет кадры с камеры и анализирует определенную область в поисках красного цвета, она работала хорошо, но мне нужно было проанализировать более одной области, поэтому я создал объект, чтобы сделать его легче, но это не таккогда я это сделал, работает код
import cv2
import numpy as np
lower = np.array([163, 84, 93],np.uint8)
upper = np.array([187, 237, 255],np.uint8)
x1 = 51
x2=586
y1=111
y2=381
w=50
cam_capture = cv2.VideoCapture(0)
cv2.destroyAllWindows()
Top_left = (x1+1, y1-1-w)
Top_right = (x2+1, y1-1)
top_pixles=abs((y1-1-y1-1-w)*(x2+1-x1+1))
while True:
_, image_frame = cam_capture.read()
# Top Rectangle marker
top = cv2.rectangle(image_frame, Top_left, Top_right, (100, 50, 200), 5)
rect_top = image_frame[Top_left[1]: Top_right[1], Top_left[0]:Top_right[0]]
# top red
hsv_top = cv2.cvtColor(rect_top, cv2.COLOR_BGR2HSV)
top_red = cv2.inRange(hsv_top, lower, upper)
top_red_pixle = cv2.countNonZero(top_red)
print("\t"+str(int(top_red_pixle/top_pixles*100)))
cv2.imshow("Sketcher ROI", image_frame)
key = cv2.waitKey(1) & 0xFF
if key == 27:
break
cv2.destroyAllWindows()
, и это мой новый код, выводит эту ошибку
Traceback (most recent call last):
line 35, in <module>
red=p.count_red(image_frame)
line 21, in count_red
cv2.rectangle(frame, (self.x1,self.y1), (self.x2,self.y2), (100, 50, 200), 5)
TypeError: an integer is required (got type tuple)
[ WARN:0] terminating async callback
import cv2
import numpy as np
lower = np.array([163, 84, 93],np.uint8)
upper = np.array([187, 237, 255],np.uint8)
class roirect():
x1=0
y1=0
x2=0
y2=0
total_pixles = 0
def init_rect(self,x1,y1,x2,y2):
self.x1=x1
self.y1 = y1
self.x2 = x2
self.y2 = y2
self.total_pixles = abs((y2 - y1) * (x2 - x1))
def count_red(self,frame):
cv2.rectangle(frame, (self.x1,self.y1), (self.x2,self.y2), (100, 50, 200), 5)
roi = frame[self.y1: self.y2, self.x1: self.x2]
hsv_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
roi_red = cv2.inRange(hsv_roi, lower, upper)
roi_red_pixle = cv2.countNonZero(roi_red)
return (int(roi_red_pixle / self.total_pixles * 100))
cam_capture = cv2.VideoCapture(0)
cv2.destroyAllWindows()
p=roirect()
p.init_rect(50,50,200,200)
while True:
image_frame = cam_capture.read()
red=p.count_red(image_frame)
cv2.imshow("Sketcher ROI", image_frame)
key = cv2.waitKey(1) & 0xFF
if key == 27:
break
cv2.destroyAllWindows()