-
Notifications
You must be signed in to change notification settings - Fork 0
/
crop.py
165 lines (135 loc) · 5.84 KB
/
crop.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
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
import cv2 as cv
import numpy as np
import screeninfo
import key_codes as key
def assistedCircleCrop(image, houghAnalysisSize=400):
windowName = "MobyCAIRO - Assisted Circle Crop"
# create window
screen = screeninfo.get_monitors()[0]
screenWidth = int(screen.width * 0.95)
screenHeight = int(screen.height * 0.90)
cv.namedWindow(windowName)
(primeRows, primeCols, _) = image.shape
# create the display-ready image
primeToDisplayScaler = max(primeRows / screenWidth, primeCols / screenHeight)
windowWidth = int(primeCols / primeToDisplayScaler)
windowHeight = int(primeRows / primeToDisplayScaler)
scaledImage = cv.resize(image, (windowWidth, windowHeight))
cv.moveWindow(windowName, screenWidth-windowWidth, 0)
# set up an image for analysis
primeToAnalyzerScaler = min(primeRows, primeCols) / houghAnalysisSize
analyzerWidth = int(primeCols / primeToAnalyzerScaler)
analyzerHeight = int(primeRows / primeToAnalyzerScaler)
analyzerImage = cv.resize(image, (analyzerWidth, analyzerHeight))
(_, analyzerImage) = cv.threshold(analyzerImage, 60, 255, cv.THRESH_BINARY)
analyzerImageGray = cv.cvtColor(analyzerImage, cv.COLOR_BGR2GRAY)
minRadius = 0
# find the circles
print('searching the image for circles...')
circles = cv.HoughCircles(analyzerImageGray, cv.HOUGH_GRADIENT, 1, 20, param1=50, param2=30, minRadius=minRadius, maxRadius=0)
displayToAnalyzerScaler = 1.0 * primeToAnalyzerScaler / primeToDisplayScaler
print("""=====================
Circle cropping interface:
PgUp: Select previous candidate circle
PgDn: Select next candidate circle
W: Increase radius by 1 pixel
S: Decrease radius by 1 pixel
Left: Move center point left 1 pixel
Up: Move center point up 1 pixel
Right: Move center point right 1 pixel
Down: Move center point down 1 pixel
Enter: Finalize crop and save image
Esc: Quit (without proceeding further)
""")
index = 0
# input loop
while True:
(centerX, centerY, radius) = circles[0][index]
centerX = int(centerX * displayToAnalyzerScaler)
centerY = int(centerY * displayToAnalyzerScaler)
radius = int(radius * displayToAnalyzerScaler)
displayImage = scaledImage.copy()
cv.circle(displayImage, (centerX, centerY), radius, (0, 0, 255), 2)
# show the update
cv.imshow(windowName, displayImage)
print('\r', end='')
print('circle # %d/%d @ (%d, %d), r = %d ' % (index+1, len(circles[0]), centerX, centerY, radius), end='', flush=True)
keyCode = key.translateKey(cv.waitKeyEx(0))
circle = circles[0][index]
if keyCode == key.ESC:
break
elif keyCode == key.ENTER:
break
elif keyCode == key.PGUP:
# select previous circle
if index > 0:
index -= 1
elif keyCode == key.PGDOWN:
# select next circle
if index < len(circles[0]):
index += 1
elif keyCode == 'W':
# increase radius
circle[2] += 1
elif keyCode == 'S':
# decrease radius
circle[2] -= 1
elif keyCode == key.LEFT_ARROW:
# move centerpoint left
circle[0] -= 1
elif keyCode == key.RIGHT_ARROW:
# move centerpoint right
circle[0] += 1
elif keyCode == key.DOWN_ARROW:
# move centerpoint down
circle[1] += 1
elif keyCode == key.UP_ARROW:
# move centerpoint up
circle[1] -= 1
cv.destroyWindow(windowName)
print()
if keyCode != key.ENTER:
return None
# create a new canvas with all white and 1 cell of padding beyond the radius
centerX = int(centerX * primeToDisplayScaler)
centerY = int(centerY * primeToDisplayScaler)
radius = int(radius * primeToDisplayScaler)
diameter = int(radius*2) + 1
croppedImage = np.zeros((diameter, diameter, 3), np.uint8)
cv.rectangle(croppedImage, (0, 0), (diameter, diameter), (255, 255, 255), thickness=-1)
# copy over the circle
print('performing final crop...')
croppedImage[radius][0:radius*2] = image[centerY][centerX-radius:centerX+radius]
for i in range(radius):
dx = int(np.sqrt(np.square(radius) - np.square(i)))
croppedImage[radius-i][radius-dx:radius+dx] = image[centerY-i][centerX-dx:centerX+dx]
croppedImage[radius+i][radius-dx:radius+dx] = image[centerY+i][centerX-dx:centerX+dx]
return croppedImage
def assistedRectangleCrop(image):
windowName = "MobyCAIRO - Rectangle Crop"
# create window
screen = screeninfo.get_monitors()[0]
screenWidth = int(screen.width * 0.95)
screenHeight = int(screen.height * 0.90)
cv.namedWindow(windowName)
(primeRows, primeCols, _) = image.shape
# create the display-ready image
primeToDisplayScaler = max(primeRows / screenWidth, primeCols / screenHeight)
windowWidth = int(primeCols / primeToDisplayScaler)
windowHeight = int(primeRows / primeToDisplayScaler)
scaledImage = cv.resize(image, (windowWidth, windowHeight))
cv.moveWindow(windowName, screenWidth-windowWidth, 0)
print('select a rectangular region to crop and press ENTER to save the cropped image')
(topX, topY, rectWidth, rectHeight) = cv.selectROI(windowName, scaledImage)
topX = int(topX * primeToDisplayScaler)
topY = int(topY * primeToDisplayScaler)
bottomX = int(topX + rectWidth * primeToDisplayScaler)
bottomY = int(topY + rectHeight * primeToDisplayScaler)
if rectWidth <= 0 or rectHeight <= 0:
return None
print('performing final crop...')
croppedImage = np.zeros((bottomY-topY, bottomX-topX, 3), np.uint8)
# copy each line
for i in range(topY, bottomY):
croppedImage[i-topY][:] = image[i][topX:bottomX]
return croppedImage