How to get points from a line in OpenCV?
The cvLine()
function can draw a straight line given two points P1(x1,y1) and P2(x2,y2). What I'm stuck at is getting the points on this line instead of drawing it straight away.
Suppose I draw a line (in green) AB and another line AC. If I follow all the pixels on line AB there will be a point where I encounter black pixels (the border of the circle that encloses A) before I reach B.
Again when traveling along the pixels on line AC black pixels will be encountered twice.
Basically I'm trying to get the points on the (green) lines, but cvLine()
doesn't seem to return any point sequence structure. Is there any way to get these points using OpenCV?
A rather dumb approach would be to draw the line using cvLine()
on a separate image, then find contours on it, then traverse that contour's CvSeq*
(the line drawn) for the points. Both the scratch image and the original image being of same size we'd be getting the points' positi开发者_开发知识库ons. Like I said, kinda dumb. Any enlightened approach would be great!
I think a CvLinIterator does what you want.
Another dirty but efficient way to find the number of points of intersection between circles and line without iterating over all pixels of the line is as follows:
# First, create a single channel image having circles drawn on it.
CircleImage = np.zeros((Height, Width), dtype=np.uint8)
CircleImage = cv2.circle(CircleImage, Center, Radius, 255, 1) # 255-color, 1-thickness
# Then create an image of the same size with only the line drawn on it
LineImage = np.zeros((Height, Width), dtype=np.uint8)
LineImage = cv2.line(LineImage, PointA, PointB, 255, 1) # 255-color, 1-thickness
# Perform bitwise AND operation
IntersectionImage = cv2.bitwise_and(CircleImage, LineImage)
# Count number of white pixels now for the number of points of intersection.
Num = np.sum(IntersectionImage == 255)
This method is also fast as instead of iterating over pixels, it is using OpenCV and numpy libraries.
On adding another circle in the image "CircleImage", you can find the number of interaction points of both the circles and the line AC.
精彩评论