Skip to content

Commit

Permalink
update: 赤色の最大輪郭を求める時、2階層の構造を考慮するようにした
Browse files Browse the repository at this point in the history
  • Loading branch information
takahashitom committed Nov 2, 2024
1 parent 41f6573 commit a420905
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions front_camera/front_camera/color_rectangle_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,37 @@ def detect_rectangle(self, frame):
cv2.imwrite(mask_path, mask) # マスク画像を保存


# 輪郭を検出
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# 最大の輪郭を見つける
if contours:
largest_contour = max(contours, key=cv2.contourArea)
return cv2.boundingRect(largest_contour)
if self.color == 'RED':
# 赤色の場合、2階層構造を考慮して最大の輪郭を見つける
# 輪郭を検出し、2階層の階層構造を取得
contours, hierarchy = cv2.findContours(mask, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
# 最大の面積を持つ輪郭を探す
largest_contour_index = None
max_area = 0

for i in range(len(contours)):
# 外側の輪郭だけを対象にする
if hierarchy[0][i][3] == -1: # 親がいない輪郭のみ
area = cv2.contourArea(contours[i])
child_index = hierarchy[0][i][2]
if child_index != -1:
area -= cv2.contourArea(contours[child_index])

if area > max_area:
max_area = area
largest_contour_index = i

# 最大の輪郭が見つかれば、その輪郭の矩形座標を返す
if largest_contour_index is not None:
return cv2.boundingRect(contours[largest_contour_index])
else:
# 輪郭を検出
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# 最大の輪郭を見つける
if contours:
largest_contour = max(contours, key=cv2.contourArea)
return cv2.boundingRect(largest_contour)

return None

if __name__ == "__main__":
Expand Down

0 comments on commit a420905

Please sign in to comment.