개정판 c84bdfd3
Commit 2018.04.10
DTI_PID/DTI_PID/App.py | ||
---|---|---|
177 | 177 |
detector = LineDetector(area.img) |
178 | 178 |
for item in self.graphicsView.scene.items(): |
179 | 179 |
if (type(item) is QGraphicsBoundingBoxItem) and item.isSymbol: |
180 |
res = detector.Detect(item, int(area.x), int(area.y))
|
|
180 |
res = detector.detectConnectedLine(item, int(area.x), int(area.y))
|
|
181 | 181 |
for pts in res: |
182 | 182 |
line = QEngineeringLineItem() |
183 | 183 |
line._vertices.append(QPointF(pts[0][0] + int(area.x), pts[0][1] + int(area.y))) |
DTI_PID/DTI_PID/LineDetector.py | ||
---|---|---|
133 | 133 |
|
134 | 134 |
class LineDetector(): |
135 | 135 |
def __init__(self, image): |
136 |
thresh = 127 |
|
137 |
self.image = cv2.threshold(image, thresh, 255, cv2.THRESH_BINARY)[1] |
|
138 |
self.width, self.height = self.image.shape[::-1] |
|
139 |
self.Result = np.zeros((self.width, self.height, 3), np.uint8) |
|
136 |
try: |
|
137 |
thresh = 127 |
|
138 |
self.image = cv2.threshold(image, thresh, 255, cv2.THRESH_BINARY)[1] |
|
139 |
self.width, self.height = self.image.shape[::-1] |
|
140 |
self.Result = np.zeros((self.width, self.height, 3), np.uint8) |
|
141 |
except Exception as ex: |
|
142 |
print('error occured({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno)) |
|
140 | 143 |
|
141 | 144 |
''' |
142 |
@brief symbol을 기준으로 양쪽으로 이미지에서 직선을 검출한다. |
|
145 |
@brief symbol을 기준으로 양쪽으로 이미지에서 직선을 검출한다.
|
|
143 | 146 |
@author humkyung |
144 | 147 |
@date 2018.04.?? |
145 | 148 |
''' |
146 |
def Detect(self, symbol, offsetX, offsetY):
|
|
149 |
def detectConnectedLine(self, symbol, offsetX, offsetY):
|
|
147 | 150 |
res = [] |
148 | 151 |
|
149 | 152 |
try: |
... | ... | |
152 | 155 |
right = int(symbol.rect().right()) |
153 | 156 |
left = int(symbol.rect().left()) |
154 | 157 |
|
155 |
pt = [right - offsetX, int(symbol.center.y()) - offsetY]
|
|
158 |
pt = [right - int(offsetX), int(symbol.center.y()) - int(offsetY)]
|
|
156 | 159 |
pool.append([[1,0], pt]) |
157 |
pt = [left - offsetX, int(symbol.center.y()) - offsetY]
|
|
160 |
pt = [left - int(offsetX), int(symbol.center.y()) - int(offsetY)]
|
|
158 | 161 |
pool.append([[-1,0], pt]) |
159 | 162 |
elif (1.57 == symbol.angle) or (4.71 == symbol.angle): # rotated by 90 or 270 degree |
160 | 163 |
bottom = int(symbol.rect().bottom()) |
161 | 164 |
top = int(symbol.rect().top()) |
162 | 165 |
|
163 |
pt = [int(symbol.center.x()) - offsetX, bottom - offsetY]
|
|
166 |
pt = [int(symbol.center.x()) - int(offsetX), bottom - int(offsetY)]
|
|
164 | 167 |
pool.append([[0,1], pt]) |
165 |
pt = [int(symbol.center.x()) - offsetX, top - offsetY]
|
|
168 |
pt = [int(symbol.center.x()) - int(offsetX), top - int(offsetY)]
|
|
166 | 169 |
pool.append([[0,-1], pt]) |
167 | 170 |
|
168 | 171 |
while len(pool) > 0: |
... | ... | |
191 | 194 |
def detectLine(self, pt, dir): |
192 | 195 |
from AppDocData import AppDocData |
193 | 196 |
|
194 |
white = [255] |
|
195 |
windowSize = AppDocData.instance().getSlidingWindowSize() |
|
196 |
xHalf = int(windowSize[0]*0.5) |
|
197 |
yHalf = int(windowSize[1]*0.5) |
|
198 |
|
|
199 |
if ([1,0] == dir): |
|
200 |
image = self.image[(pt[1]-yHalf):(pt[1]+yHalf), pt[0]:self.width] |
|
201 |
imgWidth, imgHeight = image.shape[::-1] |
|
202 |
for i in range(imgWidth-windowSize[0]): |
|
203 |
window = image[0:windowSize[1], i:i+windowSize[0]] |
|
204 |
if (white == window[0:windowSize[1],0:windowSize[0]]).all(): break |
|
205 |
if i > windowSize[0]: |
|
206 |
self.image[(pt[1]-yHalf):(pt[1]+yHalf), pt[0]:(pt[0]+i)] = white |
|
207 |
return [(pt[0], pt[1]), (pt[0] + i, pt[1])] |
|
208 |
elif ([-1,0] == dir): |
|
209 |
image = self.image[(pt[1]-yHalf):(pt[1]+yHalf), 0:pt[0]] |
|
210 |
imgWidth, imgHeight = image.shape[::-1] |
|
211 |
for i in range(imgWidth-windowSize[0], -1, -1): |
|
212 |
window = image[0:windowSize[1], i:i+windowSize[0]] |
|
213 |
if (white == window[0:windowSize[1],0:windowSize[0]]).all(): break |
|
214 |
if abs(pt[0] - i) > windowSize[0]: |
|
215 |
self.image[int(pt[1]-yHalf):int(pt[1]+yHalf), (i+windowSize[0]):pt[0]] = white |
|
216 |
return [(pt[0], pt[1]), (i+windowSize[0], pt[1])] |
|
217 |
elif ([0,1] == dir): |
|
218 |
windowSize.reverse() |
|
219 |
xHalf = int(windowSize[0]*0.5) |
|
220 |
yHalf = int(windowSize[1]*0.5) |
|
221 |
|
|
222 |
image = self.image[pt[1]:self.height, int(pt[0]-xHalf):int(pt[0]+xHalf)] |
|
223 |
imgWidth, imgHeight = image.shape[::-1] |
|
224 |
for i in range(imgHeight-windowSize[1]): |
|
225 |
window = image[i:i+windowSize[1], 0:windowSize[0]] |
|
226 |
if (white == window[0:windowSize[1],0:windowSize[0]]).all(): break |
|
227 |
if i > windowSize[1]: |
|
228 |
self.image[(pt[1]):(pt[1]+i), (pt[0]-xHalf):(pt[0]+xHalf)] = white |
|
229 |
return [(pt[0], pt[1]), (pt[0], pt[1] + i)] |
|
230 |
elif ([0,-1] == dir): |
|
231 |
windowSize.reverse() |
|
197 |
try: |
|
198 |
white = [255] |
|
199 |
windowSize = AppDocData.instance().getSlidingWindowSize() |
|
232 | 200 |
xHalf = int(windowSize[0]*0.5) |
233 | 201 |
yHalf = int(windowSize[1]*0.5) |
234 | 202 |
|
235 |
image = self.image[0:pt[1], int(pt[0]-xHalf):int(pt[0]+xHalf)] |
|
236 |
imgWidth, imgHeight = image.shape[::-1] |
|
237 |
for i in range(imgHeight-windowSize[1], -1, -1): |
|
238 |
window = image[i:i+windowSize[1], 0:windowSize[0]] |
|
239 |
if (white == window[0:windowSize[1],0:windowSize[0]]).all(): break |
|
240 |
if abs(pt[1] - i) > windowSize[1]: |
|
241 |
self.image[(i+windowSize[1]):pt[1], (pt[0]-xHalf):(pt[0]+xHalf)] = white |
|
242 |
return [(pt[0], pt[1]), (pt[0], i+windowSize[1])] |
|
203 |
if ([1,0] == dir): |
|
204 |
image = self.image[(pt[1]-yHalf):(pt[1]+yHalf), pt[0]:self.width] |
|
205 |
imgWidth, imgHeight = image.shape[::-1] |
|
206 |
for i in range(imgWidth-windowSize[0]): |
|
207 |
window = image[0:windowSize[1], i:i+windowSize[0]] |
|
208 |
if (white == window[0:windowSize[1],0:windowSize[0]]): break |
|
209 |
if i > windowSize[0]: |
|
210 |
self.image[(pt[1]-yHalf):(pt[1]+yHalf), pt[0]:(pt[0]+i)] = white |
|
211 |
return [(pt[0], pt[1]), (pt[0] + i, pt[1])] |
|
212 |
elif ([-1,0] == dir): |
|
213 |
image = self.image[(pt[1]-yHalf):(pt[1]+yHalf), 0:pt[0]] |
|
214 |
imgWidth, imgHeight = image.shape[::-1] |
|
215 |
for i in range(imgWidth-windowSize[0], -1, -1): |
|
216 |
window = image[0:windowSize[1], i:i+windowSize[0]] |
|
217 |
if (white == window[0:windowSize[1],0:windowSize[0]]): break |
|
218 |
if abs(pt[0] - i) > windowSize[0]: |
|
219 |
self.image[int(pt[1]-yHalf):int(pt[1]+yHalf), (i+windowSize[0]):pt[0]] = white |
|
220 |
return [(pt[0], pt[1]), (i+windowSize[0], pt[1])] |
|
221 |
elif ([0,1] == dir): |
|
222 |
windowSize.reverse() |
|
223 |
xHalf = int(windowSize[0]*0.5) |
|
224 |
yHalf = int(windowSize[1]*0.5) |
|
225 |
|
|
226 |
image = self.image[pt[1]:self.height, int(pt[0]-xHalf):int(pt[0]+xHalf)] |
|
227 |
imgWidth, imgHeight = image.shape[::-1] |
|
228 |
for i in range(imgHeight-windowSize[1]): |
|
229 |
window = image[i:i+windowSize[1], 0:windowSize[0]] |
|
230 |
if (white == window[0:windowSize[1],0:windowSize[0]]): break |
|
231 |
if i > windowSize[1]: |
|
232 |
self.image[(pt[1]):(pt[1]+i), (pt[0]-xHalf):(pt[0]+xHalf)] = white |
|
233 |
return [(pt[0], pt[1]), (pt[0], pt[1] + i)] |
|
234 |
elif ([0,-1] == dir): |
|
235 |
windowSize.reverse() |
|
236 |
xHalf = int(windowSize[0]*0.5) |
|
237 |
yHalf = int(windowSize[1]*0.5) |
|
238 |
|
|
239 |
image = self.image[0:pt[1], int(pt[0]-xHalf):int(pt[0]+xHalf)] |
|
240 |
imgWidth, imgHeight = image.shape[::-1] |
|
241 |
for i in range(imgHeight-windowSize[1], -1, -1): |
|
242 |
window = image[i:i+windowSize[1], 0:windowSize[0]] |
|
243 |
if (white == window[0:windowSize[1],0:windowSize[0]]): break |
|
244 |
if abs(pt[1] - i) > windowSize[1]: |
|
245 |
self.image[(i+windowSize[1]):pt[1], (pt[0]-xHalf):(pt[0]+xHalf)] = white |
|
246 |
return [(pt[0], pt[1]), (pt[0], i+windowSize[1])] |
|
247 |
except Exception as ex: |
|
248 |
print('error occured({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno)) |
|
243 | 249 |
|
244 | 250 |
return None |
245 | 251 |
|
내보내기 Unified diff