개정판 a15603c2
issue #1366: 45 symbol detection test
Change-Id: Ic867cce7c8c6b3855022821f83b32a1dd8650014
DTI_PID/DTI_PID/MainWindow.py | ||
---|---|---|
1803 | 1803 |
#cv2.fillPoly(mask,[poly],0) |
1804 | 1804 |
#src2 = np.multiply(mask,src2) |
1805 | 1805 |
|
1806 |
mask = np.ones((int(rect.height()), int(rect.width())), dtype=np.uint8) * 255
|
|
1806 |
mask = np.ones((int(rect.height()), int(rect.width())), dtype=np.uint8) * 255 |
|
1807 | 1807 |
cv2.fillConvexPoly(mask, poly, (0)) |
1808 | 1808 |
sym_img = cv2.bitwise_or(mask, symbol_image[int(rect.y()):int(rect.y()) + int(rect.height()), int(rect.x()):int(rect.x()) + int(rect.width())]) |
1809 | 1809 |
sym_img = cv2.merge((sym_img, sym_img, sym_img)) |
DTI_PID/DTI_PID/RecognitionDialog.py | ||
---|---|---|
2476 | 2476 |
|
2477 | 2477 |
@staticmethod |
2478 | 2478 |
def getCoordOnRotatedImage(angle, connPt, originImageWidth, originImageHeight): |
2479 |
import math |
|
2480 |
|
|
2479 | 2481 |
rx = None |
2480 | 2482 |
ry = None |
2481 | 2483 |
|
... | ... | |
2491 | 2493 |
direction = 'RIGHT' if angle == 90 else 'UP' if angle == 180 else 'LEFT' if angle == 270 else direction |
2492 | 2494 |
# up to here |
2493 | 2495 |
|
2496 |
''' |
|
2494 | 2497 |
transform = QTransform() |
2495 | 2498 |
if angle == 90 or angle == 270: |
2496 | 2499 |
transform.translate(originImageHeight * 0.5, originImageWidth * 0.5) |
... | ... | |
2502 | 2505 |
point = transform.map(point) |
2503 | 2506 |
rx = point.x() |
2504 | 2507 |
ry = point.y() |
2508 |
''' |
|
2509 |
|
|
2510 |
rect = QRectF(0, 0, originImageWidth, originImageHeight) |
|
2511 |
points = [rect.bottomLeft(), rect.topLeft(), rect.topRight(), rect.bottomRight()] |
|
2512 |
transform2 = QTransform() |
|
2513 |
transform2.rotate(abs(angle)) |
|
2514 |
points = [transform2.map(point) for point in points] |
|
2515 |
offset_x = abs(min([point.x() for point in points])) |
|
2516 |
offset_y = abs(min([point.y() for point in points])) |
|
2517 |
point = QPoint(connPt[1], connPt[2]) |
|
2518 |
point = transform2.map(point) |
|
2519 |
rx, ry = point.x() + offset_x, point.y() + offset_y |
|
2520 |
|
|
2521 |
''' |
|
2522 |
pX, pY = connPt[1], originImageHeight - connPt[2] |
|
2523 |
rad = math.radians(-angle) |
|
2524 |
rot_ce_X = originImageWidth * 0.5 |
|
2525 |
rot_ce_Y = originImageHeight * 0.5 |
|
2526 |
rX = (pX - rot_ce_X) * math.cos(rad) - (pY - rot_ce_Y) * math.sin(rad) + rot_ce_X |
|
2527 |
rY = (pX - rot_ce_X) * math.sin(rad) + (pY - rot_ce_Y) * math.cos(rad) + rot_ce_Y |
|
2528 |
rx = rX |
|
2529 |
ry = originImageHeight - rY |
|
2530 |
|
|
2531 |
if abs(point.x() - rx) > 0.1 or abs(point.y() - ry) > 0.1: |
|
2532 |
print('a') |
|
2533 |
''' |
|
2505 | 2534 |
|
2506 | 2535 |
symbol_idx = connPt[3] |
2507 | 2536 |
|
... | ... | |
2641 | 2670 |
import copy |
2642 | 2671 |
|
2643 | 2672 |
try: |
2673 |
tilt = False |
|
2674 |
|
|
2644 | 2675 |
symbolThreshold = symbol.getThreshold() |
2645 | 2676 |
symbolRotateCount = symbol.getRotationCount() |
2646 | 2677 |
detectFlip = symbol.getDetectFlip() |
... | ... | |
2660 | 2691 |
|
2661 | 2692 |
# get Rotated Original Point |
2662 | 2693 |
sow, soh = symGray.shape[::-1] |
2663 |
offset_x, offset_y = round(rect[3] * 0.3), round(rect[4] * 0.3)#int(max(sow, soh) / 2)
|
|
2694 |
offset_x, offset_y = round(rect[3] * 0.4), round(rect[4] * 0.4)#int(max(sow, soh) / 2)
|
|
2664 | 2695 |
|
2665 | 2696 |
roiItem = Worker.remove_small_objects(area.img, 0, sow * soh * 0.5) if hasInstrumentLabel else area.img.copy() |
2666 | 2697 |
x_start = round(rect[1]) - round(offset_x) if round(rect[1]) - round(offset_x) > 0 else 0 |
... | ... | |
2673 | 2704 |
|
2674 | 2705 |
searchedInfos = [] # score, x, y, angle, flip, originalPoint, connectionPoint, sw, sh |
2675 | 2706 |
|
2707 |
|
|
2676 | 2708 |
steps = [False, True] if detectFlip else [False] |
2677 | 2709 |
for flipped in steps: |
2678 | 2710 |
if flipped: |
2679 | 2711 |
symGray = symGrayOri |
2680 | 2712 |
symGray = cv2.flip(symGray, 1) |
2681 | 2713 |
|
2714 |
# 45 test |
|
2715 |
symGray_0 = copy.copy(symGray) |
|
2716 |
if tilt: |
|
2717 |
rect = QRectF(0, 0, symGray.shape[::-1][0], symGray.shape[::-1][1]) |
|
2718 |
points = [rect.bottomLeft(), rect.topLeft(), rect.topRight(), rect.bottomRight()] |
|
2719 |
transform2 = QTransform() |
|
2720 |
transform2.rotate(abs(45)) |
|
2721 |
points = [transform2.map(point) for point in points] |
|
2722 |
offset_x = int(max([point.x() for point in points]) - min([point.x() for point in points])) |
|
2723 |
offset_y = int(max([point.y() for point in points]) - min([point.y() for point in points])) |
|
2724 |
s_x, s_y = round((offset_x - symGray.shape[::-1][0]) / 2), round((offset_y - symGray.shape[::-1][1]) / 2) |
|
2725 |
mask = np.ones((offset_x, offset_y), dtype=np.uint8) * 255 |
|
2726 |
mask[s_y:s_y + symGray.shape[::-1][1], s_x:s_x + symGray.shape[::-1][0]] = symGray |
|
2727 |
|
|
2728 |
symGray_45 = mask |
|
2729 |
cX, cY = int(symGray_45.shape[::-1][0] / 2), int(symGray_45.shape[::-1][1] / 2) |
|
2730 |
M = cv2.getRotationMatrix2D((cX, cY), -45, 1.0) |
|
2731 |
symGray_45 = cv2.warpAffine(symGray_45, M, (symGray_45.shape[::-1][0], symGray_45.shape[::-1][1]), borderValue=(255)) |
|
2732 |
|
|
2682 | 2733 |
symbolRotatedAngle = 0 |
2683 |
#for rc in range((symbolRotateCount + 1) * 2): |
|
2684 |
for rc in range(symbolRotateCount + 1): |
|
2734 |
for rc in range((symbolRotateCount + 1) * 2): |
|
2735 |
#for rc in range(symbolRotateCount + 1): |
|
2736 |
if rc % 2 == 0: |
|
2737 |
symGray = symGray_0 |
|
2738 |
else: |
|
2739 |
if tilt: |
|
2740 |
symGray = symGray_45 |
|
2741 |
|
|
2685 | 2742 |
sw, sh = symGray.shape[::-1] |
2686 | 2743 |
|
2687 | 2744 |
originalPoint = Worker.getCalculatedOriginalPoint(additionalSymbol, symbolOriginalPoint, |
... | ... | |
2691 | 2748 |
|
2692 | 2749 |
r_w, r_h = roiItem.shape[::-1] |
2693 | 2750 |
if r_w < sw or r_h < sh: |
2694 |
symGray = cv2.rotate(symGray, cv2.ROTATE_90_CLOCKWISE) |
|
2695 |
symbolRotatedAngle = (symbolRotatedAngle + 90) % 360 |
|
2751 |
if rc % 2 == 0: |
|
2752 |
symGray_0 = cv2.rotate(symGray, cv2.ROTATE_90_CLOCKWISE) |
|
2753 |
else: |
|
2754 |
if tilt: |
|
2755 |
symGray_45 = cv2.rotate(symGray, cv2.ROTATE_90_CLOCKWISE) |
|
2756 |
#symGray = cv2.rotate(symGray, cv2.ROTATE_90_CLOCKWISE) |
|
2757 |
#symbolRotatedAngle = (symbolRotatedAngle + 90) % 360 |
|
2696 | 2758 |
#cX, cY = originalPoint[0], originalPoint[0] |
2697 | 2759 |
#M = cv2.getRotationMatrix2D((cX, cY), 45, 1.0) |
2698 | 2760 |
#symGray = cv2.warpAffine(symGray, M, (sw, sh)) |
2699 |
#symbolRotatedAngle = (symbolRotatedAngle + 45) % 360
|
|
2761 |
symbolRotatedAngle = (symbolRotatedAngle + 45) % 360 |
|
2700 | 2762 |
continue |
2701 | 2763 |
|
2702 | 2764 |
tmRes = cv2.matchTemplate(roiItem, symGray, cv2.TM_CCOEFF_NORMED) |
... | ... | |
2708 | 2770 |
if max_val > 0.1: |
2709 | 2771 |
searchedInfos.append([max_val, max_loc[0] + x_start, max_loc[1] + y_start, symbolRotatedAngle, flipped, originalPoint, connectionPoint, sw, sh]) |
2710 | 2772 |
|
2711 |
symGray = cv2.rotate(symGray, cv2.ROTATE_90_CLOCKWISE) |
|
2712 |
symbolRotatedAngle = (symbolRotatedAngle + 90) % 360 |
|
2773 |
if rc % 2 == 0: |
|
2774 |
symGray_0 = cv2.rotate(symGray, cv2.ROTATE_90_CLOCKWISE) |
|
2775 |
else: |
|
2776 |
if tilt: |
|
2777 |
symGray_45 = cv2.rotate(symGray, cv2.ROTATE_90_CLOCKWISE) |
|
2778 |
#symGray = cv2.rotate(symGray, cv2.ROTATE_90_CLOCKWISE) |
|
2779 |
#symbolRotatedAngle = (symbolRotatedAngle + 90) % 360 |
|
2713 | 2780 |
#cX, cY = originalPoint[0], originalPoint[0] |
2714 | 2781 |
#M = cv2.getRotationMatrix2D((cX, cY), 45, 1.0) |
2715 | 2782 |
#symGray = cv2.warpAffine(symGray, M, (sw, sh)) |
2716 |
#symbolRotatedAngle = (symbolRotatedAngle + 45) % 360
|
|
2783 |
symbolRotatedAngle = (symbolRotatedAngle + 45) % 360 |
|
2717 | 2784 |
|
2718 | 2785 |
if searchedInfos: |
2719 | 2786 |
searchedInfos = sorted(searchedInfos, key=lambda param: param[0], reverse=True) |
내보내기 Unified diff