-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontourtest.py
69 lines (52 loc) · 1.69 KB
/
contourtest.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from skimage import exposure
import numpy as np
import argparse
import cv2
import os
ap = argparse.ArgumentParser()
ap.add_argument("-q", "--query", required = True, help = "path to query image")
args = vars(ap.parse_args())
image = cv2.imread(args["query"])
orig = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.bilateralFilter(gray, 11, 17, 17)
edged = cv2.Canny(gray, 30, 200)
#cv2.imshow("canny", edged)
#cv2.waitKey(0)
im2, contours, hierarchy = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = sorted(contours, key = cv2.contourArea, reverse = True)[:10]
largestPanel = None
contourArray = []
#badContoursArray = []
for c in contours:
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.02 * peri, True)
#print(cv2.contourArea(c))
#if(cv2.contourArea(c) < 20000:
# badContours.append(approx)
if len(approx) == 4:
contourArray.append(approx)
#largestPanel = approx
#break
mask = np.zeros_like(image)
cv2.drawContours(mask, contourArray, -1, [255, 255, 255], -1)
#ret,thresh = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)
#cv2.imshow("contour", image)
#cv2.waitKey(0)
out = np.zeros_like(image)
out[mask == [255, 255, 255]] = image[mask == [255, 255, 255]]
#cv2.drawContours(image, contourArray, -1, (0, 255, 0), 3)
#cv2.imshow("contour", out)
#cv2.waitKey(0)
##ROI
os.chdir("/home/wangbri/Desktop/imgsplice/splitpanels")
filename = args["query"]
if filename.endswith('.png'):
filename = filename[:-4]
panelCnt = 1;
for c in contours:
if cv2.contourArea(c) > 20000:
x, y, width, height = cv2.boundingRect(c)
roi = orig[y:y+height, x:x+width]
cv2.imwrite(filename + "_" + str(panelCnt)+".png", roi)
panelCnt += 1