3h Proficient in OpenCV (4) - drawing shapes and text

0. Preparation

Right-click the newly created project, select Python File, create a new Python file, import cv2import the cv2 library at the beginning, import numpyand rename it to np.

import cv2
import numpy as np

insert image description here
We also need to know that in OpenCV, the direction of the coordinate axis is the x-axis to the right, the y-axis down, and the coordinate origin is in the upper left corner. For example, the following picture is 640 pixels long and 480 pixels wide. OK, let's start the study of this section.
insert image description here

1. Draw a grayscale image
  1. np.zeros((512,512)), defines a 512X512 (height X width) matrix, all elements are 0
  2. Because there is only 1 channel, this is a grayscale image, and all pixel values ​​are 0, which is a pure black image
img=np.zeros((512,512))
cv2.imshow("Image",img)
cv2.waitKey(0)

Let's take a look at the effect:
insert image description here
we output the size of this image to see if there is only one channel and find that it is.

print(img.shape)

insert image description here

2. Colorize the image
  • np.zeros((512,512,3),np.uint8)It is to set a three-dimensional matrix with a height of 512, a width of 512, and the number of channels to 3. np.uint8 is to ensure that the value of the element is between [0,255]. That is, the range of the BGR color space.
  • img[:]=255,0,0is to modify the whole image to change the color to blue because B is 255 and G and R are 0.
  • Finally imshow displays the image.
img=np.zeros((512,512,3),np.uint8)
print(img.shape)
img[:]=255,0,0
cv2.imshow("Image",img)
cv2.waitKey(0)

Let's run it and see that the image does turn blue:
insert image description here

Let's take a look at the output of the console, from the single-channel grayscale image to a 3-channel color image:
insert image description here
As we said in the previous section, image cropping is actually a region selection of the matrix, so the coloring is the same, we can The image is colored in a certain area, but not all:img[200:300,200:300]=255,0,0

img=np.zeros((512,512,3),np.uint8)
print(img.shape)
img[200:300,200:300]=255,0,0
cv2.imshow("Image",img)
cv2.waitKey(0)

The operation effect is as follows:
insert image description here

3. Draw the shape

First, draw a straight line

  • The line() method of cv2, the parameters are (image, start point coordinates, end point coordinates, line color, line thickness)
  • 我们这里的坐标顺序都是(宽,高)。因为在OpenCV中函数都是先宽后高,符合图像认知的规律。
  • The coordinates of the numpy matrix are (height, width), because it is a certain row and a certain column, which conforms to the law of arrays.
img=np.zeros((512,512,3),np.uint8)
cv2.line(img,(0,0),(img.shape[1],img.shape[0]),(125,125,0),3)
cv2.imshow("Image",img)
cv2.waitKey(0)

Let's run and see the effect, the color is turquoise and the thickness is 3 lines:
insert image description here
2. Draw a rectangle

  • The rectangle method of cv2, the parameters are (image, coordinates of the upper left corner, coordinates of the lower right corner, line color, line thickness)
img=np.zeros((512,512,3),np.uint8)
cv2.line(img,(0,0),(img.shape[1],img.shape[0]),(125,125,0),3)
cv2.rectangle(img,(0,0),(250,250),(123,222,66),10)
cv2.imshow("Image",img)
cv2.waitKey(0)

Let's run to see the effect, the lines and our rectangles are drawn on the grayscale image:
insert image description here
3. Draw a circle

  • The circle method of cv2, the parameters are (image, circle center, radius, line color, line thickness)
img=np.zeros((512,512,3),np.uint8)
cv2.line(img,(0,0),(img.shape[1],img.shape[0]),(125,125,0),3)
cv2.rectangle(img,(0,0),(250,250),(123,222,66),10)
cv2.circle(img,(400,400),30,(0,165,165),5)
cv2.imshow("Image",img)
cv2.waitKey(0)

You can adjust the coordinates or colors at will, and run to see the effect:
insert image description here

4. Draw the text

On the basis of drawing the shape above, add a line of code to draw text.

Let's first look at the parameters of the source code of the putText() method, you can see that the order is (original image, text, starting coordinates, font, size, color, thickness)

def putText(img, text, org, fontFace, fontScale, color, thickness=None, lineType=None, bottomLeftOrigin=None)

The implemented code is on the fifth line:

img=np.zeros((512,512,3),np.uint8)
cv2.line(img,(0,0),(img.shape[1],img.shape[0]),(125,125,0),3)
cv2.rectangle(img,(0,0),(250,250),(123,222,66),10)
cv2.circle(img,(400,400),30,(0,165,165),5)
cv2.putText(img,"OpenCV",(300,200),cv2.FONT_HERSHEY_SIMPLEX,1,(0,125,0),2)
cv2.imshow("Image",img)
cv2.waitKey(0)

Run and see the effect:
insert image description here
In addition to using the OpenCv method, you can also use the PIL library and pyplot library to achieve: Python adds annotation information to the picture


The content of the fourth section takes some time to understand slowly. In fact, knowing the difference between the OpenCV coordinate system and the numpy matrix coordinate system, it is suddenly clear. It is better to go through your eyes a thousand times than to go through your hands once, go and knock it once with your hands~

Guess you like

Origin blog.csdn.net/qq_42257666/article/details/122908196