質問編集履歴
6
tuiki
title
CHANGED
File without changes
|
body
CHANGED
@@ -69,45 +69,31 @@
|
|
69
69
|
cv2.destroyAllWindows()
|
70
70
|
|
71
71
|
```
|
72
|
-
・
|
72
|
+
・面積
|
73
73
|
```ここに言語を入力
|
74
|
-
|
74
|
+
-*- coding: utf-8 -*-
|
75
75
|
import cv2
|
76
76
|
import numpy as np
|
77
77
|
|
78
78
|
|
79
|
-
|
79
|
+
img = cv2.imread("/home/pi/picture/alarm9-1.png")
|
80
|
-
|
80
|
+
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV_FULL)
|
81
|
-
|
81
|
+
h, s, v = cv2.split(hsv)
|
82
82
|
|
83
|
-
|
83
|
+
mask = np.zeros_like(h)
|
84
|
-
|
84
|
+
mask[(h > 240) & ((100 < s) & (s < 200)) & (v > 180)] = 255
|
85
85
|
|
86
86
|
|
87
|
-
|
87
|
+
# 輪郭を抽出する。
|
88
|
-
|
88
|
+
mask, contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
|
89
89
|
|
90
|
-
|
90
|
+
# 各輪郭の面積を出力する。(デバッグ用)
|
91
|
-
|
91
|
+
print(list(map(cv2.contourArea, contours)))
|
92
92
|
|
93
|
-
# 指定した範囲の面積を持つ輪郭のみ、抽出する。
|
94
|
-
contours = list(filter(lambda x: 10 <= cv2.contourArea(x) <= 10000, contours))
|
95
|
-
|
96
|
-
# 輪郭に外接する長方形を取得する。
|
97
|
-
rects = map(cv2.boundingRect, contours)
|
98
|
-
|
99
|
-
|
93
|
+
cv2.waitKey(0)
|
100
|
-
|
101
|
-
|
102
|
-
img = cv2.imread("/home/pi/picture/alarm9-1.png")
|
103
|
-
|
104
|
-
rects = find_rect_of_target_color(img)
|
105
|
-
|
106
|
-
for x, y, w, h in rects:
|
107
|
-
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 255), thickness=2)
|
108
|
-
|
109
|
-
cv2.
|
94
|
+
cv2.destroyAllWindows()
|
110
|
-
|
111
95
|
```
|
112
96
|
・出力結果
|
113
|
-
|
97
|
+
```ここに言語を入力
|
98
|
+
[1.5, 0.0, 0.0, 3.0, 2.5, 0.0, 0.5, 7.5, 49.0, 12.5, 0.0, 3.5, 0.0, 0.0, 0.0, 0.0, 4.5, 0.0, 0.0, 3.5, 19.0, 0.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.5, 0.0, 0.5, 1.0, 3.5, 5052.0, 8.5, 5.5, 2.0, 4.0, 4.0, 15.0, 25.5, 2.0, 8.5, 26.0, 5.5, 18.5, 2.0, 4.0, 44.5, 13.5, 33.0, 2.0, 2.0, 22.5, 7.0, 6.0, 5.5, 4.0, 14.5, 8.5, 2.0, 4.0, 2.0, 831.5, 4.5, 0.0, 0.0, 2.0, 4.0, 10.0, 6.0, 2.0, 6.0, 9.5, 2.0, 2.0, 17.0, 14.0, 12.0, 4.0, 2.0, 61.5, 4.0, 4.0, 23.0, 14.5, 4.0, 2.0, 11.5, 2.0, 2.0, 7.5, 43.5, 4.0, 13.0, 4.0, 2.0, 11.0, 4.0, 2.0, 6.0, 4.0, 2.0, 5.5, 6.0, 11.5]
|
99
|
+
```
|
5
追記した
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,9 +1,6 @@
|
|
1
1
|
下記のコードは動画から特定色を検出し、その検出した特定色の一番面積の大きいところを四角で描画するものです。
|
2
2
|
このコードを指定した面積のみ特定色を取り出せるように変更したいのですが、どのようにコードを書けばよいでしょうか。
|
3
3
|
|
4
|
-
○ 追記
|
5
|
-
試しにコードを書いてみて実行したのですが、下に載せている写真のように何も起こらないで、動画が流れるわけでもなく、どのキーを押しても反応しなくなりました。コードが違うのか、それ以外に問題があるのかよくわからないので教えていただきたいです。
|
6
|
-
|
7
4
|
```ここに言語を入力
|
8
5
|
import cv2
|
9
6
|
import numpy as np
|
@@ -72,5 +69,45 @@
|
|
72
69
|
cv2.destroyAllWindows()
|
73
70
|
|
74
71
|
```
|
72
|
+
・追記のコード
|
73
|
+
```ここに言語を入力
|
74
|
+
# -*- coding: utf-8 -*-
|
75
|
+
import cv2
|
76
|
+
import numpy as np
|
77
|
+
|
78
|
+
|
79
|
+
def find_rect_of_target_color(img):
|
80
|
+
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
|
81
|
+
h, s, v = cv2.split(hsv)
|
82
|
+
|
83
|
+
mask = np.zeros_like(h)
|
84
|
+
mask[(h > 240) & ((100 < s) & (s < 200)) & (v > 180)] = 255
|
85
|
+
|
86
|
+
|
87
|
+
# 輪郭を抽出する。
|
88
|
+
mask, contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
|
89
|
+
|
90
|
+
# 各輪郭の面積を出力する。(デバッグ用)
|
91
|
+
print(list(map(cv2.contourArea, contours)))
|
92
|
+
|
93
|
+
# 指定した範囲の面積を持つ輪郭のみ、抽出する。
|
94
|
+
contours = list(filter(lambda x: 10 <= cv2.contourArea(x) <= 10000, contours))
|
95
|
+
|
96
|
+
# 輪郭に外接する長方形を取得する。
|
97
|
+
rects = map(cv2.boundingRect, contours)
|
98
|
+
|
99
|
+
return rects
|
100
|
+
|
101
|
+
|
102
|
+
img = cv2.imread("/home/pi/picture/alarm9-1.png")
|
103
|
+
|
104
|
+
rects = find_rect_of_target_color(img)
|
105
|
+
|
106
|
+
for x, y, w, h in rects:
|
107
|
+
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 255), thickness=2)
|
108
|
+
|
109
|
+
cv2.imwrite("result.png", img)
|
110
|
+
|
111
|
+
```
|
75
|
-
・結果
|
112
|
+
・出力結果
|
76
|
-

|
4
sasa
title
CHANGED
File without changes
|
body
CHANGED
@@ -67,7 +67,7 @@
|
|
67
67
|
elif area >500:
|
68
68
|
continue
|
69
69
|
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), thickness=2)
|
70
|
-
cv2.imshow('red', frame)
|
70
|
+
cv2.imshow('red', frame)
|
71
71
|
capture.release()
|
72
72
|
cv2.destroyAllWindows()
|
73
73
|
|
3
イラン部分消した
title
CHANGED
File without changes
|
body
CHANGED
@@ -36,43 +36,6 @@
|
|
36
36
|
cv2.destroyAllWindows()
|
37
37
|
```
|
38
38
|
|
39
|
-
```ここに言語を入力
|
40
|
-
import cv2
|
41
|
-
import numpy as np
|
42
|
-
|
43
|
-
def find_rect_of_target_color(image):
|
44
|
-
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV_FULL)
|
45
|
-
h = hsv[:, :, 0]
|
46
|
-
s = hsv[:, :, 1]
|
47
|
-
v = hsv[:, :, 2]
|
48
|
-
mask = np.zeros(h.shape, dtype=np.uint8)
|
49
|
-
mask[(h > 240) & ((100 < s) & (s < 200)) & (v > 180)] = 255
|
50
|
-
mask, contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
|
51
|
-
rects = []
|
52
|
-
for contour in contours:
|
53
|
-
approx = cv2.convexHull(contour)
|
54
|
-
rect = cv2.boundingRect(approx)
|
55
|
-
rects.append(np.array(rect))
|
56
|
-
return rects
|
57
|
-
|
58
|
-
capture = cv2.VideoCapture(0)
|
59
|
-
while cv2.waitKey(30) < 0:
|
60
|
-
_, frame = capture.read()
|
61
|
-
rects = find_rect_of_target_color(frame)
|
62
|
-
for x, y, w, h in rects:
|
63
|
-
area = w * h
|
64
|
-
if area < 100:
|
65
|
-
continue
|
66
|
-
elif area >500:
|
67
|
-
continue
|
68
|
-
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), thickness=2)
|
69
|
-
cv2.imshow('red', frame)
|
70
|
-
capture.release()
|
71
|
-
cv2.destroyAllWindows()
|
72
|
-
|
73
|
-
|
74
|
-
```
|
75
|
-
|
76
39
|
・試したコード
|
77
40
|
```ここに言語を入力
|
78
41
|
import cv2
|
2
ついき
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
面積を指定して動画からその面積を持つ特定色のものを認識したい
|
1
|
+
面積(画素数)を指定して動画からその面積(画素数)を持つ特定色のものを認識したい
|
body
CHANGED
File without changes
|
1
追記した
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
下記のコードは動画から特定色を検出し、その検出した特定色の一番面積の大きいところを四角で描画するものです。
|
2
2
|
このコードを指定した面積のみ特定色を取り出せるように変更したいのですが、どのようにコードを書けばよいでしょうか。
|
3
3
|
|
4
|
+
○ 追記
|
5
|
+
試しにコードを書いてみて実行したのですが、下に載せている写真のように何も起こらないで、動画が流れるわけでもなく、どのキーを押しても反応しなくなりました。コードが違うのか、それ以外に問題があるのかよくわからないので教えていただきたいです。
|
6
|
+
|
4
7
|
```ここに言語を入力
|
5
8
|
import cv2
|
6
9
|
import numpy as np
|
@@ -31,4 +34,80 @@
|
|
31
34
|
cv2.imshow('red', frame)
|
32
35
|
capture.release()
|
33
36
|
cv2.destroyAllWindows()
|
34
|
-
```
|
37
|
+
```
|
38
|
+
|
39
|
+
```ここに言語を入力
|
40
|
+
import cv2
|
41
|
+
import numpy as np
|
42
|
+
|
43
|
+
def find_rect_of_target_color(image):
|
44
|
+
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV_FULL)
|
45
|
+
h = hsv[:, :, 0]
|
46
|
+
s = hsv[:, :, 1]
|
47
|
+
v = hsv[:, :, 2]
|
48
|
+
mask = np.zeros(h.shape, dtype=np.uint8)
|
49
|
+
mask[(h > 240) & ((100 < s) & (s < 200)) & (v > 180)] = 255
|
50
|
+
mask, contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
|
51
|
+
rects = []
|
52
|
+
for contour in contours:
|
53
|
+
approx = cv2.convexHull(contour)
|
54
|
+
rect = cv2.boundingRect(approx)
|
55
|
+
rects.append(np.array(rect))
|
56
|
+
return rects
|
57
|
+
|
58
|
+
capture = cv2.VideoCapture(0)
|
59
|
+
while cv2.waitKey(30) < 0:
|
60
|
+
_, frame = capture.read()
|
61
|
+
rects = find_rect_of_target_color(frame)
|
62
|
+
for x, y, w, h in rects:
|
63
|
+
area = w * h
|
64
|
+
if area < 100:
|
65
|
+
continue
|
66
|
+
elif area >500:
|
67
|
+
continue
|
68
|
+
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), thickness=2)
|
69
|
+
cv2.imshow('red', frame)
|
70
|
+
capture.release()
|
71
|
+
cv2.destroyAllWindows()
|
72
|
+
|
73
|
+
|
74
|
+
```
|
75
|
+
|
76
|
+
・試したコード
|
77
|
+
```ここに言語を入力
|
78
|
+
import cv2
|
79
|
+
import numpy as np
|
80
|
+
|
81
|
+
def find_rect_of_target_color(image):
|
82
|
+
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV_FULL)
|
83
|
+
h = hsv[:, :, 0]
|
84
|
+
s = hsv[:, :, 1]
|
85
|
+
v = hsv[:, :, 2]
|
86
|
+
mask = np.zeros(h.shape, dtype=np.uint8)
|
87
|
+
mask[(h > 240) & ((100 < s) & (s < 200)) & (v > 180)] = 255
|
88
|
+
mask, contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
|
89
|
+
rects = []
|
90
|
+
for contour in contours:
|
91
|
+
approx = cv2.convexHull(contour)
|
92
|
+
rect = cv2.boundingRect(approx)
|
93
|
+
rects.append(np.array(rect))
|
94
|
+
return rects
|
95
|
+
|
96
|
+
capture = cv2.VideoCapture(0)
|
97
|
+
while cv2.waitKey(30) < 0:
|
98
|
+
_, frame = capture.read()
|
99
|
+
rects = find_rect_of_target_color(frame)
|
100
|
+
for x, y, w, h in rects:
|
101
|
+
area = w * h
|
102
|
+
if area < 100:
|
103
|
+
continue
|
104
|
+
elif area >500:
|
105
|
+
continue
|
106
|
+
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), thickness=2)
|
107
|
+
cv2.imshow('red', frame)
|
108
|
+
capture.release()
|
109
|
+
cv2.destroyAllWindows()
|
110
|
+
|
111
|
+
```
|
112
|
+
・結果
|
113
|
+

|