Exemple #1
0
    avgsize = int(result / numblobs)
    print str(numblobs) + " blobs found covering " + str(result) + "px"
    filtered = cv.CreateImage(cv.GetSize(img), cv.IPL_DEPTH_8U, 1)
    cvblob.FilterLabels(labelImg, filtered, blobs)

    # Largest Blob size
    bigblob = cvblob.GreaterBlob(blobs)
    print "largest blob is " + str(bigblob) + " which is " + str(
        blobs[bigblob].area) + " px"

    bigblobs = copy.copy(blobs)
    cvblob.FilterByLabel(bigblobs, bigblob)
    #print str(len(bigblobs.keys())) + " blobs with label " + str(bigblob)

    # find centroid of largest blob
    centroid = cvblob.Centroid(bigblobs[bigblob])
    print "centroid of blob " + str(bigblob) + " is " + str(centroid)

    imgOut = cv.CreateImage(cv.GetSize(img), cv.IPL_DEPTH_8U, 3)
    cv.Zero(imgOut)
    cvblob.RenderBlobs(
        labelImg, blobs, img, imgOut,
        cvblob.CV_BLOB_RENDER_COLOR | cvblob.CV_BLOB_RENDER_CENTROID
        | cvblob.CV_BLOB_RENDER_BOUNDING_BOX | cvblob.CV_BLOB_RENDER_ANGLE,
        1.0)

    print "mean color for blob " + str(bigblob) + " is " + str(
        cvblob.BlobMeanColor(blobs[bigblob], labelImg, img))

    #cv.ShowImage("test_filtered", filtered)
    cv.ShowImage("Original", img)
def findBlobs(img, min_color, max_color, window, renderedwindow, location,
              area):
    blobs = cvblob.Blobs()  #initializes the blobs class
    size = cv.GetSize(img)  #gets size of image

    hsv = cv.CreateImage(size, cv.IPL_DEPTH_8U, 3)  #New HSV image for alter
    thresh = cv.CreateImage(size, cv.IPL_DEPTH_8U,
                            1)  #New Gray Image for later
    labelImg = cv.CreateImage(size, cvblob.IPL_DEPTH_LABEL,
                              1)  #New Blob image for later

    cv.CvtColor(img, hsv, cv.CV_BGR2HSV)  #converts image to hsv image
    cv.InRangeS(hsv, min_color, max_color, thresh)  #thresholds it

    #Corrections to remove false positives
    cv.Smooth(thresh, thresh, cv.CV_BLUR)
    cv.Dilate(thresh, thresh)
    cv.Erode(thresh, thresh)

    result = cvblob.Label(thresh, labelImg,
                          blobs)  #extracts blobs from a greyscale image

    numblobs = len(
        blobs.keys())  #number of blobs based off of length of blobs dictionary

    #if there are blobs found
    if (numblobs > 0):
        avgsize = int(result / numblobs)
        print str(numblobs) + " blobs found covering " + str(result) + "px"

        #Removes blobs that are smaller than a certain size based off of average size
        remv = []
        for x in blobs:
            if (blobs[x].area < avgsize / 3):
                remv.append(x)
        for x in remv:
            del blobs[x]

        numblobs = len(
            blobs.keys())  #gets the number of blobs again after removing some
        print str(numblobs) + " blobs remaining"

        filtered = cv.CreateImage(cv.GetSize(img), cv.IPL_DEPTH_8U, 1)
        cvblob.FilterLabels(
            labelImg, filtered, blobs
        )  #Creates a binary image with the blobs formed (imgIn, imgOut, blobs)

        #Centroid, area, and circle for all blobs
        for blob in blobs:
            location.append(cvblob.Centroid(blobs[blob]))
            area.append(blobs[blob].area)
            cv.Circle(img, (int(cvblob.Centroid(
                blobs[blob])[0]), int(cvblob.Centroid(blobs[blob])[1])),
                      int(math.sqrt(int(blobs[blob].area) / 3.14)) + 25,
                      cv.Scalar(0, 0, 0))

        imgOut = cv.CreateImage(cv.GetSize(img), cv.IPL_DEPTH_8U, 3)
        cv.Zero(imgOut)

        cvblob.RenderBlobs(
            labelImg, blobs, img, imgOut,
            cvblob.CV_BLOB_RENDER_COLOR | cvblob.CV_BLOB_RENDER_CENTROID
            | cvblob.CV_BLOB_RENDER_BOUNDING_BOX | cvblob.CV_BLOB_RENDER_ANGLE,
            1.0)  #Marks up the blobs image to put bounding boxes, etc on it

        cv.ShowImage("Window", img)  #shows the orininalimage
        cv.ShowImage("Rendered", imgOut)  #shows the blobs image

        return blobs  #returns the list of blobs

    else:
        print " ...Zero blobs found. \nRedifine color range for better results"  #if no blobs were found print an error message

        cv.ShowImage("Window", img)  #show the original image