Beispiel #1
0
def find_longest_contour(contour_seq):
	x = contour_seq
	max_len = 0
	max = None
	try:
		while x is not None:
			if cv.ArcLength(x) > max_len:
				max_len = cv.ArcLength(x)
				max = x
			x = x.h_next()
	except:
		pass
	return (max, max_len)
Beispiel #2
0
def get_contours(frame, approx=True):
    """Get contours from an image
    :: iplimage -> CvSeq
    """
    # A workaround for OpenCV 2.0 crash on receiving a (nearly) black image
    nonzero = cv.CountNonZero(frame)
    logging.debug("Segmentation got an image with %d nonzero pixels", nonzero)
    if nonzero < 20 or nonzero > 10000:
        return []

    storage = cv.CreateMemStorage(0)
    # find the contours
    contours = cv.FindContours(frame, storage, cv.CV_RETR_LIST,
                               cv.CV_CHAIN_APPROX_SIMPLE)
    if contours is None:
        return []

    res = []
    while contours:
        if not approx:
            result = contours
        else:
            result = cv.ApproxPoly(contours, storage, cv.CV_POLY_APPROX_DP,
                                   cv.ArcLength(contours) * 0.02, 1)
        res.append(result)
        contours = contours.h_next()

    return res
Beispiel #3
0
 def _get_approx(self, conts):
     '''
     Returns contur aproximation
     @param conts: conturs
     '''
     per = cv.ArcLength(conts)
     conts = cv.ApproxPoly(conts, cv.CreateMemStorage(),
                           cv.CV_POLY_APPROX_DP, per * 0.05)
     #cv.DrawContours(self.img, conts, (0,0,255), (0,255,0), 4)
     return list(conts)
def find_squares_from_binary( gray ):
    """
    use contour search to find squares in binary image
    returns list of numpy arrays containing 4 points
    """
    squares = []
    storage = cv.CreateMemStorage(0)
    contours = cv.FindContours(gray, storage, cv.CV_RETR_TREE, cv.CV_CHAIN_APPROX_SIMPLE, (0,0))  
    storage = cv.CreateMemStorage(0)
    while contours:
        #approximate contour with accuracy proportional to the contour perimeter
        arclength = cv.ArcLength(contours)
        polygon = cv.ApproxPoly( contours, storage, cv.CV_POLY_APPROX_DP, arclength * 0.02, 0)
        if is_square(polygon):
            squares.append(polygon[0:4])
        contours = contours.h_next()

    return squares
Beispiel #5
0
def get_contours(frame):
    """Get contours from an image
    :: iplimage -> CvSeq
    """
    storage = cv.CreateMemStorage(0)
    # find the contours
    contours = cv.FindContours( frame,
                                storage,
                                cv.CV_RETR_LIST,
                                cv.CV_CHAIN_APPROX_SIMPLE )
    if contours is None:
        return

    contours = cv.ApproxPoly( contours,
                              storage,
                              cv.CV_POLY_APPROX_DP,
                              cv.ArcLength(contours)*0.05,
                              1 )
    return contours
def shapeAnalysis(mask):

    height, width = mask.shape
    pixels = height * width

    # spatial and central moments
    moments = cv.Moments(mask, binary=1)
    huMoments = cv.GetHuMoments(moments)
    print "Shape hu moments", huMoments

    # distances from the gravity point
    contour_seq = cv.FindContours(np.array(mask), cv.CreateMemStorage(),
                                  cv.CV_RETR_TREE, cv.CV_CHAIN_APPROX_SIMPLE)
    gravity_center = (int(moments.m10 / moments.m00),
                      int(moments.m01 / moments.m00))  # (x, y)
    gx, gy = gravity_center
    distances = np.array(
        [math.sqrt((gx - x)**2 + (gy - y)**2) for (x, y) in contour_seq])
    dist_distri, bin_dist = np.histogram(distances,
                                         bins=10,
                                         range=None,
                                         normed=True)
    print "dist distribution", dist_distri
    dist_max = np.max(distances)
    dist_min = np.min(distances)
    dist_ratio_min_max = dist_min / dist_max
    print "dist ratio min max", dist_ratio_min_max
    dist_mean = np.mean(distances)
    dist_std = np.std(distances)

    # normalize distance min and max
    dist_max = dist_max / pixels
    dist_min = dist_min / pixels
    dist_mean = dist_mean / pixels
    dist_std = dist_std / pixels
    print "dist max", dist_max
    print "dist min", dist_min
    print "dist mean", dist_mean
    print "dist std", dist_std

    # number of petals
    nbPetals = np.sum([
        min(x1, x2) < dist_mean < max(x1, x2)
        for x1, x2 in zip(distances[:-1], distances[1:])
    ]) / 2
    print "petals", nbPetals

    poly_seq = cv.ApproxPoly(contour_seq, cv.CreateMemStorage(),
                             cv.CV_POLY_APPROX_DP, 2.8)
    ppimg = np.zeros(mask.shape)
    for (x, y) in poly_seq:
        ppimg[y, x] = 255
    imsave('/home/cplab/workspace/imageex/src/imageex/static/POLYYYAAAAA.png',
           ppimg)

    convex_hull = cv.ConvexHull2(poly_seq, cv.CreateMemStorage())
    convexity_defects = cv.ConvexityDefects(poly_seq, convex_hull,
                                            cv.CreateMemStorage())

    # number of defects
    nbDefects = len(convexity_defects)
    print "defects", nbDefects

    convexity_seq = sum([[cd[0], cd[2], cd[1]] for cd in convexity_defects],
                        [])
    ppimg = np.zeros(mask.shape)
    for (x, y) in convexity_seq:
        ppimg[y, x] = 255
    imsave('/home/cplab/workspace/imageex/src/imageex/static/CONVEXXAAAAA.png',
           ppimg)

    convexity_depths = np.array([cd[3] for cd in convexity_defects])
    convexity_depth_max = np.max(convexity_depths)
    convexity_depth_min = np.min(convexity_depths)
    convexity_depth_ratio_min_max = convexity_depth_min / convexity_depth_max
    print "convexity depth ratio min max", convexity_depth_ratio_min_max

    #normalize
    convexity_depth_max = convexity_depth_max / pixels

    print "convexity depth max", convexity_depth_max

    area = cv.ContourArea(contour_seq)
    perimeter = cv.ArcLength(contour_seq)
    perimeterOarea = perimeter / area
    print "perimeter over area", perimeterOarea

    features = []
    features += list(huMoments)
    features += dist_distri, dist_ratio_min_max, dist_max, dist_min, dist_mean, dist_std
    features += nbPetals, nbDefects
    features += convexity_depth_ratio_min_max, convexity_depth_max, perimeterOarea

    return features