1

我正在做一个项目来跟踪激光和光电二极管,并将相机连接到树莓派上。pi 将向 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

4

1 回答 1

1

findContours 返回此元组(图像、轮廓、层次结构)。因此,在您的情况下,尝试将其作为 findContours 函数的 LHS:_, lines, pyramids = cv2.findContours

编辑:
对不起,这不是解决方案,下面的一个对我有用。
在 con2z 函数调用中用罐头替换 grayput。findContours 需要二值图像,而 grayput 不是。

于 2015-06-26T17:54:34.493 回答