David Tran
David Tran

Reputation: 325

OpenCV findContours only finds the border of the image

I'm working on a project to track a laser and a photodiode with a camera attached to a raspberry pi. The pi will send instructions to an arduino, which will reorient the laser until I get a response from the photodiode. Right now, I'm working on the camera aspect of the process.

I'm trying to find the contours of my image so that I can match them with the general contours of the objects I'll be using, but my findContours() only gives me the border of my image.

I wish I could post the images, but I don't have enough rep. The Canny Edge is black and white, white lines with a black background. The image with the contours on it is the captured image but with a drawn border and no other contours.

Here's my code:

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)

EDIT: Here are the two photos https://i.sstatic.net/Jd6o0.jpg https://i.sstatic.net/mAy2d.jpg

Upvotes: 2

Views: 4329

Answers (1)

Fourierist
Fourierist

Reputation: 36

findContours returns this tuple (image, contours, hierarchy). So in your case try this as L.H.S of your findContours function: _, lines, pyramids = cv2.findContours

EDIT:
Sorry, that was not the solution, below one worked for me.
Replace grayput with cans in con2z function call. findContours expects binary image, which grayput is not.

Upvotes: 2

Related Questions