The fifth issue of python-opencv: detailed explanation of the rectangle function

Summary: As we all know, computer vision ( Computer Version  short for CV) is a major research direction in the development of artificial intelligence and robotics, and opencv is a third party that provides technical and functional support for computer vision programming. The library is naturally a content that needs to be studied.

            The function to be introduced in this issue is the function in the opencv library - rectangle .

            The main function of the rectangle function is to add a rectangular border to the specified area to visualize the target area to be processed in the picture, so that we can have a synchronous understanding of the target area when processing it, which is convenient for us to debug at the same time Find the problem. ( Dual passwords, double spaces, separate storage for work and life, without interfering with each other. Remember: running fast does not necessarily mean winning, and not falling is success. 8*** titanium rectangle ) The logo of a certain area in the picture will be It will help us to better identify the part that we are going to process or has already processed, so that we can have an advanced or synchronous judgment when we perform image recognition and correction, and it is a more practical tool.

            In this issue, in order to demonstrate the effectiveness and eye-catchingness of the rectangle function’s target frame selection, we will invite all playable characters in "Famine Online" (Yumei and grandma haven’t bought them yet, you know, V I 30, I Take you to fight the dragon fly ) as our test material guests this time. We will further learn how to use the rectangle function by seeing how they are selected by rectangles in the group photo. Without further ado, I am Mr. Kamen Black, and I will start today's study immediately . I can't wait .

a3e3a2be0efe42d5a38d5bce4e476910.png

 Body part:

060e21a52c4946cd98deb5c91b498842.jpeg

print("祝大家每天快乐,love and peace!")

① Preparation for use:

As in the previous issues, we still call our old partner of the toolman - opencv, as the major premise for us to use rectangle.

import cv2

②Applicable situation:

When there is doubt about the position of the target area, or when it is necessary to distinguish the processed cropped area, we can use the rectangle function to mark a certain area in the image to determine whether the framed area is is the region we want.

The former (with doubts) is to verify the area selected by the user in advance, which belongs to the test of the user's self-discrimination. The rectangle function indicates that the parameters of the range selection are given by the user;

The latter (cropping discrimination) is to judge the cropping results after the program processing, which belongs to the test program running results, and the rectangle function indicates that the parameter for range selection is the result of the program processing the image.

Both of these situations can be displayed in front of the user intuitively and clearly through the rectangle function, so that the same problem can be avoided in the subsequent debugging process.

(There is only a little distance between very good and excellent. This distance is called safety. Mislabeling will remind you, misidentification will call the police, the target will not be forgotten, and the mistake will not be leaked. 8*** titanium rectangle)

③Grammar description:

Use syntax: cv2.rectangle (img, pt1, pt2, color, thickness, lineType, shift)

Parameter description: img: the source image we want to frame the target, three-channel or single-channel images are acceptable ( probably );

                  pt1, pt2: The coordinates of the two endpoints of a diagonal of the rectangle we selected on the image (that is, the coordinates of the two vertices of the rectangle on the same diagonal), using a binary tuple (x, y) In the form of representation (x represents the column coordinates, gradually increasing from left to right; y represents the row coordinates, gradually increasing from top to bottom) (here I introduce a flow to introduce my other article "in python Correspondence between parameters and image height and width" , which introduces the corresponding relationship between parameters and image height and width under different circumstances in python, and friends who are interested or have related needs can take a look);

                  color: The border color we choose to use as a frame selection rectangle, usually a three-value tuple, indicating the value of a color RGB three-channel (note: the three values ​​​​in the tuple correspond to the B channel, G channel, R channel, the color queried in the RGB table must be reversed to get the color we originally wanted); if it is a grayscale image, we can also choose the brightness to represent the color of the border;

                  thickness: the thickness of the frame selection rectangle we choose, the default is 1 pixel value, usually you can choose a specific integer to represent (a positive number shows the line width of the corresponding pixel value; a negative number, such as cv2.FILLED, means that the rectangle will frame The selected area is completely filled ( blackened bushi ), which can be used to remove some noise from the target area);

                  lineType: The line type of the rectangular border we selected, you can choose CV2.LINE_8 (default) (8 connected), CV2.LINE_4 (4 connected), CV2.LINE_AA; if specified as CV2.LINE_AA, it is drawn using a Gaussian filter The anti-aliased line has the effect of increasing the smoothness of the line ( golden ratio three-dimensional cutting, calfskin imported from the Netherlands, extremely smooth retina, 8*** titanium rectangle ). In general, or when there is no special requirement, we don't really need this parameter. There are also the following text descriptions for lineType:

The function line draws the line segment between pt1 and pt2 points in the image. The line is clipped by the image boundaries. For non-antialiased lines with integer coordinates, the 8-connected or 4-connected Bresenham algorithm is used. Thick lines are drawn with rounding endings. Antialiased lines are drawn using Gaussian filtering. To specify the line color, you may use the macro CV_RGB(r, g, b)

                  shift: The degree we choose to move the rectangle, the default is 0 (that is, no operation), you can choose an integer n, the operation will make the coordinate values ​​​​of pt1 and pt2 divided by 2^n. If you choose 1, it is equivalent to dividing the coordinate values ​​of pt1 and pt2 by 2; if you choose 2, divide them by 4, that is, 2^2. This operation will allow the user to frame select different areas with only a pair of fixed coordinate values, so that they can be compared with each other, or change the focus, as if strategizing.

