OpenCV-Python learning (13) - OpenCV polygon filling and drawing (cv.fillPoly, cv.polylines)

1. Knowledge points

  1. Learn the use of cv.polylines function;
  2. Learn the use of cv.fillPoly function.

2. Draw polyline or polygon cv.polylines function description

2.1 Function usage

cv.polylines(img, pts, isClosed, color[, thickness[, lineType[, shift]]]) → img

2.2 Parameter description

parameter illustrate
img An img object representing the image on which to draw the rectangle.
pts Represents one or more point sets.
isClosed Represents a flag that determines whether the drawn polygon is closed. If True, draw several closed polygons; if False, draw a polyline connecting all points.
color Indicates the color.
thickness Indicates the line width, note: must be greater than 0 .
lineType Indicates the linearity of drawing a straight line, the default is LINE_8.
shift Indicates that the coordinates are accurate to the number of digits after the decimal point.

2.3 Description of lineType value

value describe
cv.LINE_4 Indicates a 4-adjacent linetype.
cv.LINE_8 Represents an 8-contiguous linetype.
cv.LINE_AA Indicates anti-aliased line style, smoother image.

3. Fill color cv.fillPoly function description

3.1 Function usage

cv.fillPoly(img, pts, color[, lineType[, shift[, offset]]]) → img

3.2 Parameter description

parameter illustrate
img An img object representing the image on which to draw the rectangle.
pts Represents one or more point sets.
color Indicates the color.
lineType Indicates the linearity of drawing a straight line, the default is LINE_8.
shift Indicates that the coordinates are accurate to the number of digits after the decimal point.

3.3 Description of lineType value

value describe
cv.LINE_4 Indicates a 4-adjacent linetype.
cv.LINE_8 Represents an 8-contiguous linetype.
cv.LINE_AA Indicates anti-aliased line style, smoother image.

4. Attention

  1. The value of thickness line width must be greater than 0;
  2. When the isClosed closed flag is True, draw several closed polygons; when the closed flag is False, draw a polyline connecting all points;
  3. The pts point set indicates that the functions cv.polylines and cv.fillPoly can draw or fill one or more polygons;
  4. The pts point set parameter must set dtype=np.uint8.

5. Examples

5.1 Example code

import cv2 as cv
import numpy as np

# 以五角星的重心为原点,计算各点坐标
def get_star_point(r = 100):
  # 计算没一份的度数和内五边形的r
  pi_val = np.pi / 180
  min_r = r * np.sin(18 * pi_val) / np.cos(36 * pi_val)
  # 外五边形的坐标
  a = [0,r]
  b = [r * np.cos(18 * pi_val), r * np.sin(18 * pi_val)]
  c = [r * np.cos(54 * pi_val), - r * np.sin(54 * pi_val)]
  d = [- r * np.cos(54 * pi_val), - r * np.sin(54 * pi_val)]
  e = [- r * np.cos(18 * pi_val), r * np.sin(18 * pi_val)]
  # 内五边形的坐标
  in_a = [min_r * np.cos(54 * pi_val),min_r * np.sin(54 * pi_val)]
  in_b = [min_r * np.cos(18 * pi_val),- min_r * np.sin(18 * pi_val)]
  in_c = [0, - min_r]
  in_d = [- min_r * np.cos(18 * pi_val),- min_r * np.sin(18 * pi_val)]
  in_e = [- min_r * np.cos(54 * pi_val),min_r * np.sin(54 * pi_val)]
  return {
    "out": [a, b, c, d, e],
    "in": [in_a, in_b, in_c, in_d, in_e]
  }

# 绘制五边形
def create_pentagon(a,b,c,d,e,isClosed=True,color=(0,0,255),fill=False,fillColor=(255,0,0)):
  img = np.ones((200, 200, 3), np.uint8)*255
  pts = np.array([a,b,c,d,e])
  # 向左上角移动100像素原点
  pts[:,:] += 100
  cv.polylines(img, [pts], isClosed, color,1)
  # 判断是否填充多边形
  if fill:
    cv.fillPoly(img,[pts],fillColor)
  return img

