2017-01-04 7 views
0

輪郭を使ってエッジ検出画像のオブジェクト識別を手伝ってください。これは私のコードの一部ですが、私はいくつかの画像を分離することができますが、大きな詳細な画像では難しいです。どのように私はこれを変更することができますエッジ検出された画像のオブジェクト識別?

import numpy as np 
import cv2 

# load image 
img = cv2.imread('res/test6.jpg', 1) 

# convert the image to grayscale, blur it, and detect edges 
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 
gray = cv2.GaussianBlur(gray, (5, 5), 0) 
edged = cv2.Canny(gray, 35, 125) 
height, width = edged.shape 

# find contours of object 
ret, thresh = cv2.threshold(edged, 127, 255, 0) 
contours = cv2.findContours(thresh, 1, 2) 
cnts = contours[1] 
for cnt in cnts: 
    # find and draw a rectangle around object 
    x, y, w, h = cv2.boundingRect(cnt) 
    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2) 

    # line parameter 
    x1 = x + w/2 
    y1 = y + h 
    x2 = x + w/2 
    y2 = height 

    # mark pixel depth with arrow 
    cv2.arrowedLine(img, (x2, y2), (x1, y1), (255, 0, 0), 4) 
    distance = (y2 - y1) * 0.03 + 4 

    cv2.putText(img, str(distance) + "m", (x1 + 5, y1 + 50), cv2.FONT_HERSHEY_SIMPLEX, 0.5, 255) 

    print height, width 

cv2.imshow('image', img) 
cv2.waitKey(0) 
cv2.destroyAllWindows() 
import numpy as np 
import cv2 

# load image 
img = cv2.imread('res/test6.jpg', 1) 

# convert the image to grayscale, blur it, and detect edges 
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 
gray = cv2.GaussianBlur(gray, (5, 5), 0) 
edged = cv2.Canny(gray, 35, 125) 
height, width = edged.shape 

# find contours of object 
ret, thresh = cv2.threshold(edged, 127, 255, 0) 
contours = cv2.findContours(thresh, 1, 2) 
cnts = contours[1] 
for cnt in cnts: 
    # find and draw a rectangle around object 
    x, y, w, h = cv2.boundingRect(cnt) 
    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2) 

    # line parameter 
    x1 = x + w/2 
    y1 = y + h 
    x2 = x + w/2 
    y2 = height 

    # mark pixel depth with arrow 
    cv2.arrowedLine(img, (x2, y2), (x1, y1), (255, 0, 0), 4) 
    distance = (y2 - y1) * 0.03 + 4 

    cv2.putText(img, str(distance) + "m", (x1 + 5, y1 + 50), cv2.FONT_HERSHEY_SIMPLEX, 0.5, 255) 

    print height, width 

cv2.imshow('image', img) 
cv2.waitKey(0) 
cv2.destroyAllWindows() 

私が欲しいのは、特定のオブジェクトを制限することです。与えられたコードから img1 out1

+1

あなたのイメージを知らなくてもあなたを助けることができる人はどうすればいいですか?[質問]を読んでから質問を改善してください。 – Piglet

+0

いくつかの画像を追加しました –

+1

あなたはどの出力を期待し、あなたが提供したイメージはオーバーレイでオーバーロードされているため、問題を理解するのに役立ちません。 – Piglet

答えて

1

、私はあなたが輪郭を使用して、オブジェクトを特定したとします。次に、それらの輪郭を長方形で囲みました。

もう一度進んでください。今取得した輪郭を囲む矩形の重心を見つけてください。重心から画像の下端までの距離を測定します。

for c in cnts: 
    ------# compute the center of the contour 
    M = cv2.moments(c) 
    cX = int(M["m10"]/M["m00"]) 
    cY = int(M["m01"]/M["m00"]) 

    ------# draw the contour and center of the shape on the image 
    cv2.drawContours(image, [c], -1, (0, 255, 0), 2) 
    cv2.circle(image, (cX, cY), 7, (255, 255, 255), -1) 

    ------# show the image 
    cv2.imshow("Image", image) 

私は答えに私を助けたTHIS POSTに出くわしました。

ImageMomentsの詳細を知るには、WikipediaのTHIS ARTICLEをお勧めします。

+0

あなたの助けていただきありがとうございますが、私はこのエラーが発生しました。ファイル "D:/FYP/MyProject/Distance/MeasureDistance.py"、ライン46、 cX = int(M ["m10"]/M ["m00"] ) ZeroDivisionError:0で浮動小数点除算 –

+1

[このPOST]を参照してください(http://stackoverflow.com/questions/35247211/zerodivisionerror-python) –

関連する問題