在口罩上绘制轮廓
我有一个带有口罩的图像;我在图像中找到了对象的轮廓。由于某种原因,当我调用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
轮廓以灰色绘制,因为输入图像蒙版_IMG是单渠道图像。 CV2。Threshold或Cv22的输出返回单渠道图像,无论其输入如何。
这是您应该修复代码以获取所需效果的方法:
或者您可以直接在输入图像上绘制轮廓。在这种情况下,您只需要更改CV22.DrawContour到以下代码
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:
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