개정판 036c0b65
Implementing issue #508: Flow Mark 인식
DTI_PID/DTI_PID/Shapes/QEngineeringLineItem.py | ||
---|---|---|
305 | 305 |
return res |
306 | 306 | |
307 | 307 |
''' |
308 |
@brief add flow arrow |
|
309 |
@author humkyung |
|
310 |
@date 2018.05.08 |
|
308 |
@brief add flow arrow
|
|
309 |
@author humkyung
|
|
310 |
@date 2018.05.08
|
|
311 | 311 |
@history 2018.05.24 Jeongwoo Modifying Draw Flow Arrow |
312 | 312 |
''' |
313 | 313 |
def addFlowArrow(self): |
314 |
from QEngineeringFlowArrowItem import QEngineeringFlowArrowItem |
|
315 |
from AppDocData import AppDocData |
|
316 | 314 |
import numpy as np |
317 | 315 |
import cv2 |
318 | 316 |
import math |
319 | 317 |
import sys |
320 |
global src |
|
318 |
from shapely.geometry import Point |
|
319 |
from QEngineeringFlowArrowItem import QEngineeringFlowArrowItem |
|
320 |
from AppDocData import AppDocData |
|
321 | 321 |
|
322 | 322 |
try: |
323 |
docData = AppDocData.instance() |
|
324 |
area = docData.getArea('Drawing') |
|
325 | ||
326 | 323 |
startPt = self.startPoint() |
327 | 324 |
endPt = self.endPoint() |
325 |
length = self.length() |
|
326 |
direction = [(endPt[0] - startPt[0]) / length, (endPt[1] - startPt[1]) / length] |
|
328 | 327 | |
329 | 328 |
left = min(startPt[0], endPt[0]) |
330 | 329 |
top = min(startPt[1], endPt[1]) |
... | ... | |
333 | 332 | |
334 | 333 |
rect = None |
335 | 334 |
if self.isVertical(): |
336 |
rect = QRectF(left - 10, top, (right - left) + 20, bottom)
|
|
335 |
rect = QRectF(left - 10, top, (right - left) + 20, (bottom - top))
|
|
337 | 336 |
else: |
338 | 337 |
rect = QRectF(left, top - 10, (right - left), (bottom - top) + 20) |
339 | 338 | |
340 | 339 |
docData = AppDocData.instance() |
341 | 340 |
area = docData.getArea('Drawing') |
342 |
img = area.img[int(rect.top()):int(rect.bottom()), int(rect.left()):int(rect.right())] |
|
341 |
img = np.array(AppDocData.instance().getCurrentPidSource().getPyImageOnRect(rect)) |
|
342 |
#img = area.img[int(rect.top()):int(rect.bottom()), int(rect.left()):int(rect.right())] |
|
343 |
|
|
343 | 344 |
imgLine = cv2.threshold(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), 127, 255, cv2.THRESH_BINARY)[1] |
344 |
# Remove Noise
|
|
345 |
kernel = np.ones((10, 10), np.uint8)
|
|
345 |
# remove noise
|
|
346 |
kernel = np.ones((5, 5), np.uint8)
|
|
346 | 347 |
imgLine = cv2.dilate(imgLine, kernel) |
347 | 348 |
imgLine = cv2.bitwise_not(imgLine) |
349 | ||
348 | 350 |
image, contours, hierarchy = cv2.findContours(imgLine, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) |
349 | 351 |
if contours: |
350 | 352 |
contours = sorted(contours, key=cv2.contourArea, reverse=True) |
351 | 353 |
[x, y, w, h] = cv2.boundingRect(contours[0]) |
352 |
if w > 10 and w < 100 and h > 10 and h < 100: |
|
353 |
item = QGraphicsBoundingBoxItem(rect.left() + x, rect.top() + y, w, h) |
|
354 |
if w > 10 and w < 100 and h > 10 and h < 100: # check arrow mark size |
|
355 |
imgArrowMark = imgLine[y:y+h, x:x+w] |
|
356 | ||
357 |
# DEBUG - display flow arrow area |
|
358 |
''' |
|
359 |
item = QGraphicsBoundingBoxItem(rect.left() + x - 10, rect.top() + y - 10, w + 20, h + 20) |
|
354 | 360 |
item.isSymbol = True |
355 | 361 |
item.angle = 0 |
356 | 362 |
item.setPen(QPen(Qt.red, 1, Qt.SolidLine)) |
357 | 363 |
item.setBrush(QBrush(QColor(255,255,0,100))) |
358 | 364 |
self.scene().addItem(item) |
359 | ||
360 | 365 |
''' |
361 |
edges = cv2.Canny(imgGray, 50, 150, apertureSize=3) |
|
366 |
# up to here |
|
367 | ||
368 |
edges = cv2.Canny(imgArrowMark, 50, 150, apertureSize=3) |
|
362 | 369 |
lines = cv2.HoughLinesP(edges, 1, np.pi/180, 10, minLineLength=10, maxLineGap=5) |
363 | 370 | |
364 |
# ####### HoughLinesP
|
|
371 |
####### HoughLinesP |
|
365 | 372 |
if lines is not None: |
366 | 373 |
for line in lines: |
367 | 374 |
for x1, y1, x2, y2 in line: |
... | ... | |
371 | 378 |
dx /= length |
372 | 379 |
dy /= length |
373 | 380 |
if (self.isVertical() and (dx < 0.001 or math.fabs(dx - 1) < 0.001)) or (self.isHorizontal() and (dx < 0.001 or math.fabs(dx - 1) < 0.001)): continue |
374 |
cv2.line(img,(x1,y1),(x2,y2),(0,0,255),2) |
|
375 |
''' |
|
376 | ||
377 |
cv2.imshow('img', imgLine) |
|
378 |
cv2.waitKey(0) |
|
379 |
cv2.destroyAllWindows() |
|
381 |
dist1 = self.distanceTo((rect.left() + x + x1, rect.top() + y + y1)) |
|
382 |
dist2 = self.distanceTo((rect.left() + x + x2, rect.top() + y + y2)) |
|
383 |
if dist1 > dist2: # point which's distance is longer would be start point |
|
384 |
_start = (rect.left() + x + x1, rect.top() + y + y1) |
|
385 |
_end = (rect.left() + x + x2, rect.top() + y + y2) |
|
386 |
else: |
|
387 |
_start = (rect.left() + x + x2, rect.top() + y + y2) |
|
388 |
_end = (rect.left() + x + x1, rect.top() + y + y1) |
|
389 | ||
390 |
dist1 = Point(startPt[0], startPt[1]).distance(Point(_start[0], _start[1])) |
|
391 |
dist2 = Point(startPt[0], startPt[1]).distance(Point(_end[0], _end[1])) |
|
392 |
if dist1 > dist2: |
|
393 |
startPt,endPt = endPt,startPt |
|
394 |
direction[0],direction[1] = -direction[0],-direction[1] |
|
380 | 395 |
except Exception as ex: |
381 | 396 |
print('error occured({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno)) |
382 | 397 | |
383 | 398 |
center = [(startPt[0]+endPt[0])*0.5, (startPt[1]+endPt[1])*0.5] |
384 |
length = self.length() |
|
385 |
dx = (endPt[0] - startPt[0]) / length |
|
386 |
dy = (endPt[1] - startPt[1]) / length |
|
387 | ||
388 |
arrow = QEngineeringFlowArrowItem(center, (dx,dy)) |
|
399 |
arrow = QEngineeringFlowArrowItem(center, direction) |
|
389 | 400 |
arrow.buildItem() |
390 | 401 |
self.scene().addItem(arrow) |
391 | 402 |
내보내기 Unified diff