在口罩上绘制轮廓

发布于 2025-01-26 13:06:05 字数 505 浏览 3 评论 0原文

我有一个带有口罩的图像;我在图像中找到了对象的轮廓。由于某种原因,当我调用cv2.drawContours()时,对象的轮廓将以灰色绘制。有什么方法可以在图像上绘制彩色线条?

以下是代码:

img = cv2.imread("Assets/Setup2.jpg")
hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
masked_img = cv2.inRange(hsv_img, (50, 40, 40), (70, 255, 255))
contours = cv2.findContours(masked_img, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)[0]
cv2.drawContours(masked_img, contours, -1, (60, 200, 200), 5)
cv2.imshow("Frame", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

I have an image with a mask; I have found the contour of the object in the image. For some reason, when I call cv2.drawContours(), the contour of the object is drawn in grey. Is there any way to draw colored lines on the image?

Below is the code:

img = cv2.imread("Assets/Setup2.jpg")
hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
masked_img = cv2.inRange(hsv_img, (50, 40, 40), (70, 255, 255))
contours = cv2.findContours(masked_img, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)[0]
cv2.drawContours(masked_img, contours, -1, (60, 200, 200), 5)
cv2.imshow("Frame", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愚人国度 2025-02-02 13:06:06

轮廓以灰色绘制,因为输入图像蒙版_IMG是单渠道图像。 CV2。Threshold或Cv22的输出返回单渠道图像,无论其输入如何。

这是您应该修复代码以获取所需效果的方法:

img = cv2.imread("Assets/Setup2.jpg")
hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
masked_img = cv2.inRange(hsv_img, (50, 40, 40), (70, 255, 255))
masked_img = cv2.cvtColor(masked_img, cv2.COLOR_GRAY2BGR)  // changes gray to BGR
contours = cv2.findContours(masked_img, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)[0]
cv2.drawContours(masked_img, contours, -1, (60, 200, 200), 5) 
cv2.imshow("Frame", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

或者您可以直接在输入图像上绘制轮廓。在这种情况下,您只需要更改CV22.DrawContour到以下代码

cv2.drawContours(img, contours, -1, (60, 200, 200), 5) 

The contour is drawn in gray because the input image masked_img is a single-channel image. Output from cv2.threshold or cv2.inRange returns single-channel image regardless of its inputs.

Here is how you should fix your code to get the desired effects:

img = cv2.imread("Assets/Setup2.jpg")
hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
masked_img = cv2.inRange(hsv_img, (50, 40, 40), (70, 255, 255))
masked_img = cv2.cvtColor(masked_img, cv2.COLOR_GRAY2BGR)  // changes gray to BGR
contours = cv2.findContours(masked_img, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)[0]
cv2.drawContours(masked_img, contours, -1, (60, 200, 200), 5) 
cv2.imshow("Frame", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

or you can draw the contour directly on your input image. In which case you only need to change the cv2.drawContour to the following code

cv2.drawContours(img, contours, -1, (60, 200, 200), 5) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文