cv2.fillConvexPoly
往多边形内部填充值
先做边缘检测,把小的目标填上:
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
for num in range(len(contours)):
if (cv2.contourArea(contours[num]) < 200):
print("fill")
# cv2.drawContours(image, contours[num], -1, (0, 255, 0), 2)
cv2.fillConvexPoly(thresh, contours[num], 255)
if len(contours)>0:
cv2.imshow("thresh2", thresh)
单通道的图可以:
import numpy as np
import cv2
img = np.zeros((680, 920), np.uint8)
triangle = np.array([[0, 0], [500, 800], [500, 400]])
cv2.fillConvexPoly(img, triangle, 255)
# cv2.fillConvexPoly(img, triangle, (255, 255, 255))
cv2.imshow("'asdf",img)
cv2.waitKey()
多通道也可以:
cv2.fillConvexPoly()函数可以用来填充凸多边形,只需要提供凸多边形的顶点即可.
我们来画一个三角形
img = np.zeros((1080, 1920, 3), np.uint8)
triangle = np.array([[0, 0], [1500, 800], [500, 400]])
cv2.fillConvexPoly(img, triangle, (255, 255, 255))
cv2.imshow("'asdf",img)
cv2.waitKey(
cv2.fillPoly()
cv2.fillPoly()函数可以用来填充任意形状的图型.可以用来绘制多边形,工作中也经常使用非常多个边来近似的画一条曲线.cv2.fillPoly()函数可以一次填充多个图型.
img = np.zeros((1080, 1920, 3), np.uint8)
area1 = np.array([[250, 200], [300, 100], [750, 800], [100, 1000]])
area2 = np.array([[1000, 200], [1500, 200], [1500, 400], [1000, 400]])
cv2.fillPoly(img, [area1, area2], (255, 255, 255))
cv2.imshow("asdf",img)
cv2.waitKey()