This is the 10th day of my participation in the August Text Challenge.More challenges in August

If you have ideas or techniques you’d like to share, feel free to leave them in the comments section.

The purpose of this blog: How to add a rectangle to a picture using OpenCV, using the functions cv2.rectangle and cv2.circle.

cv2.rectangle

This function draws a simple rectangle on the image in the following syntax:

 cv2.rectangle(img, pt1, pt2, color[, thickness[, lineType[, shift]]])
Copy the code

This function returns no value. The parameters are described as follows:

  • Img: image of the rectangle to be drawn;
  • Pt1: The starting coordinate of the rectangle, represented by a tuple, i.e. (X coordinate, Y coordinate);
  • Pt2: the end coordinates of the rectangle, represented by tuples, as above;
  • Color: the border color of the rectangle to draw. If BGR is used, (255,0,0) is blue.
  • Thinkness: The thickness of the rectangle border pixels, if used- 1, will fill the entire rectangle;
  • The lineType and shift parameters are not used for the time being.

Mark the image with a red rectangle.

import cv2 as cv
import matplotlib.pyplot as plt

img = cv.imread('12.jpg', -1)
img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
cv.rectangle(img, (90.30), (280.230), (255.0.0), 2)

plt.imshow(img)
plt.show()
Copy the code

The effect is as follows:If you havethinknessSet to- 1, will appear as a red rectangle.

cv.rectangle(img, (90.30), (280.230), (255.0.0), -1)
Copy the code

cv2.circle

This function is used to draw circles. The syntax is as follows:

 cv2.circle(img, center_coordinates, radius, color, thickness)
Copy the code

Parameter Description:

  • Img: Image of the circle to be drawn;
  • Center_coordinates: The center coordinates of the circle. Is represented by tuples, that is, (X coordinate value, Y coordinate value);
  • Radius: The radius of a circle;
  • Color: The color of the boundary of the circle to be drawn. For BGR, use tuples, for example :(255,0,0) is blue;
  • Thickness: It is the thickness and thickness of a circle boundary. A thickness of -1 pixel will fill the rectangle with the specified color.

The test code is as follows:

cv.circle(img, (60.60), 50, (0.200.230), 2)
plt.imshow(img)
plt.show()
Copy the code

Other functions

Use OpenCV to mark images, as well as other functions such as cv2.line() to draw lines, cv2.ellipse() to draw ellipses, and cv2.puttext () to draw text.

Seeing this, I instinctively noticed the text drawing, because a big hole often appeared in this is the problem of Chinese garbled code.

Next, give it a try.

Let’s look at the syntax for this function:

cv2.putText(img, text, org, font, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]])
Copy the code

The parameters are described as follows:

  • Img: image of text to be drawn.
  • Text: Text string to be drawn.
  • Org: coordinates in the lower left corner of the image text string. Using tuples, i.e. (X coordinate value, Y coordinate value)
  • Font: font type. Some font types areFONT_HERSHEY_SIMPLEX.FONT_HERSHEY_PLAIN
  • FontScale: fontScale factor multiplied byfont-specificThe basic size
  • Color: The color of the text string to be drawn. For BGR, a tuple is used. For example :(255,0,0) is blue.
  • Thickness: It is the thickness and thickness of a line.
  • LineType: Optional argument, which gives the type of row to use.
  • BottomLeftOrigin: Optional. If true, the image data origin is in the lower left corner. Otherwise, it is in the upper-left corner.

Try a wave in English first.

import cv2 as cv
import matplotlib.pyplot as plt

img = cv.imread('12.jpg', -1)
img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
cv.rectangle(img, (90.30), (280.230), (255.0.0), 2)
cv.circle(img, (60.60), 50, (0.200.230), 2)

# draw text
cv.putText(img, 'OpenCV', (250.250), cv.FONT_HERSHEY_SIMPLEX,
           1, (0.190.150), 2)

plt.imshow(img)
plt.show()
Copy the code

English is obviously fine.Change the content to Chinese and check whether the output is normal.

cv.putText(img, 'Dream Eraser', (100.350), cv.fon, 1, (0.190.150), 2)
Copy the code

As the eraser had expected, the Chinese characters were garbled.For this part, after retrieval learning, we found that the solution is relatively fixed. Just read the following article.

Blog.csdn.net/ctwy291314/…

In addition to this method, there is a kind of, can also try.

Python + OpencV + FreeType (support py2, Py3)

OpenCV end

An hour has passed. Have you mastered the Python OpenCV related knowledge points?

In your spare time, you can subscribe to eraser’s crawler course to learn crawler knowledge.