Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

Drawing a Line on Image

1. Importing Packages.
In [1]: import
numpy
as
np

import
matplotlib.pyplot
as
plt

import
cv2


2. Generating Black Intensity Image.


In [2]: img
=
np.ones((512,512))

img
=
img.astype('uint8')

cv2.imshow('img',img)

cv2.waitKey(0)


13
Out[2]:

Note꞉ After the excecution of the previous cell following steps has to be performed before the execution of next cell.
1. Left click on the opencv2 image window.
2. Press "enter" key.
In [3]: cv2.destroyAllWindows()


In [4]: plt.imshow(img)

plt.xlabel('x')

plt.ylabel('y')


Text(0,
0.5,
'y')
Out[4]:

3. Drawing Line on the image.


"cv2.line (image, (x1,y1),(x2,y2),(B,G,R),px)"
image꞉ Input image on which the line should be drawn.
(x1,y1)꞉ Starting point of the line.
(x2,y2)꞉ End point of the line.
(B,G,R)꞉ Color of the line (Blue, Green, and Red).
px꞉ Lenght of the line
In [5]: line_img
=
img

x1
=
0;
y1
=
512;
x2
=
512;
y2
=
0;
#
note
"x"
is
horizonatal
direction,
and
"y"
is
in
vertical
direction

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


array([[

1,


1,


1,
...,


1,
255,
255],

Out[5]:







[

1,


1,


1,
...,
255,
255,
255],








[

1,


1,


1,
...,
255,
255,
255],








...,








[

1,
255,
255,
...,


1,


1,


1],








[255,
255,
255,
...,


1,


1,


1],








[255,
255,
255,
...,


1,


1,


1]],
dtype=uint8)

In [6]: cv2.imshow('Image
With
Line',line_img)

cv2.waitKey(0)


13
Out[6]:

Note꞉ After the excecution of the previous cell following steps has to be performed before the execution of next cell.
1. Left click on the opencv2 image window.
2. Press "enter" key.
In [7]: cv2.destroyAllWindows()


In [8]: plt.imshow(img)

plt.xlabel('x')

plt.ylabel('y')


Text(0,
0.5,
'y')
Out[8]:

In [ ]: 



You might also like