-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsquare.py
More file actions
34 lines (24 loc) · 1.04 KB
/
square.py
File metadata and controls
34 lines (24 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import cv2
import numpy as np
def detect_square1():
# 画像を読み込む。
gray = cv2.imread('images/sample.png', cv2.IMREAD_GRAYSCALE)
# gray = cv2.imread('output/mit_005.png', cv2.IMREAD_GRAYSCALE)
# 輪郭抽出
# OpenCV 3 の場合
# contours = cv2.findContours(
# binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[1]
contours = cv2.findContours(
gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[0]
contours_list = sorted(list(contours), key=lambda x: cv2.contourArea(x), reverse=True)
# 一番面積が大きい輪郭を選択する。
# max_cnt = max(contours, key=lambda x: cv2.contourArea(x))
max_cnt = contours_list[0]
# 黒い画像に一番大きい輪郭だけ塗りつぶして描画する。
out = np.zeros_like(gray)
mask = cv2.drawContours(out, [max_cnt], -1, color=255, thickness=-1)
# 背景画像と前景画像を合成
result = np.where(mask == 255, gray, out)
cv2.imwrite('images/out.png', result)
if __name__ == '__main__':
detect_square1()