辨識圖片中物體的最大長寬
Measuring distance between objects in an image

★範例所使用的圖檔下載位置

http://imgur.com/pSaQUS0

★程式範例執行

#以圖片最左物體最大寬為基準,量測其他物體最大長寬
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

def midpoint(ptA, ptB):
    return ((ptA[0] + ptB[0]) * 0.5, (ptA[1] + ptB[1]) * 0.5)


# 構造參數解析跟解析參數
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
    help="指定要開啟的圖檔 ")
ap.add_argument("-w", "--width", type=float, required=True,
    help="最左物體最大寬度 (英吋)")
args = vars(ap.parse_args())


# 讀取圖檔→灰階→模糊
# cv2.GaussianBlur模糊程度可以用3x3, 5x5, 7x7
image = cv2.imread(args["image"])
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 = cnts[0] if imutils.is_cv2() else cnts[1]

# 'pixels Per Metric' = object_width / know_width (相機像素 / 已知物品的寬度)
(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))

    # 如果未知 'pixelsPerMetric', 則使用下一行之算式
    if pixelsPerMetric is None:
        pixelsPerMetric = dB / args["width"]

    # 計算物品之最大長寬
    dimA = dA / pixelsPerMetric
    dimB = dB / pixelsPerMetric

    # 顯示出畫面中物品的大小
    cv2.putText(orig, "{:.1f}in".format(dimA),
        (int(tltrX - 15), int(tltrY - 10)), cv2.FONT_HERSHEY_SIMPLEX,
        0.65, (255, 255, 255), 2)
    cv2.putText(orig, "{:.1f}in".format(dimB),
        (int(trbrX + 10), int(trbrY)), cv2.FONT_HERSHEY_SIMPLEX,
        0.65, (255, 255, 255), 2)

    cv2.imshow("Image", orig)
    cv2.waitKey(0)     

◎輸入指令

★範例執行結果






★Numpy & Scipy 運用的主要數學計算

用numpy找出物體四個邊界的中心
用scipy進行長度換算,換算的方式是用座標法:



◎計算過程
1.以一開始輸入基準物(最左的物體)的實際寬度和用座標法在圖片中算出的寬度做比值
2.中心點分別為上下、左右,測量出圖片中兩線段長
3.把第二步驟求出的線段長帶入第一步驟求出的比值,算出待測物的最大長寬


Comments

comments powered by Disqus