キャニー画像から長方形のオブジェクトを見つけます

maping-ID:

キャニーエッジ検出

これは、トラックコンテナ画像が、上面図です。まず、私は、長方形を見つけて、各コーナーの位置を知る必要があります。目標は、コンテナの大きさを知ることです。

nathancy:

ここでは単純なアプローチがあります:

  1. バイナリイメージを取得します。ロード画像、グレースケールに変換、ガウスぼかし、その後、大津のしきい値

  2. 歪んだ外接する四角形の輪郭を検索します。我々輪郭を見つけ、次に使用してフィルタcontour area長方形の輪郭を単離します。次は、私たちはして歪んだ外接する四角形を見つけるcv2.minAreaRectと、マスクブランク上にこれを描きます。

  3. コーナーを探します。我々はすでにとして実装市-Tomasiのコーナー検出器を使用するcv2.goodFeaturesToTrackコーナー検出のために。見てください、これを各パラメータの説明について。


検出された境界の矩形->マスク->検出コーナー

コーナーポイント

(188, 351)
(47, 348)
(194, 32)
(53, 29)

コード

import cv2
import numpy as np

# Load image, grayscale, blur, Otsu's threshold
image = cv2.imread('1.png')
mask = np.zeros(image.shape[:2], dtype=np.uint8)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5,5), 0)
thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]

# Find distorted bounding rect
cnts = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    area = cv2.contourArea(c)
    if area > 5000:
        # Find distorted bounding rect
        rect = cv2.minAreaRect(c)
        box = cv2.boxPoints(rect)
        box = np.int0(box)
        cv2.fillPoly(mask, [box], (255,255,255))

# Find corners
corners = cv2.goodFeaturesToTrack(mask,4,.8,100)
offset = 15
for corner in corners:
    x,y = corner.ravel()
    cv2.circle(image,(x,y),5,(36,255,12),-1)
    x, y = int(x), int(y)
    cv2.rectangle(image, (x - offset, y - offset), (x + offset, y + offset), (36,255,12), 3)
    print("({}, {})".format(x,y))

cv2.imshow('thresh', thresh)
cv2.imshow('image', image)
cv2.imshow('mask', mask)
cv2.waitKey()

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=285225&siteId=1