-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_shape.py
42 lines (30 loc) · 1.4 KB
/
test_shape.py
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
35
36
37
38
39
40
41
42
import cv2
import numpy as np
from matplotlib import pyplot as plt
def process(img):
work_img = img.copy()
# _, threshold = cv2.threshold(work_img, 137, 255, 0)
_, threshold = cv2.threshold(work_img, 30, 200, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
contours, hierarchy = cv2.findContours(threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
max_area = 0
max_contour = []
for cnt in contours:
approx = cv2.approxPolyDP(cnt, 0.1 * cv2.arcLength(cnt, True), True)
cnt_area = cv2.contourArea(cnt)
if len(approx) > 3 and cnt_area > max_area:
max_area = cnt_area
max_contour = approx
work_img = cv2.cvtColor(work_img, cv2.COLOR_GRAY2RGB)
cv2.drawContours(work_img, [max_contour], 0, (255, 0, 0), 4)
return work_img, threshold
img = cv2.imread("img_test/5.jpg", 0)
img_blur = cv2.blur(img, (20, 20))
res_img, threshold = process(img)
plt.subplot(231), plt.imshow(cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)), plt.title('Image')
plt.subplot(232), plt.imshow(threshold), plt.title('Threshold')
plt.subplot(233), plt.imshow(res_img), plt.title('Largest shape')
res_img, threshold = process(img_blur)
plt.subplot(234), plt.imshow(cv2.cvtColor(img_blur, cv2.COLOR_BGR2RGB)), plt.title('Image blurred')
plt.subplot(235), plt.imshow(threshold), plt.title('Threshold from blurred')
plt.subplot(236), plt.imshow(res_img), plt.title('Largest shape')
plt.show()