JavaScript
x
50
50
1
while(True):
2
3
_, frame = cap.read()
4
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
5
a, b = 0, 0
6
7
height, width, layers = frame.shape
8
start_height = height - height
9
left_x = width - 500
10
11
#right_area = frame[y:y+height, x:x+300]
12
#left_area = frame[y:y+height,]
13
14
black_min = np.array([0, 0, 0], np.uint8)
15
black_max = np.array([179, 255, 70], np.uint8)
16
17
red_min = np.array([0, 70, 50], np.uint8)
18
red_max = np.array([20, 255, 255], np.uint8)
19
20
red1 = np.array([170, 70, 50], np.uint8)
21
red2 = np.array([180, 255, 255], np.uint8)
22
23
red_mask = cv2.inRange(hsv, red1, red2)
24
red_mask2 = cv2.inRange(hsv, red_min, red_max)
25
kernel = np.ones((7, 7), "uint8")
26
27
red_mask = cv2.dilate(red_mask, kernel)
28
res_red = cv2.bitwise_and(frame, frame, mask=red_mask)
29
red_mask2 = cv2.dilate(red_mask2, kernel)
30
res_red2 = cv2.bitwise_and(frame, frame, mask=red_mask2)
31
32
area1 = frame[a:a + height, b:b + 300]
33
area2 = frame[a:a + height, left_x:left_x + 500]
34
35
cnts, _ = cv2.findContours(red_mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
36
37
38
for pic, c in enumerate(cnts):
39
40
area = cv2.contourArea(c)
41
if (700 > area > 350):
42
x,y,w,h = cv2.boundingRect(c)
43
cv2.rectangle(frame, (x, y),(x + w, y + h),(0, 0, 255), 2)
44
cv2.putText(frame, "Red detected", (x, y), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 255))
45
46
47
cv2.imshow("Video from camera", frame)
48
if cv2.waitKey(10) & 0xFF == ord('q'):
49
break
50
I belive that for C++ the solution looks like this:
findContours(Mask(cv::Rect(x,y,width,height)), contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0,0) );
How can I change this line to get the results I want?
this is the area I’m interested in frame[a:a + height, b:left_x + 300] Thanks in advance!
Advertisement
Answer
your code looks strange to me, here a snippet with a line
JavaScript
1
24
24
1
QString line="144,81:626,232";
2
QStringList points = line.split(":");
3
qDebug()<< points[0];
4
qDebug()<< points[1];
5
QRectF rectangle;
6
QStringList xy1 = points[0].split(",");
7
qDebug()<< xy1[0];
8
qDebug()<< xy1[1];
9
auto x1=QString(xy1[0]).toFloat();
10
auto y1=QString(xy1[1]).toFloat();
11
12
rectangle.setTopLeft(QPointF(x1,y1));
13
14
xy1 = points[1].split(",");
15
qDebug()<< xy1[0];
16
qDebug()<< xy1[1];
17
x1=QString(xy1[0]).toFloat();
18
y1=QString(xy1[1]).toFloat();
19
20
rectangle.setBottomRight(QPointF(x1,y1));
21
22
qDebug() << "Rec: " << rectangle;
23
return 0;
24
so for the input "144,81:626,232";
you get the output:
JavaScript
1
2
1
Rec: QRectF(144,81 482x151)
2
because that is the way qt print rectangle objects-> topLeftPoint wxh