简体   繁体   中英

How to calculate distance from object to edge of image in OpenCV (Python 2.7)

I am currently trying to determine alignment to a rectangular shaped goal using OpenCV and Python on a Raspberry Pi. Since this is for a robotics-related event, I cannot use conventional methods of determining distance and angle as the robot would constantly be shifting and would not be facing the goal at a perfect angle, thus resulting in an inaccurate measurement. Instead I was wondering if it is possible to determine the length in pixels from the horizontal edges of the image and the edges of the rectangle. My logic for this would be if the distances of both sides to both edges are equal, then the bot is aligned. Any help would be appreciated. Also, any example code would also help greatly.

Thanks

Assume a rectangle is identified as contour / filled polygon .

You can make a quick pass 1d kernel (ie a line) and identify the intersects of the rectangle contour and the line. Here's a thread about this .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM