개정판 8383d53e
issue #000: add drawing ex
Change-Id: Iabea8fe7c61a1995c090cfbdcd85eda0f7f67ad7
DTI_PID/DTI_PID/Drawing.py | ||
---|---|---|
136 | 136 |
import cv2 |
137 | 137 |
import numpy as np |
138 | 138 |
|
139 |
if self._image is None: |
|
140 |
app_doc_data = AppDocData.instance() |
|
141 |
|
|
142 |
numpyArray = np.asarray(bytearray(self.contents), dtype=np.uint8) |
|
143 |
image = cv2.imdecode(numpyArray, cv2.IMREAD_UNCHANGED) |
|
144 |
self._image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) |
|
145 |
|
|
146 |
configs = app_doc_data.getConfigs('Filter', 'Threshold') |
|
147 |
if configs: |
|
148 |
tokens = configs[0].value.split(',') |
|
149 |
if tokens[0] == 'OTSU': |
|
150 |
self._image = cv2.threshold(self._image, int(tokens[1]), 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1] |
|
151 |
elif tokens[0] == 'Binary': |
|
152 |
self._image = cv2.threshold(self._image, int(tokens[1]), 255, cv2.THRESH_BINARY)[1] |
|
153 |
else: |
|
154 |
self._image = cv2.threshold(self._image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1] |
|
155 |
|
|
156 |
configs = app_doc_data.getConfigs('Filter', 'Mode') |
|
157 |
if 1 == len(configs) and int(configs[0].value) is not 0: |
|
158 |
self._image = cv2.GaussianBlur(self._image, (1, 1), 0, 0) # gaussian blur |
|
159 |
|
|
160 |
""" |
|
161 |
configs = app_doc_data.getConfigs('Small Object Size', 'Min Area') |
|
162 |
min_area = int(configs[0].value) if 1 == len(configs) else 20 |
|
163 |
configs = app_doc_data.getConfigs('Small Object Size', 'Max Area') |
|
164 |
max_area = int(configs[0].value) if 1 == len(configs) else 50 |
|
165 |
|
|
166 |
selected_contours = [] |
|
167 |
contours, hierarchy = cv2.findContours(self._image, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) |
|
168 |
index = 0 |
|
169 |
for contour in contours: |
|
170 |
child, parent = hierarchy[0][index][2], hierarchy[0][index][3] |
|
171 |
if child == -1: |
|
172 |
area = cv2.contourArea(contour, False) |
|
173 |
if area < min_area: |
|
174 |
selected_contours.append(contour) |
|
175 |
index += 1 |
|
176 |
# draw contour with white color |
|
177 |
self._image = cv2.drawContours(self._image, selected_contours, -1, (255, 255, 255), -1) |
|
178 |
""" |
|
179 |
|
|
180 |
configs = app_doc_data.getConfigs('Filter', 'DilateSize') |
|
181 |
if 1 == len(configs) and int(configs[0].value) is not 0: |
|
182 |
size = int(configs[0].value) |
|
183 |
kernel = np.ones((size, size), np.uint8) |
|
184 |
self._image = cv2.erode(self._image, kernel, iterations=1) |
|
185 |
|
|
186 |
configs = app_doc_data.getConfigs('Filter', 'FlatSize') |
|
187 |
if 1 == len(configs) and int(configs[0].value) is not 0: |
|
188 |
size = int(configs[0].value) |
|
189 |
kernel = np.ones((size, size), np.uint8) |
|
190 |
self._image = cv2.morphologyEx(self._image, cv2.MORPH_CLOSE, kernel) |
|
191 |
self._image = cv2.morphologyEx(self._image, cv2.MORPH_OPEN, kernel) |
|
192 |
|
|
193 |
self._image_origin = self._image.copy() |
|
194 |
self.height, self.width = self._image.shape |
|
139 |
try: |
|
140 |
if self._image is None: |
|
141 |
app_doc_data = AppDocData.instance() |
|
142 |
|
|
143 |
numpyArray = np.asarray(bytearray(self.contents), dtype=np.uint8) |
|
144 |
image = cv2.imdecode(numpyArray, cv2.IMREAD_UNCHANGED) |
|
145 |
self._image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) |
|
146 |
|
|
147 |
configs = app_doc_data.getConfigs('Filter', 'Threshold') |
|
148 |
if configs: |
|
149 |
tokens = configs[0].value.split(',') |
|
150 |
if tokens[0] == 'OTSU': |
|
151 |
self._image = cv2.threshold(self._image, int(tokens[1]), 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1] |
|
152 |
elif tokens[0] == 'Binary': |
|
153 |
self._image = cv2.threshold(self._image, int(tokens[1]), 255, cv2.THRESH_BINARY)[1] |
|
154 |
else: |
|
155 |
self._image = cv2.threshold(self._image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1] |
|
156 |
|
|
157 |
configs = app_doc_data.getConfigs('Filter', 'Mode') |
|
158 |
if 1 == len(configs) and int(configs[0].value) is not 0: |
|
159 |
self._image = cv2.GaussianBlur(self._image, (1, 1), 0, 0) # gaussian blur |
|
160 |
|
|
161 |
""" |
|
162 |
configs = app_doc_data.getConfigs('Small Object Size', 'Min Area') |
|
163 |
min_area = int(configs[0].value) if 1 == len(configs) else 20 |
|
164 |
configs = app_doc_data.getConfigs('Small Object Size', 'Max Area') |
|
165 |
max_area = int(configs[0].value) if 1 == len(configs) else 50 |
|
166 |
|
|
167 |
selected_contours = [] |
|
168 |
contours, hierarchy = cv2.findContours(self._image, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) |
|
169 |
index = 0 |
|
170 |
for contour in contours: |
|
171 |
child, parent = hierarchy[0][index][2], hierarchy[0][index][3] |
|
172 |
if child == -1: |
|
173 |
area = cv2.contourArea(contour, False) |
|
174 |
if area < min_area: |
|
175 |
selected_contours.append(contour) |
|
176 |
index += 1 |
|
177 |
# draw contour with white color |
|
178 |
self._image = cv2.drawContours(self._image, selected_contours, -1, (255, 255, 255), -1) |
|
179 |
""" |
|
180 |
|
|
181 |
configs = app_doc_data.getConfigs('Filter', 'DilateSize') |
|
182 |
if 1 == len(configs) and int(configs[0].value) is not 0: |
|
183 |
size = int(configs[0].value) |
|
184 |
kernel = np.ones((size, size), np.uint8) |
|
185 |
self._image = cv2.erode(self._image, kernel, iterations=1) |
|
186 |
|
|
187 |
configs = app_doc_data.getConfigs('Filter', 'FlatSize') |
|
188 |
if 1 == len(configs) and int(configs[0].value) is not 0: |
|
189 |
size = int(configs[0].value) |
|
190 |
kernel = np.ones((size, size), np.uint8) |
|
191 |
self._image = cv2.morphologyEx(self._image, cv2.MORPH_CLOSE, kernel) |
|
192 |
self._image = cv2.morphologyEx(self._image, cv2.MORPH_OPEN, kernel) |
|
193 |
|
|
194 |
self._image_origin = self._image.copy() |
|
195 |
self.height, self.width = self._image.shape |
|
196 |
except Exception as ex: |
|
197 |
from App import App |
|
198 |
from AppDocData import MessageType |
|
199 |
|
|
200 |
message = 'error occurred({}) in {}:{}'.format(repr(ex), sys.exc_info()[-1].tb_frame.f_code.co_filename, |
|
201 |
sys.exc_info()[-1].tb_lineno) |
|
202 |
App.mainWnd().addMessage.emit(MessageType.Error, message) |
|
203 |
|
|
204 |
return None |
|
195 | 205 |
|
196 | 206 |
return self._image |
197 | 207 |
|
내보내기 Unified diff