我有下图:
我使用以下代码勾勒出此图像中使用此代码的所有圆形斑点:import numpy as np
import cv2
im = cv2.imread('im.jpg')
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,200,255,0)
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(im,contours,-1,(0,0,255),1)
#(B,G,R)
cv2.imshow('image',im)
cv2.waitKey(0)
cv2.destroyAllWindows()
它产生了这样的图像:
这对第一步很好。但我真的很难为蓝色的斑点画出不同颜色的轮廓。我试过使用多个轮廓:import numpy as np
import cv2
im = cv2.imread('im.jpg')
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,200,255,0)
ret, thresh2 = cv2.threshold(imgray,130,255,0)
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
contours2, hierarchy2 = cv2.findContours(thresh2,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(im,contours,-1,(0,0,255),1)
cv2.drawContours(im,contours2,-1,(0,255,0),1)
#(B,G,R)
cv2.imshow('image',im)
cv2.waitKey(0)
cv2.destroyAllWindows()
图像显示如下:
这种方法的第一个问题是它不能准确地只勾勒出蓝色的斑点。此外,threshold函数中的灵敏度等级必须根据照明等情况对每个图像进行修改。是否有更平滑的方法来做到这一点?