def create_five_pointed_star(a,b,c,d,e,
isClosed=True,
color=(0,0,255),
fill=False,
fillColor=(255,0,0)):
  img = np.ones((200, 200, 3), np.uint8)*255
  pts = np.array([a,c,e,b,d])
  # 向左上角移动100像素原点
  pts[:,:] += 100
  cv.polylines(img, [pts], isClosed, color,1)
  # 判断是否填充多边形
  if fill:
    cv.fillPoly(img,[pts],fillColor)
  return img

def create_five_pointed_star_all(
  a,b,c,d,e,
  in_a,in_b,in_c,in_d,in_e,
  isClosed=True,
  color=(0,0,255),
  fill=False,
  fillColor=(255,0,0)):
  img = np.ones((200, 200, 3), np.uint8)*255
  pts = np.array([a,in_a,b,in_b,c,in_c,d,in_d,e,in_e])
  # 向左上角移动100像素原点
  pts[:,:] += 100
  cv.polylines(img, [pts], isClosed, color,1)
  # 判断是否填充多边形
  if fill:
    cv.fillPoly(img,[pts],fillColor)
  return img

def create_demo():
  # 以五角星长轴半径计算各点坐标,注意计算坐标需要倒转
  star_points = get_star_point(60)
  [a,b,c,d,e] = list(map(lambda items: list(map(lambda val: - int(val), items)), star_points.get("out")))
  [in_a, in_b, in_c, in_d, in_e] = list(map(lambda items: list(map(lambda val: - int(val), items)), star_points.get("in")))
  # 
  img = np.ones((600, 600, 3), np.uint8)*255
  # 不闭合折线
  # 五角星【线交叉】
  img[0:200,0:200] = create_five_pointed_star(a,b,c,d,e,isClosed=False)
  # 五边形
  img[200:400,0:200] = create_pentagon(a,b,c,d,e,isClosed=False)
  # 五角星【十点】
  img[400:600,0:200] = create_five_pointed_star_all(a,b,c,d,e,in_a,in_b,in_c,in_d,in_e,isClosed=False)

  # 闭合多边形
  # 五角星【线交叉】
  img[0:200,200:400] = create_five_pointed_star(a,b,c,d,e)
  # 五边形
  img[200:400,200:400] = create_pentagon(a,b,c,d,e)
  # 五角星【十点】
  img[400:600,200:400] = create_five_pointed_star_all(a,b,c,d,e,in_a,in_b,in_c,in_d,in_e)

  # # 闭合填充多边形
  # # 五角星【线交叉】
  img[0:200,400:600] = create_five_pointed_star(a,b,c,d,e,fill=True)
  # 五边形
  img[200:400,400:600] = create_pentagon(a,b,c,d,e,fill=True)
  # 五角星【十点】
  img[400:600,400:600] = create_five_pointed_star_all(a,b,c,d,e,in_a,in_b,in_c,in_d,in_e,fill=True)

  cv.imshow("img", img)
  cv.waitKey(0)
  cv.destroyAllWindows()

if __name__ == "__main__":
  create_demo()

5.2 Example running results

Enter a picture description

6. Summary

  1. When calculating the coordinates of each point of the five-pointed star, the mathematical four-quadrant coordinates are used, and the center of gravity is used as the origin to create coordinates;
  2. When opencv draws graphics, the x-axis is the same as the mathematical coordinate axis, but y is the positive axis downward, and the origin of the coordinates is at the upper left corner of the image [0,0];
  3. Through the above two points, we can know that first: the calculated y-axis coordinate needs to be reversed; second: the origin of the calculation point needs to be calculated for displacement.

Guess you like

Origin blog.csdn.net/m0_38082783/article/details/127671039