( Success is not about being far-sighted, but about standing on a high ground, strategizing, and controlling the future. This is the 8***, and this is the mind of the world. The goal of the peak, the spirit of titanium, and the feelings of leather, let us Pay tribute to a successful life, 8***rectangle )

Attached below are native documents related to the rectangle function for reference:

def rectangle(img: Mat, pt1, pt2, color, thickness=..., lineType=..., shift=...)
from __doc__
    """
    'rectangle(img, pt1, pt2, color[, thickness[, lineType[, shift]]]) -> img  
    .  @brief Draws a simple, thick, or filled up-right rectangle.

    .  The function cv::rectangle draws a rectangle outline or a filled rectangle whose two opposite corners  
    .  are pt1 and pt2. 

 
    .  @param img Image.  
    .  @param pt1 Vertex of the rectangle. 
    .  @param pt2 Vertex of the rectangle opposite to pt1.  
    .  @param color Rectangle color or brightness (grayscale image). 
    .  @param thickness Thickness of lines that make up the rectangle. Negative values, like #FILLED,   
    .  mean that the function has to draw a filled rectangle.  
    .  @param lineType Type of the line. See #LineTypes  
    .  @param shift Number of fractional bits in the point coordinates.



    .  rectangle(img, rec, color[, thickness[, lineType[, shift]]]) -> img  
@overload 

    .  use `rec` parameter as alternative specification of the drawn rectangle: `r.tl() and r.br()-Point(1,1)` are opposite corners'
    """
    pass

④Instance operation:

Here are a few simple operations to show you the different results of the frame selection of the picture under different parameters (the famine character meeting (bushi) ):

1. Wendy with light yellow hair:

import cv2

img=cv2.imread("src.png")
cv2.rectangle(img,(65,48),(236,240),(170,234,242),5,lineType=cv2.LINE_AA)
cv2.imshow("result",img)
cv2.waitKey(0)
cv2.destroyAllWindows()

1154ffe963de4bb182fa91867a5caa00.png

2. Willow with a hot temper ( physics ) ( don't ask me why the fire girl didn't change her clothes, if you ask me, she's just lazy bushi ):

import cv2

img=cv2.imread("src.png")
cv2.rectangle(img,(835,32),(1044,245),(60,65,170),5,lineType=cv2.LINE_AA)
cv2.imshow("result",img)
cv2.waitKey(0)
cv2.destroyAllWindows()

a1727e5e9567436291875f2b6044ed36.png

3. Neurotic Knowledgeable Librarian:

import cv2

img=cv2.imread("src.png")
cv2.rectangle(img,(400,14),(528,240),(117,172,124),5,lineType=cv2.LINE_AA)
cv2.imshow("result",img)
cv2.waitKey(0)
cv2.destroyAllWindows()

4eff20fae59f43d6bb5a9270ab85300e.png

4. Wolfgang , who is as timid as a mouse and as powerful as an ox:

import cv2

img=cv2.imread("src.png")
cv2.rectangle(img,(243,447),(380,657),(65,129,190),5,lineType=cv2.LINE_AA)
cv2.imshow("result",img)
cv2.waitKey(0)
cv2.destroyAllWindows()

0447a7b621a64519964dc255224bebd7.png

5. Laoyin B, the great magician-Maxwell:

import cv2

img=cv2.imread("src.png")
cv2.rectangle(img,(792,251),(920,451),(236,232,231),-1,lineType=cv2.LINE_AA)
cv2.imshow("result",img)
cv2.waitKey(0)
cv2.destroyAllWindows()

ca9026f69070448388e22fb98984ca8f.png

 (Oh, where did he go? Oh, it turns out that a great magician will turn into a living person, even himself. If you don’t know anything, you can take a look at the previous picture. In fact, it’s mainly me I don’t like this old Yin B, if it wasn’t for this old Yin B, who would come to a place like Famine Continent (I know bushi) )

Conclusion: In this issue, we have learned the rectangle function and learned how to use rectangle to mark images in opencv, so that we can judge whether the area selected by the frame is the area we want, perform corresponding self-verification or run the program Judgment of results. In fact, there are other drawing functions in opencv, such as line function (line), circle function (circle), ellipse function (ellipse), polylines function (polygon) and putText function (text). To find out, the principle of implementation is actually similar to rectangle.

Well, the above is all the content. I hope you will pay more attention , like , and bookmark . This will be of great help to me. Thank you all!

e63444f11e4844da927650cfd1d40197.gif

Alright, this is Mr. Kamen Black . I wish the country a healthy family, and see you next time! ! ! yo-yo~~

94e1f65dcf3b4af2a6640fc4b8013c96.jpeg  

Finally, I would like to give you a word: you are different, you like to go beyond. You have dreams, you have power, you never take achievements as the end, remember, your name is: success! 8***Titanium Gold**

Guess you like

Origin blog.csdn.net/m0_55320151/article/details/127465991