如何绘制对象的特定或多个轮廓

2024-04-19 16:49:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我似乎找不到一种方法来画出不止一个物体的轮廓。在

输入图像:

enter image description here

代码:

import cv2
import numpy as np

#import image
img = cv2.imread('img.png', 0)

#Thresh
ret, thresh = cv2.threshold(img, 200, 255, cv2.THRESH_BINARY)

#Finding the contours in the image
_, contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

#Convert img to RGB and draw contour
img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)
cv2.drawContours(img, contours, 0, (0,0,255), 2)

#save output img
cv2.imwrite('output_img.png', img)

输出:

enter image description here

只绘制较大的对象轮廓。我怎么画两个轮廓?在


Tags: the方法代码图像imageimportnumpyimg
1条回答
网友
1楼 · 发布于 2024-04-19 16:49:06

drawContours中的第三个参数更改为-1(第三个参数是等高线的索引),这将绘制图像中的所有轮廓:

cv2.drawContours(img, contours, -1, (0,0,255), 2)

如果只想绘制两个轮廓,并且前两个轮廓是白色对象,请使用:

^{pr2}$

相关问题 更多 >