55

If I have the polar coordinates of a line, how can I draw it on an image in OpenCV & python?

Line function takes 2 points, but draws only the segment. I want to draw a line from one edge of the image to other.

Rahul
  • 1,495
  • 1
  • 15
  • 25
  • Could you show us the code of how you have defined the line in polar coordinates? – jabaldonedo Sep 05 '13 at 09:21
  • I have theta and rho. So my line is x*cos(theta)-y*sin(theta) = rho. To be more precise, I am trying to implement Hough transform myself and want to visualize the lines from Hough space on my image. – Rahul Sep 05 '13 at 09:29
  • ellipse can do arcs, if that#s what you're looking for – berak Sep 05 '13 at 09:32
  • It's still a line, problem is mapping that line to pixels. Something that `Line` function does when you give it two points. I can calculate 2 points within the image bounds for a given equation, but I want it to extrapolate for the entire width/height of the image. – Rahul Sep 05 '13 at 09:34
  • 3
    Just calculate for 2 points outside. cv's Line is fine with e.g. (-10,-10) for a point – Robert Caspary Sep 05 '13 at 09:37
  • Awesome, works! Thanks @RobertCaspary. Do you want copy that as the answer for anyone else? – Rahul Sep 05 '13 at 09:57
  • Yes, its a quick and dirty solution ;-) – Robert Caspary Sep 05 '13 at 10:33
  • What I missed: `pip install opencv-python` for `cv2` – Martin Thoma May 01 '20 at 16:35

4 Answers4

82

Just calculate for 2 points outside. opencv's Line is fine with e.g. (-10,-10) for a point.

import cv2  # python-opencv
import numpy as np

width, height = 800, 600
x1, y1 = 0, 0
x2, y2 = 200, 400
image = np.ones((height, width)) * 255

line_thickness = 2
cv2.line(image, (x1, y1), (x2, y2), (0, 255, 0), thickness=line_thickness)

http://docs.opencv.org/2.4/modules/core/doc/drawing_functions.html#cv2.line

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
Robert Caspary
  • 1,584
  • 9
  • 7
20

Take a look to the following solution, I firstly convert a line in polar equations to cartesian and then I use numpy.vectorize() to generate a vector that allows me to get represent the line in any point of the space.

import cv2
import numpy as np

img_size = (200,200)
img = np.ones(img_size) * 255

# polar equation
theta = np.linspace(0, np.pi, 1000)
r = 1 / (np.sin(theta) - np.cos(theta))

# polar to cartesian
def polar2cart(r, theta):
    x = r * np.cos(theta)
    y = r * np.sin(theta)
    return x, y

x,y = polar2cart(r, theta)
x1, x2, y1, y2 = x[0], x[1], y[0], y[1]

# line equation y = f(X)
def line_eq(X):
    m = (y2 - y1) / (x2 - x1)
    return m * (X - x1) + y1

line = np.vectorize(line_eq)

x = np.arange(0, img_size[0])
y = line(x).astype(np.uint)

cv2.line(img, (x[0], y[0]), (x[-1], y[-1]), (0,0,0))
cv2.imshow("foo",img)
cv2.waitKey()

Result:

enter image description here

jabaldonedo
  • 25,822
  • 8
  • 77
  • 77
  • 2
    This looks good. But I don't think you need to vectorize and calculate for the entire range. You just need to calculate for x = 0 and x = img_size[0]. – Rahul Sep 05 '13 at 10:09
7

You can see how to do this in the Hough Line Transform tutorial.

import cv2
import numpy as np

img = cv2.imread('dave.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray,50,150,apertureSize = 3)

lines = cv2.HoughLines(edges,1,np.pi/180,200)
for rho,theta in lines[0]:
    a = np.cos(theta)
    b = np.sin(theta)
    x0 = a*rho
    y0 = b*rho
    x1 = int(x0 + 1000*(-b))
    y1 = int(y0 + 1000*(a))
    x2 = int(x0 - 1000*(-b))
    y2 = int(y0 - 1000*(a))

    cv2.line(img,(x1,y1),(x2,y2),(0,0,255),2)

cv2.imwrite('houghlines3.jpg',img)
James L.
  • 12,893
  • 4
  • 49
  • 60
0

This is one way to solve the problem of drawing infinite line segment in OpenCV with two given points.


### function to find slope 
 def slope(p1,p2):
    x1,y1=p1
    x2,y2=p2
    if x2!=x1:
        return((y2-y1)/(x2-x1))
    else:
        return 'NA'

### main function to draw lines between two points
def drawLine(image,p1,p2):
    x1,y1=p1
    x2,y2=p2
    ### finding slope
    m=slope(p1,p2)
    ### getting image shape
    h,w=image.shape[:2]

    if m!='NA':
        ### here we are essentially extending the line to x=0 and x=width
        ### and calculating the y associated with it
        ##starting point
        px=0
        py=-(x1-0)*m+y1
        ##ending point
        qx=w
        qy=-(x2-w)*m+y2
    else:
    ### if slope is zero, draw a line with x=x1 and y=0 and y=height
        px,py=x1,0
        qx,qy=x1,h
    cv2.line(image, (int(px), int(py)), (int(qx), int(qy)), (0, 255, 0), 2)
    return image

You can use p1 and p2 according to your requirement and call the function drawLine.

Sreekiran A R
  • 3,123
  • 2
  • 20
  • 41