当前位置:网站首页>[200 opencv routines] 211 Draw vertical rectangle

[200 opencv routines] 211 Draw vertical rectangle

2022-06-27 09:38:00 Xiaobai youcans

OpenCV routine 200 piece General catalogue


【youcans Of OpenCV routine 200 piece 】211. Draw vertical rectangle

7.1 Basic parameters of drawing function

OpenCV It provides drawing function , You can draw a line on the image 、 rectangular 、 round 、 Ellipse and other geometric figures .

function cv.line()、cv.rectangle()、cv.circle()、cv.polylines() And so on are used to draw straight lines in the image 、 rectangular 、 circular 、 Polygon and other geometric shapes , There are some setting parameters in these drawing functions , Introduce the following :

  • img: Input / output image , Format is unlimited
  • color: The color of the drawing line ,(b,g,r) Tuple of format , Or a scalar representing the gray value
    • thickness: Draw the thickness of the line , The default value is 1px,-1 Indicates internal padding
  • lineType: Draw the linear of the line segment , The default is LINE_8
    • cv.FILLED: Inside filling ( Solid graphics )
    • cv.LINE_4:4 Adjacency Linetype
    • cv.LINE_8:8 Adjacency Linetype
    • cv.LINE_AA: Anti aliasing , Smoother image
  • shift: The number of decimal places of point coordinates , The default is 0

7.3 Draw a rectangle

The function prototype :

function cv.rectangle() Used to draw a rectangle perpendicular to the image boundary on the image .

cv.rectangle(img, pt1, pt2, color[, thickness=1, lineType=LINE_8, shift=0]) → img
cv.rectangle(img, rec, color[, thickness=1, lineType=LINE_8, shift=0]) → img

Parameter description :

  • img: Input / output image , Allows single channel grayscale images or multi-channel color images
  • pt1: Coordinates of the first point of the matrix ,(x1, y1) Tuple of format
  • pt2: And pt1 Coordinates of the second point of the diagonal matrix ,(x2, y2) Tuple of format
  • color: The color of the drawing line ,(b,g,r) Tuple of format , Or a scalar representing the gray value
  • thickness: Draw the line width of the rectangle , The default value is 1px, A negative number means that the inside of the rectangle is filled
  • lineType: Draw the linear of the line segment , The default is LINE_8
  • shift: The number of decimal places of point coordinates , The default is 0

matters needing attention :

  1. The drawing operation will be performed directly on the incoming image img Make changes , Whether to accept the return value of the function or not . If you want to keep the input image unchanged, use img.copy() replicate .
  2. Use the opposite corner of the rectangle pt1、pt2 Draw a rectangle .pt1、pt2 Order independent and interchangeable , It can be top left 、 Lower right corner , It can also be the lower left 、 Upper right corner .
  3. If the diagonal coordinates exceed the image boundary , The drawn rectangle is clipped by the image boundary , That is, only part of the rectangular border within the image boundary is drawn .
  4. Draw on a color image , line color color You can tuple (b,g,r) Express , Such as (0,0,255) It means red ; It can also be scalar b, But it doesn't mean grayscale lines , It means color (b,0,0).
  5. Only gray lines can be drawn on a single channel gray image , Cannot draw colored lines . however , line color color It can be scalar b, It can also be tuples (b,g,r), Will be interpreted as grayscale values b. The parameters of the last two channels in the tuple are invalid .
  6. When drawing a filled rectangle , Recommended choice thickness=-1 Set up , key word “thickness” It can be omitted .
  7. In some applications , Use (x , y, x+w, y+h) Define the coordinates of the diagonal point , Be careful (x,y) Must be The top left corner of the rectangle coordinate .
  8. Use rec Parameter draw rectangle ,r.tl() and r.br() Is the diagonal of the rectangle .

Rect Rectangle class :

stay OPenCV/C++ It defines Rect class , It is called rectangle class , contain Point Member variables of class x、y( rectangular Top left vertex coordinate ) and Size Member variables of class width and height( The width and height of the rectangle ).

