-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMorphologyFinder.py
193 lines (162 loc) · 7.22 KB
/
MorphologyFinder.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
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
#Author: Stan Yin
#GitHub Name: SomeB1oody
#This project is based on CC 4.0 BY, please mention my name if you use it.
#This project requires opencv and wx.
import cv2
import wx
def convert_to_double(str1, str2):
# 初始化返回的值
double1, double2 = None, None
error_message = None
success = True
# 转换第一个字符串
try:
if str1 != "": # 空字符串跳过转换,直接赋值 None
double1 = float(str1)
if double1 < 0: # 检查是否为负数
success = False
error_message = f"Error: First input is negative: {str1}"
# 空字符串情况算作成功转换
except ValueError:
success = False
error_message = f"Error: First input is not a valid number: {str1}"
# 转换第二个字符串
try:
if str2 != "": # 空字符串跳过转换,直接赋值 None
double2 = float(str2)
if double2 < 0: # 检查是否为负数
success = False
error_message = f"Error: Second input is negative: {str2}"
except ValueError:
success = False
error_message = f"Error: Second input is not a valid number: {str2}"
return success, double1, double2, error_message
def detect_rectangles(img, min_area, aspect_ratio_range_min, aspect_ratio_range_max):
# 1. 转换为灰度图像
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 2. 高斯模糊去噪
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
# 3. Canny 边缘检测
edged = cv2.Canny(blurred, 50, 150)
# 4. 形态学膨胀操作
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
dilated = cv2.dilate(edged, kernel, iterations=2)
# 5. 找到轮廓
contours, _ = cv2.findContours(dilated.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# 6. 遍历所有轮廓,筛选矩形
rectangles = []
for contour in contours:
# 使用多边形逼近轮廓
epsilon = 0.02 * cv2.arcLength(contour, True)
approx = cv2.approxPolyDP(contour, epsilon, True)
# 如果多边形有 4 个顶点,并且是凸的,则认为是矩形
if len(approx) == 4 and cv2.isContourConvex(approx):
# 计算矩形的面积
area = cv2.contourArea(approx)
if min_area:
if area < min_area:
continue # 忽略面积太小的矩形
# 计算矩形的宽高比
x, y, w, h = cv2.boundingRect(approx)
aspect_ratio = w / float(h)
if aspect_ratio_range_min or aspect_ratio_range_max:
if aspect_ratio_range_min and aspect_ratio_range_max:
if aspect_ratio_range_min <= aspect_ratio <= aspect_ratio_range_max:
rectangles.append(approx)
elif aspect_ratio_range_min:
if aspect_ratio_range_min <= aspect_ratio:
rectangles.append(approx)
elif aspect_ratio_range_max:
if aspect_ratio <= aspect_ratio_range_max:
rectangles.append(approx)
# 7. 在图像上绘制矩形
img_with_rectangles = img.copy()
for rect in rectangles:
cv2.drawContours(img_with_rectangles, [rect], -1, (0, 255, 0), 3)
return img_with_rectangles
class MorphologyRectangle(wx.Frame):
def __init__(self, *args, **kw):
super(MorphologyRectangle, self).__init__(*args, **kw)
panel = wx.Panel(self)
self.vbox = wx.BoxSizer(wx.VERTICAL)
# 输入路径
self.hbox = wx.BoxSizer(wx.HORIZONTAL)
self.file_button = wx.Button(panel, label="Select image")
self.Bind(wx.EVT_BUTTON, self.on_select_file, self.file_button)
self.hbox.Add(self.file_button,flag=wx.ALL, border=5)
self.input_path_text = wx.StaticText(panel, label="Click \"Select image\" first")
self.hbox.Add(self.input_path_text, flag=wx.ALL, border=5)
self.vbox.Add(self.hbox, flag=wx.EXPAND)
# 最小面积
self.vbox.Add(wx.StaticText(panel, label=
"Specify the minimum area(pixel^2) to look for(not specified if left blank):"
), flag=wx.ALL, border=5)
self.vbox.Add(wx.StaticText(panel, label="Digit only"), flag=wx.ALL, border=5)
self.minimum_area = wx.TextCtrl(panel)
self.vbox.Add(self.minimum_area, flag=wx.ALL, border=5)
# 长宽比
self.vbox.Add(wx.StaticText(panel, label=
"Specify the aspect ratio range to look for(not specified if left blank)"
), flag=wx.ALL, border=5)
self.vbox.Add(wx.StaticText(panel, label=
"Digit only Example: 0.9 ~ 1.1"
), flag=wx.ALL, border=5)
self.hbox = wx.BoxSizer(wx.HORIZONTAL)
self.range_min = wx.TextCtrl(panel)
self.range_max = wx.TextCtrl(panel)
self.hbox.Add(self.range_min, flag=wx.ALL, border=2)
self.hbox.Add(wx.StaticText(panel, label="~"), flag=wx.ALL, border=5)
self.hbox.Add(self.range_max, flag=wx.ALL, border=2)
self.vbox.Add(self.hbox, flag=wx.ALL, border=5)
# 查找按钮
self.find_button = wx.Button(panel, label="Find")
self.find_button.Bind(wx.EVT_BUTTON, self.on_find_button)
self.vbox.Add(self.find_button, flag=wx.ALL, border=5)
# 设置面板的布局管理器
panel.SetSizer(self.vbox)
panel.Layout()
def on_select_file(self, event):
with wx.FileDialog(None, "Select a image", wildcard="所有文件 (*.*)|*.*",
style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST) as dialog:
if dialog.ShowModal() == wx.ID_OK:
self.input_path_text.SetLabel(f"{dialog.GetPath()}")
self.selected_file = dialog.GetPath()
def on_find_button(self, event):
#获取输入
path = self.selected_file
min_area = self.minimum_area.GetValue()
rmin = self.range_min.GetValue()
rmax = self.range_max.GetValue()
if path:
img = cv2.imread(path, cv2.IMREAD_COLOR)
if img is None:
wx.MessageBox("Fail to load image", "Error", wx.OK, wx.ICON_ERROR)
return
else:
wx.MessageBox("Please enter input path first", 'Error', wx.OK | wx.ICON_ERROR)
return
if min_area:
if min_area.isdigit():
min_area = int(min_area)
if min_area == 0:
wx.MessageBox(
"input should be a nonnegative integer", "Error", wx.OK | wx.ICON_ERROR)
return
else:
wx.MessageBox(
"input should be a nonnegative integer", "Error", wx.OK | wx.ICON_ERROR)
return
flag, range_min, range_max, error_message = convert_to_double(rmin, rmax)
if not flag:
wx.MessageBox(error_message, "Error", wx.OK | wx.ICON_ERROR)
return
img_with_rectangles = detect_rectangles(img, min_area, range_min, range_max)
cv2.imshow("Morphology", img_with_rectangles)
cv2.waitKey(0)
if __name__ == "__main__":
app = wx.App()
frame = MorphologyRectangle(None)
frame.SetTitle('Morphology Transformer')
frame.SetSize((500, 325))
frame.Show()
app.MainLoop()