首页 文章

OpenCV findContours只能找到图像的边框

提问于
浏览
1

我正在开展一个项目来跟踪激光器和光电二极管,相机连接到树莓派 . pi将向arduino发送指令,arduino将重新调整激光器直到我得到光电二极管的响应 . 现在,我正在研究这个过程的相机方面 .

我试图找到我的图像的轮廓,以便我可以将它们与我将要使用的对象的一般轮廓相匹配,但我的findContours()只给出了我图像的边框 .

我希望我可以发布图像,但我没有足够的代表 . Canny Edge是黑色和白色,白色线条,黑色背景 . 带有轮廓的图像是捕获的图像,但带有绘制的边框,没有其他轮廓 .

这是我的代码:

def DED(grayImg):                           #Edge Detection, returns image array

    minInt, maxInt, minLoc, maxLoc = cv2.minMaxLoc(grayImg) #Grayscale: MinIntensity, Max, and locations
    beam = cv2.mean(grayImg)    #Find the mean intensity in the img pls.
    mean = float(beam[0])
    CannyOfTuna = cv2.Canny(grayImg, (mean + minInt)/2, (mean + maxInt)/2)  #Finds edges using thresholding and the Canny Edge process.

    return CannyOfTuna


def con2z(Gray, ogImage):                           #Find contours from = Canny Edge Image, draw onto original
    lines, pyramids = cv2.findContours(Gray, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)


    gimmeGimme = cv2.drawContours(ogImage, lines, -1, (128,255,0), 3)       #draw contours on 
                #The -1 signifies ALL contours will be drawn.

    return lines

with picamera.PiCamera() as camera:
    camera.resolution = (640,480)
    out = camera.capture('output.jpg')                  # Camera start
    output = cv2.imread('output.jpg')

    grayput = cv2.cvtColor(output, cv2.COLOR_BGR2GRAY)      #Grayscale
    cv2.imwrite('gray.jpg', grayput)

    cans = DED(grayput)                                     #Canny Edge
    cv2.imwrite('Canny.jpg', cans)


    lines = con2z(grayput, output)                      # Contours please

    print(lines)
    cv2.imwrite('contours.jpg', output)

编辑:这是两张照片http://imgur.com/EVeMVdm,QLoYa2o#0 http://imgur.com/EVeMVdm,QLoYa2o#1

1 回答

  • 1

    findContours返回此元组(图像,轮廓,层次结构) . 所以在你的情况下,尝试使用你的findContours函数的L.H.S:_,lines,pyramids = cv2.findContours

    编辑:
    对不起,这不是解决方案,下面一个为我工作 .
    在con2z函数调用中用cans替换grayput . findContours期望二进制图像,而灰色图像不是 .

相关问题