-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContourTest1.py
More file actions
204 lines (166 loc) · 7.34 KB
/
ContourTest1.py
File metadata and controls
204 lines (166 loc) · 7.34 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
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import cv2
import json
import os
import numpy as np
# physical constants
PIXELS_PER_MM = 20.0 #number of pixels each mm is on the printer
# constants
IMAGE_PATH = 'C:/Users/Will/Desktop/RavenTech/PythonCameraContours/Scan029.jpg'
DESTINATION_PATH = 'C:/Users/Will/Desktop/RavenTech/PythonCameraContours/Exports/'
IMAGE_RESOLUTION = (cv2.imread(IMAGE_PATH).shape[1], cv2.imread(IMAGE_PATH).shape[0]) #width, height
RESIZE_SCALE = 1
RESIZE_DIMS = (int(IMAGE_RESOLUTION[0]*RESIZE_SCALE), int(IMAGE_RESOLUTION[1]*RESIZE_SCALE)) # dimensions to resize image for consistent processing
BLUR_KSIZE = (3, 3) # kernel size for gaussian blur
CANNY_THRESH = (1, 40) # (low, high) thresholds for canny edge detector for edge sensitivity
MORPH_KSIZE = (40, 40) # size of structuring element in closing operation to fill small gaps
EPSILON_FACTOR = 0.05 # proportion of arc length used by approxPolyDP to simplify a polygon
ELLIPTICAL_DILATION = (10, 10) #kernel size for elliptical dilation to bridge small gaps in edge map
def load_and_resize(path, dims):
img = cv2.imread(path)
return cv2.resize(img, dims, interpolation=cv2.INTER_AREA)
def to_grayscale_and_blur(img, ksize):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, ksize, 0)
return blurred
def detect_edges(img, low_high, kernel_size):
low, high = low_high
edges = cv2.Canny(img, low, high)
# dilate to close small gaps
dil_k = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, ELLIPTICAL_DILATION)
edges_d = cv2.dilate(edges, dil_k, iterations=1)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, kernel_size)
closed = cv2.morphologyEx(edges_d, cv2.MORPH_CLOSE, kernel)
return closed
def find_largest_contour(edges):
"""
Return a tuple with the first item being the largest contour, and the second being all the contours
"""
contours, _ = cv2.findContours(edges,
cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_NONE)
if not contours:
return None, []
largest_contour = max(contours, key=lambda c: cv2.arcLength(c, False))
if is_contour_closed(largest_contour) == False:
#force closed with convex hull
largest_contour = cv2.convexHull(largest_contour)
return largest_contour, contours
def is_contour_closed(cnt, tolerance=0):
"""
Return True if the contour cnt is closed, i.e. its start and end points
are closer than tol pixels.
"""
# cnt has shape (N,1,2) so extract the xy pairs
p_start = cnt[0][0]
p_end = cnt[-1][0]
return np.linalg.norm(p_start - p_end) < tolerance
def approximate_rectangle(contour, factor):
peri = cv2.arcLength(contour, True)
return cv2.approxPolyDP(contour, factor * peri, True)
def draw_contours(img, all_ctrs, largest, approx):
out = img.copy()
# draw all detected contours in blue
cv2.drawContours(out, all_ctrs, -1, (255, 0, 0), 2)
# highlight largest in red
if largest is not None:
cv2.drawContours(out, [largest], -1, (0, 0, 255), 2)
# highlight 4-point approximation in green
if approx is not None and len(approx) <= 6:
cv2.drawContours(out, [approx], -1, (0, 255, 0), 2)
return out
def show_and_save(img, name, fname):
cv2.imshow(name, img)
img_name = os.path.splitext(os.path.basename(IMAGE_PATH))[0]
fname = img_name+'_'+fname
fname = os.path.dirname(DESTINATION_PATH)+'/'+fname
cv2.imwrite(fname, img)
def crop_image(img_full_res, img_scaled, contour_scaled, resize_scale=RESIZE_SCALE):
# get bounding box in scaled space
x, y, w, h = cv2.boundingRect(contour_scaled)
# convert to full resolution
crop_x, crop_y, width, height = map(lambda v: int(v / resize_scale),
(x, y, w, h))
# crop full-res img
img_cropped = img_full_res[crop_y:crop_y+height, crop_x:crop_x+width]
#rescale contour (float->int)
contour = (contour_scaled.astype(np.float32)*[(1/RESIZE_SCALE), (1/RESIZE_SCALE)]).astype(np.int32)
#move its origin relative to crop
shift = np.array([[[crop_x, crop_y]]], dtype=np.int32) # shape (1,1,2)
contour_cropped = contour - shift
# draw it and save
rectangle_pts = approximate_rectangle(contour_cropped, EPSILON_FACTOR)
img_cropped_contours = draw_contours(
img_cropped,
None,
contour_cropped,
rectangle_pts
)
show_and_save(img_cropped_contours, 'CROPPED IMAGE' ,'cropped_image.jpg')
# save metadata
make_json(crop_x, crop_y, width, height, contour_cropped)
return img_cropped
def make_json(crop_x, crop_y, width, height, contour,
image_path=IMAGE_PATH, destination_path=DESTINATION_PATH,pixels_per_mm=PIXELS_PER_MM):
"""
Generate a JSON metadata file alongside `image_path` containing:
- original_image_size: [width, height]
- crop rectangle (x, y, width, height)
- pixels_per_mm scale factor
Returns the path to the created JSON file.
"""
# Grab image name
img_name = os.path.splitext(os.path.basename(IMAGE_PATH))[0]
# Read image once to get resolution
img = cv2.imread(image_path)
if img is None:
raise FileNotFoundError(f"Could not load image at {image_path!r}")
img_resolution = (img.shape[1], img.shape[0]) # width, height
#grab coordinates
coordinates = []
for point in contour:
coord = point[0] # grabs [ x, y ] pair
x = int(coord[0])
y = int(coord[1])
coordinates.append({'x': x, 'y':y})
# Build metadata dict
metadata = {
"image_name": img_name,
"original_image_size": [img_resolution[0], img_resolution[1]],
"pixels_per_mm": pixels_per_mm,
"crop": {
"x": crop_x,
"y": crop_y,
"width": width,
"height": height
},
"contour": coordinates,
}
# Build json filename
base_filename = os.path.splitext(os.path.basename(image_path))[0] #gets name of image
json_filename = os.path.join(
os.path.dirname(destination_path),
f"{base_filename}_metadata.json")
# Write to disk
with open(json_filename, "w") as file:
json.dump(metadata, file, indent=4)
return json_filename
def main():
img = load_and_resize(IMAGE_PATH, RESIZE_DIMS)
pre = to_grayscale_and_blur(img, BLUR_KSIZE)
edges = detect_edges(pre, CANNY_THRESH, MORPH_KSIZE)
largest_contour, contours = find_largest_contour(edges)
approx = None
if largest_contour is not None:
approx = approximate_rectangle(largest_contour, EPSILON_FACTOR)
#if len(approx) != 4:
# print('RUNTIME ERROR: could not approximate rectangle adjust settings')
result = draw_contours(img, contours, largest_contour, approx)
largest_contour_image = draw_contours(img, None, largest_contour, approx)
crop_image(cv2.imread(IMAGE_PATH), img, approx)
show_and_save(edges, 'edges', 'edges.jpg')
show_and_save(result,'contours', 'contours.jpg')
show_and_save(largest_contour_image, 'result', 'result.jpg')
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == '__main__':
main()