cv2.polylines, cv2.fillPoly and polygon drawing and segmentation results Python functions (1)

If you just want to learn code, just read the next article:
https://blog.csdn.net/HaoZiHuang/article/details/127027469

Let’s first lay out a few functions used cv2.polylines:cv2.fillPoly

The following content is partially taken from:
http://www.juzicode.com/opencv-python-polylines-puttext

Let’s take a look at the code first:

import cv2
import numpy as np


img = np.zeros((512,512,3)) #白色背景
color=(0,255,0)             #绿色

# ------ 五角星的顶点 ------ 
pts = np.array([[70,190],
                [222,190],
                [280,61],
                [330,190],
                [467,190],
                [358,260],
                [392,380],
                [280,308],
                [138,380],
                [195,260]])
# pts = pts.reshape((-1, 1, 2))  # reshape为 10x1x2 的 numpy.ndarray
print(pts.shape)     # (10, 1, 2)

# ------ 绘制图形 ------ 
cv2.polylines(img,
              # [pts-20, pts+30],
              [pts],
              True,    # 是否闭合
              color,
              5)
 

# ------ 以下是显示三件套 ------
cv2.imshow('show',img)
cv2.waitKey()
cv2.destroyAllWindows()

cv2.polylinesParameters:

  • Sketch board drawing
  • list of drawn polygons
  • bool whether closed
  • color
  • Thickness

  • This is the graph with the third parameter being True (closed) / False (not closed) :

Insert image description here
However, the last point of some labels is the same as the first point. In this case, False/True does not affect it, and it will be closed because the last point is the same as the first point.

  • The fourth parameter is the color. Note that it is in the form of BGR, (0, 0, 255)which is red.
  • The fifth parameter is the thickness. Note that you cannot give -1 to fill the polygon like drawing a circle. There is a dedicated function for polygon filling.cv2.fillPoly
  • Next is the second parameter worth talking about. Why is it [pts]? Why is it a list? How about I change it to [pts1, pts2]try?
    Every time, here you can pass in multiple [X, 1, 2]np.ndarrays in the shape

    of. We do this Experiment:
    cv2.polylines(img,
                  [pts-20, pts+30],
                  True,   # 是否闭合
                  color,
                  5)
    

Insert image description here
In this way, two polygons are drawn, but the colors are the same. Therefore, if you want to draw multiple polygons with different colors, you should call this function multiple times instead of passing in multiple arrays in this list.

  • There is this line in the code:
    # pts = pts.reshape((-1, 1, 2))  # reshape为 10x1x2 的 numpy.ndarray
    
    I commented it out. I saw many people's blogs with this line. I tried it without this operation and it worked. It can still run with this operation. It's a bit outrageous. Maybe the old version of opencv requires this operation. ???

OKOK, finally finished cv2.polylinestalking about it, let’s talk about this next cv2.fillPoly, its usage is relatively simple:

import cv2
import numpy as np


img = np.zeros((512,512,3)) #白色背景
color=(0,0,255)             # 红色

#五角星
pts = np.array([[70,190],
                [222,190],
                [280,61],
                [330,190],
                [467,190],
                [358,260],
                [392,380],
                [280,308],
                [138,380],
                [195,260],
                [70,190]])


cv2.fillPoly(img,   # 原图画板
             [pts], # 多边形的点
             color=(0, 0, 255))

 
cv2.imshow('show',img)
cv2.waitKey()

Simple usage requires passing in pictures, polygon points and colors
Insert image description here

When you see this, you may want to do something like this:

cv2.fillPoly(img,   # 原图画板
             [pts-20, pts+30], # 多边形的点
             color=(0, 0, 255))

Do you think his result is like this:
Insert image description here

The actual result is this:
Insert image description here
As for why this is the case, I think the knowledge of imaging can give an explanation. It should be the section on polygon scan conversion . If you are interested, take a look for yourself.

The next article will look at the function for drawing polygon segmentation results
https://blog.csdn.net/HaoZiHuang/article/details/127027469

(I’ve been so annoyed recently. I realized that I’ve been figuring out how to use functions written by others... I rarely write them myself. I’m just a package-switcher wwwwww)

Guess you like

Origin blog.csdn.net/HaoZiHuang/article/details/127026323