gpt4 book ai didi

python - 如何在 OpenCV/python 上更改轮廓颜色

转载 作者:太空宇宙 更新时间:2023-11-03 22:02:56 25 4
gpt4 key购买 nike

我试图在此图像上绘制不同颜色的轮廓 ( https://docs.opencv.org/3.4.0/d4/d73/tutorial_py_contours_begin.html ),但轮廓总是白色的。下面是代码

import cv2
import numpy as np

img = cv2.imread(r'C:\Users\Ron Shporer\Desktop\TESTSAVES\TESTLines.png',0)

ret,thresh = cv2.threshold(img,127,255,0)
im2,contours,hierarchy = cv2.findContours(thresh, 1, 2)
cnt = contours[0]
cv2.drawContours(img, contours, -1, (255,0,0), 5)

cv2.namedWindow('img', cv2.WINDOW_NORMAL)
cv2.imshow('img',img)


cv2.waitKey(0)
cv2.destroyAllWindows()`

下图:

contours

最佳答案

解决方法很简单。您可以将输入图像转换为 3 channel 图像,然后在转换后的彩色图像上绘制轮廓。

代码应该是这样的:

import cv2
import numpy as np

img = cv2.imread(r'C:\Users\Ron Shporer\Desktop\TESTSAVES\TESTLines.png',0)

ret,thresh = cv2.threshold(img,127,255,0)
im2,contours,hierarchy = cv2.findContours(thresh, 1, 2)
cnt = contours[0]

result_img = cv2.cvtColor(frame, cv2.COLOR_GRAY2RGB)

cv2.drawContours(result_img, contours, -1, (255,0,0), 5)

cv2.namedWindow('img', cv2.WINDOW_NORMAL)
cv2.imshow('img', result_img)

cv2.waitKey(0)
cv2.destroyAllWindows()

关于python - 如何在 OpenCV/python 上更改轮廓颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51560541/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com