Drawing With OpenCV

We have discussed that an image is nothing but a logical array with number in each pixel representing the intensity of light in each pixel . And we can think it as numpy array and to work with it we have opencv library.

 what if we wanted to draw a single line? Or a circle? NumPy does not provide that type of functionality — it’s only a numerical processing library, after all!

OpenCV provides convenient, easy-to-use methods to draw shapes on an image. In this lesson, we’ll review the three most basic methods to draw shapes: cv2.line , cv2.rectangle , and cv2.circle .

The main objective of this post is to become familiar with the cv2.line , cv2.rectangle , and cv2.circle  functions.

Lines+Rectangles

Before drawing any shape let’s quickly understand upon what we are drawing our work.

Up until this point, we have only loaded images from disk. However, we can also define our images manually using NumPy arrays which we will do now. Given that OpenCV interprets an image as a NumPy array, there is no reason why we can’t manually define the image.

let’s open your notebook and make a python file named shape.py

our code should look like this don’t ignore if you are a novice in vision

We will now explain our code and what we are doing today .

We construct a NumPy array using the np.zeros  method with 300 rows and 300 columns, yielding a 300 x 300 pixel image. We also allocate space for 3 channels — one for Red, Green, and Blue, respectively. As the name suggests, the zeros method fills every element in the array with an initial value of zero.

It’s important to draw your attention to the second argument of the np.zeros  method: the data type, dtype . Since we are representing our image as a RGB image with pixels in the range [0, 255], it’s important that we use an 8-bit unsigned integer, or uint8 . There are many other data types that we can use (common ones include 32-bit integers, and 32-bit, and 64-bit floats) .

Now that we have our canvas initialized, we can do some drawing:

The first thing we do on  is define a tuple used to represent the color “green.” Then, we draw a green line from point (0, 0), the top-left corner of the image, to point (300, 300), the bottom-right corner of the image.

In order to draw the line, we make use of the cv2.line  method. The first argument to this method is the image we are going to draw on. In this case, it’s our canvas . The second argument is the starting point of the line. We choose to start our line from the top-left corner of the image, at point (0, 0) — again, remember that the Python language is zero-index. We also need to supply an ending point for the line (the third argument). We define our ending point to be (300, 300), the bottom-right corner of the image. The last argument is the color of our line: in this case, green. Lines show our image and then wait for a keypress .

As you can see, using the cv2.line  function is quite simple! But there is one other important argument to consider in the cv2.line  method: the thickness.

On Lines above we define the color red as a tuple (again, in BGR rather than RGB format). We then draw a red line from the top-right corner of the image to the bottom left. The last parameter to the method controls the thickness of the line — we decide to make the thickness 3 pixels. Again, we show our image and wait for a keypress.

Drawing a line was simple enough. Now we can move on to drawing rectangles given in code.

The output of drawing our lines and rectangles can be seen below:

As you can see, the output matches our code. We were able to draw a green line from the top-left corner to the bottom-right corner, followed by a thicker red line from the top-right corner to the bottom-left corner.

We were also able to draw a green rectangle, a slightly thicker red rectangle, and a completely filled in blue rectangle.

That’s really all there is to it! You now have a solid grasp of drawing rectangles. In the next section, we’ll move on to drawing circles.

Circles

Drawing circles is just as simple as drawing rectangles, but the function arguments are a little different .

We have to calculate two variables: centerX  and centerY . These two variables represent the (x, y) coordinates of the center of the image. We calculate the center by examining the shape of our NumPy array, and then dividing by two. The height of the image can be found in canvas.shape[0]  and the width in canvas.shape[1] 

we loop over a number of radius values, starting from 0 and ending at 150, incrementing by 25 at each step. The range  function is exclusive; therefore, we specify a stopping value of 175 rather than 150

list(range(0, 175, 25))

[0, 25, 50, 75, 100, 125, 150]

handles the actual drawing of the circle. The first parameter is our canvas , the image we want to draw the circle on. We then need to supply the point in which our circle will be drawn around. We pass in a tuple of (centerX, centerY)  so that our circles will be centered at the center of the image. The third argument is the radius of the circle we wish to draw. Finally, we pass in the color of our circle: in this case, white.

Let’s do some abstract drawing:

And in last part of our code contains a hands-on on what we learn here. Thank you Please give a thumbs up if you are liking our post and in any how it enriches your journey into Advance Machine Learning.

github link:click here.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s