如何解决灰度图像上的颜色轮廓
我编写了一个简单的代码来查找图像中对象的轮廓。
img = cv2.imread(file_path,cv2.IMREAD_GRAYSCALE)
blur = cv2.GaussianBlur(img_enhanced,(25,25),0) # apply blur for contour
ret,binary = cv2.threshold(blur,1,255,cv2.THRESH_BINARY) # apply threshold to blur image
contours,hierarchy = cv2.findContours(binary,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) # find countour
obj_index = contours.index(max(contours,key=len)) # find index of largest object
contour_img = cv2.drawContours(img,contours,obj_index,(0,0),3) # draw coutour on original image
plt.imshow(contour_img)
plt.show()
原始图像已经是灰度图像,但无论如何我在导入图像时都应用了cv2.IMREAD_GRAYSCALE。
我想如果我使用以下语法在“绘制”轮廓时应用灰度图像,
contour_img = cv2.drawContours(img,3)
我可以得到带有彩色轮廓的灰度图像,但是它显示 奇怪的彩色图像。
如何在灰度图像上绘制彩色轮廓线?
预先感谢
图像样本:黑色背景和白色物体
解决方法
根据我的评论:
尝试类似的方法,并告诉我是否可行
编辑:我更改了阈值范围。 cv2.threshold(blur,25,255,cv2.THRESH_BINARY)
img = cv2.imread(file_path,1)
print(img.shape) # this should give you (img_h,img_w,3)
img2 = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(img2,(25,25),0) # apply blur for contour
ret,binary = cv2.threshold(blur,cv2.THRESH_BINARY) # apply threshold to blur image
contours,hierarchy = cv2.findContours(binary,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) # find countour
obj_index = contours.index(max(contours,key=len)) # find index of largest object
contour_img = cv2.drawContours(img,contours,obj_index,(0,0),3) # draw coutour on original image
plt.imshow(contour_img,cmap='gray')
plt.show()
plt.hist(blur.ravel(),bins=50)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。