当前位置:网站首页>[200 opencv routines] 212 Draw a slanted rectangle

[200 opencv routines] 212 Draw a slanted rectangle

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

OpenCV routine 200 piece General catalogue


【youcans Of OpenCV routine 200 piece 】212. Draw a slanted 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 .


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

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

routine A4.3: Draw a slanted rectangle on the image

cv.rectangle Only rectangles perpendicular to the boundary can be drawn on the image . If you need to draw a slanted rectangle , To obtain the coordinates of each vertex of the inclined rectangle , Construct a closed rectangle by drawing a straight line .

    # A4.3  Draw a slanted rectangle on the image 
    height, width, channels = 600, 400, 3
    img = np.ones((height, width, channels), np.uint8)*192  #  Create a black image  RGB=0

    #  Rotate around the center of the rectangle 
    x, y, w, h = (100, 200, 200, 100)  #  Top left coordinates  (x,y),  Width  w, Height  h
    cx, cy = x+w//2, y+h//2  #  Rectangular center 
    img1 = img.copy()
    cv.circle(img1, (cx,cy), 4, (0,0,255), -1)  #  Center of rotation 
    angle = [15, 30, 45, 60, 75, 90]  #  Rotation Angle , clockwise 
    for i in range(len(angle)):
        ang = angle[i] * np.pi / 180
        x1 = int(cx + (w/2)*np.cos(ang) - (h/2)*np.sin(ang))
        y1 = int(cy + (w/2)*np.sin(ang) + (h/2)*np.cos(ang))
        x2 = int(cx + (w/2)*np.cos(ang) + (h/2)*np.sin(ang))
        y2 = int(cy + (w/2)*np.sin(ang) - (h/2)*np.cos(ang))
        x3 = int(cx - (w/2)*np.cos(ang) + (h/2)*np.sin(ang))
        y3 = int(cy - (w/2)*np.sin(ang) - (h/2)*np.cos(ang))
        x4 = int(cx - (w/2)*np.cos(ang) - (h/2)*np.sin(ang))
        y4 = int(cy - (w/2)*np.sin(ang) + (h/2)*np.cos(ang))
        color = (30*i, 0, 255-30*i)
        cv.line(img1, (x1,y1), (x2,y2), color)
        cv.line(img1, (x2,y2), (x3,y3), color)
        cv.line(img1, (x3,y3), (x4,y4), color)
        cv.line(img1, (x4,y4), (x1,y1), color)

    #  Rotate around the top left vertex of the rectangle 
    x, y, w, h = (200, 200, 200, 100)  #  Top left coordinates  (x,y),  Width  w, Height  h
    img2 = img.copy()
    cv.circle(img2, (x, y), 4, (0,0,255), -1)  #  Center of rotation 
    angle = [15, 30, 45, 60, 75, 90, 120, 150, 180, 225]  #  Rotation Angle , clockwise 
    for i in range(len(angle)):
        ang = angle[i] * np.pi / 180
        x1, y1 = x, y
        x2 = int(x + w * np.cos(ang))
        y2 = int(y + w * np.sin(ang))
        x3 = int(x + w * np.cos(ang) - h * np.sin(ang))
        y3 = int(y + w * np.sin(ang) + h * np.cos(ang))
        x4 = int(x - h * np.sin(ang))
        y4 = int(y + h * np.cos(ang))
        color = (30 * i, 0, 255 - 30 * i)
        cv.line(img2, (x1, y1), (x2, y2), color)
        cv.line(img2, (x2, y2), (x3, y3), color)
        cv.line(img2, (x3, y3), (x4, y4), color)
        cv.line(img2, (x4, y4), (x1, y1), color)

    plt.figure(figsize=(9, 6))
    plt.subplot(121), plt.title("img1"), plt.axis('off')
    plt.imshow(cv.cvtColor(img1, cv.COLOR_BGR2RGB))
    plt.subplot(122), plt.title("img2"), plt.axis('off')
    plt.imshow(cv.cvtColor(img2, cv.COLOR_BGR2RGB))
    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
212. Draw a slanted rectangle

原网站

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