saran
saran

Reputation: 29

Live camera to shape distance calculation based on computer vision

The goal is to live detect the walls and export the distacne to wall .There is a setup , A closed 4 wall , one set of unique & ideal shape in each wall ( Triangle , Square .....) A robot with camera will roam inside the walls and have computer vision. Robot should detect the shape and export the distance between camera and wall( or that shape ).

I have implemented this goal by Opencv and the shape detection ( cv2.approxPolyDP ) and distance calculation ( perimeter calculation and edge counting then conversion of pixel length to real distance ).

It perfectly works in 90 degree angle , but not effective when happening in other angles.

Any better way of doing it.

Thanks

for cnt in contours[1:]:
# considering countours from 1 because from practical experience whole frame is often considered as a contour
        area = cv2.contourArea(cnt)
        # area of detected contour
        approx = cv2.approxPolyDP(cnt, 0.02*cv2.arcLength(cnt, True), True)
        #It predicts and makes pixel connected contour to a shape
        x = approx.ravel()[0]
        y = approx.ravel()[1]
        # detected shape type label text placement 
        perimeter = cv2.arcLength(cnt,True)
        # find perimeter

Upvotes: 0

Views: 709

Answers (1)

ffletcherr
ffletcherr

Reputation: 65

in other degrees you have the perspective view of the shapes.

you must use Geometric Transformations to neutralize perspective effect (using a known-shape object or angle of the camera).

also consider that using rectified images is highly recommended Camera Calibration.


Edit:

lets assume you have a square on the wall. when camera capture an image from non-90-degree straight-on view of the object. the square is not align and looks out of shape, this causes measurement error.

but you can use cv2.getPerspectiveTransform() .the function calculates the 3x3 matrix of a perspective transform M.

after that use warped = cv2.warpPerspective(img, M, (w,h)) and apply perspective transformation to the image. now the square (in warped image) looks like 90-degree straight-on view and your current code works well on the output image (warped image).

and excuse me for bad explanation. maybe this blog posts can help you:

4 Point OpenCV getPerspective Transform Example

Find distance from camera to object/marker using Python and OpenCV

Upvotes: 1

Related Questions