|
@@ -0,0 +1,127 @@
|
|
|
+
|
|
|
+from scipy.spatial import distance as dist
|
|
|
+from imutils import perspective
|
|
|
+from imutils import contours
|
|
|
+import numpy as np
|
|
|
+import argparse
|
|
|
+import imutils
|
|
|
+import cv2
|
|
|
+import time
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+def midpoint(ptA, ptB):
|
|
|
+ return ((ptA[0] + ptB[0]) * 0.5, (ptA[1] + ptB[1]) * 0.5)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+image = cv2.imread("test3.jpg")
|
|
|
+
|
|
|
+bg_img = np.zeros((image.shape[0], image.shape[1] // 2, 3), np.uint8)
|
|
|
+
|
|
|
+
|
|
|
+length = int(2 / 2.54 * image.shape[1] / 2)
|
|
|
+
|
|
|
+
|
|
|
+cv2.rectangle(bg_img, (bg_img.shape[1] // 2 - length // 2, bg_img.shape[0] // 2 - length // 2),
|
|
|
+ (bg_img.shape[1] // 2 + length // 2, bg_img.shape[0] // 2 + length // 2), (255, 255, 255), 2)
|
|
|
+
|
|
|
+
|
|
|
+image = np.concatenate((bg_img, image), axis=1)
|
|
|
+cv2.imshow('Result', image)
|
|
|
+cv2.waitKey(0)
|
|
|
+
|
|
|
+
|
|
|
+gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
|
|
+
|
|
|
+gray = cv2.GaussianBlur(gray, (7, 7), 0)
|
|
|
+
|
|
|
+
|
|
|
+edged = cv2.Canny(gray, 50, 100)
|
|
|
+
|
|
|
+edged = cv2.dilate(edged, None, iterations=1)
|
|
|
+edged = cv2.erode(edged, None, iterations=1)
|
|
|
+
|
|
|
+
|
|
|
+cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL,
|
|
|
+ cv2.CHAIN_APPROX_SIMPLE)
|
|
|
+cnts = imutils.grab_contours(cnts)
|
|
|
+
|
|
|
+
|
|
|
+(cnts, _) = contours.sort_contours(cnts)
|
|
|
+
|
|
|
+pixelsPerMetric = None
|
|
|
+
|
|
|
+
|
|
|
+for c in cnts:
|
|
|
+
|
|
|
+ if cv2.contourArea(c) < 100:
|
|
|
+ continue
|
|
|
+
|
|
|
+
|
|
|
+ orig = image.copy()
|
|
|
+ box = cv2.minAreaRect(c)
|
|
|
+ box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box)
|
|
|
+ box = np.array(box, dtype="int")
|
|
|
+
|
|
|
+
|
|
|
+ box = perspective.order_points(box)
|
|
|
+ cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2)
|
|
|
+
|
|
|
+
|
|
|
+ for (x, y) in box:
|
|
|
+ cv2.circle(orig, (int(x), int(y)), 5, (0, 0, 255), -1)
|
|
|
+
|
|
|
+
|
|
|
+ (tl, tr, br, bl) = box
|
|
|
+ (tltrX, tltrY) = midpoint(tl, tr)
|
|
|
+ (blbrX, blbrY) = midpoint(bl, br)
|
|
|
+
|
|
|
+
|
|
|
+ (tlblX, tlblY) = midpoint(tl, bl)
|
|
|
+ (trbrX, trbrY) = midpoint(tr, br)
|
|
|
+
|
|
|
+
|
|
|
+ cv2.circle(orig, (int(tltrX), int(tltrY)), 5, (255, 0, 0), -1)
|
|
|
+ cv2.circle(orig, (int(blbrX), int(blbrY)), 5, (255, 0, 0), -1)
|
|
|
+ cv2.circle(orig, (int(tlblX), int(tlblY)), 5, (255, 0, 0), -1)
|
|
|
+ cv2.circle(orig, (int(trbrX), int(trbrY)), 5, (255, 0, 0), -1)
|
|
|
+
|
|
|
+
|
|
|
+ cv2.line(orig, (int(tltrX), int(tltrY)), (int(blbrX), int(blbrY)),
|
|
|
+ (255, 0, 255), 2)
|
|
|
+ cv2.line(orig, (int(tlblX), int(tlblY)), (int(trbrX), int(trbrY)),
|
|
|
+ (255, 0, 255), 2)
|
|
|
+
|
|
|
+
|
|
|
+ dA = dist.euclidean((tltrX, tltrY), (blbrX, blbrY))
|
|
|
+ dB = dist.euclidean((tlblX, tlblY), (trbrX, trbrY))
|
|
|
+
|
|
|
+
|
|
|
+ if pixelsPerMetric is None:
|
|
|
+ pixelsPerMetric = dB / (1.968503 *2.54)
|
|
|
+
|
|
|
+
|
|
|
+ dimA = dA / pixelsPerMetric
|
|
|
+ dimB = dB / pixelsPerMetric
|
|
|
+
|
|
|
+
|
|
|
+ cv2.putText(orig, "{:.1f}cm".format(dimB),
|
|
|
+ (int(tltrX - 15), int(tltrY - 10)), cv2.FONT_HERSHEY_SIMPLEX,
|
|
|
+ 0.65, (255, 255, 255), 2)
|
|
|
+ cv2.putText(orig, "{:.1f}cm".format(dimA),
|
|
|
+ (int(trbrX + 10), int(trbrY)), cv2.FONT_HERSHEY_SIMPLEX,
|
|
|
+ 0.65, (255, 255, 255), 2)
|
|
|
+ print('長:',"{:.1f}cm".format(dimB))
|
|
|
+ print('寬:',"{:.1f}cm".format(dimA))
|
|
|
+
|
|
|
+ now_time = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime(time.time()))
|
|
|
+ save_pic_name = now_time + '_' + '.jpg'
|
|
|
+ cv2.imwrite(save_pic_name, orig)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
|
|
|
+ cv2.resizeWindow("Image", 800, 600)
|
|
|
+ cv2.imshow("Image", orig)
|
|
|
+ cv2.waitKey(0)
|