本文介绍了三角形填充opencv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

opencv中有Rectangle填充,圆形和椭圆形填充,但是谁能说出如何使用opencv,python填充图像中的三角形.

We have Rectangle fillings , circle and ellipse filling in opencv, but can anyone say how to fill a triangle in an image using opencv ,python.

推荐答案

填充三角形形状的最简单解决方案是在OpenCV中使用绘制轮廓函数.假设我们知道三角形的三个点为"pt1","pt2"和"pt3":

The simplest solution of filling a triangle shape is using draw contour function in OpenCV. Assuming we know the three points of the triangle as "pt1", "pt2" and "pt3":

import cv2
import numpy as np

image = np.ones((300, 300, 3), np.uint8) * 255

pt1 = (150, 100)
pt2 = (100, 200)
pt3 = (200, 200)

cv2.circle(image, pt1, 2, (0,0,255), -1)
cv2.circle(image, pt2, 2, (0,0,255), -1)
cv2.circle(image, pt3, 2, (0,0,255), -1)

我们可以将三个点放入一个数组中并绘制轮廓:

We can put the three points into an array and draw as a contour:

triangle_cnt = np.array( [pt1, pt2, pt3] )

cv2.drawContours(image, [triangle_cnt], 0, (0,255,0), -1)

cv2.imshow("image", image)
cv2.waitKey()

这是输出图像.干杯.

Here is the output image. Cheers.

这篇关于三角形填充opencv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 22:52