stay OpenCV/Python in , You can't create... Directly Rect Rectangle class . But some internal functions use or return Rect, Such as cv.boundingRect.

rect = cv.boundingRect(contours[c])
cv.rectangle(img, (rect[0], rect[1]), (rect[0]+rect[2], rect[1]+rect[3]), (0,0,255)) 

rect stay C++ Is returned Rect Rectangle class , have access to rect.tl() and rect.br() Returns the coordinates of the upper left and lower right corners . And in the python What's back is 4 Tuples of elements (x , y, w, h), respectively Top left vertex Coordinates of (x,y)、 The width of the rectangle w And height h.


routine A4.2: Draw a vertical rectangle on the image

    # A4.2  Draw a vertical rectangle on the image 
    height, width, channels = 400, 300, 3
    img = np.ones((height, width, channels), np.uint8)*160  #  Create a black image  RGB=0

    img1 = img.copy()
    cv.rectangle(img1, (0,20), (100,200), (255,255,255))  #  white 
    cv.rectangle(img1, (20,0), (300,100), (255,0,0), 2)  #  Blue  B=255
    cv.rectangle(img1, (300,400), (250,300), (0,255,0), -1)  #  green , fill 
    cv.rectangle(img1, (0,400), (50,300), 255, -1)  # color=255  Blue 
    cv.rectangle(img1, (20,220), (25,225), (0,0,255), 4)  #  Influence of line width 
    cv.rectangle(img1, (60,220), (67,227), (0,0,255), 4)
    cv.rectangle(img1, (100,220), (109,229), (0,0,255), 4)

    img2 = img.copy()
    x, y, w, h = (50, 50, 200, 100)  #  Top left coordinates  (x,y),  Width  w, Height  h
    cv.rectangle(img2, (x,y), (x+w,y+h), (0,0,255), 2)
    text = "({},{}),{}*{}".format(x, y, w, h)
    cv.putText(img2, text, (x, y-5), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,255))

    #  Drawing lines can be used for grayscale images , Parameters  color  Only the first channel value is valid , And is set to the gray value 
    gray = np.zeros((height, width), np.uint8)  #  Create a grayscale image 
    img3 = cv.line(gray, (0,10), (300,10), 64, 2)
    cv.line(img3, (0,30), (300,30), (128,128,255), 2)
    cv.line(img3, (0,60), (300,60), (192,64,255), 2)
    cv.rectangle(img3, (0,200), (30,150), 128, -1)  # Gray=128
    cv.rectangle(img3, (60,200), (90,150), (128,0,0), -1)  # Gray=128
    cv.rectangle(img3, (120,200), (150,150), (128,255,255), -1)  # Gray=128
    cv.rectangle(img3, (180,200), (210,150), 192, -1)  # Gray=192
    cv.rectangle(img3, (240,200), (270,150), 255, -1)  # Gray=255

    plt.figure(figsize=(9, 6))
    plt.subplot(131), plt.title("img1"), plt.axis('off')
    plt.imshow(cv.cvtColor(img1, cv.COLOR_BGR2RGB))
    plt.subplot(132), plt.title("img2"), plt.axis('off')
    plt.imshow(cv.cvtColor(img2, cv.COLOR_BGR2RGB))
    plt.subplot(133),plt.title("img3"), plt.axis('off')
    plt.imshow(img3, cmap="gray")
    plt.tight_layout()
    plt.show()

Routine results :

 Insert picture description here



【 At the end of this section 】

Copyright notice :
reference : Use the Photoshop Levels adjustment (adobe.com)
[email protected] Original works , Reprint must be marked with the original link :(https://blog.csdn.net/youcans/article/details/125432101)
Copyright 2022 youcans, XUPT
Crated:2022-6-20
Welcome to your attention 『youcans Of OpenCV routine 200 piece 』 series , Ongoing update
Welcome to your attention 『youcans Of OpenCV Learning lessons 』 series , Ongoing update

210. There are so many holes in drawing a straight line ?
211. Draw vertical rectangle

原网站

版权声明
本文为[Xiaobai youcans]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/178/202206270934305346.html