创建一个带有图像框架的对象

时间:2019-02-28 22:15:00

标签: python opencv oop cv2

我正在开发一个程序,该程序可以从照相机中获取帧并分析特定区域以寻找红色,效果很好,但是我需要分析多个区域,因此我创建了一个对象使其变得更容易使用,但并没有做到这一点。我做的时候没有工作 这是工作代码

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()

{{1}}

0 个答案:

没有答案