Ejemplo n.º 1
0
def test_moments_hu():
    image = np.zeros((20, 20), dtype=np.double)
    image[13:15, 13:17] = 1
    mu = moments_central(image, 13.5, 14.5)
    nu = moments_normalized(mu)
    hu = moments_hu(nu)
    # shift image by dx=2, dy=3, scale by 0.5 and rotate by 90deg
    image2 = np.zeros((20, 20), dtype=np.double)
    image2[11, 11:13] = 1
    image2 = image2.T
    mu2 = moments_central(image2, 11.5, 11)
    nu2 = moments_normalized(mu2)
    hu2 = moments_hu(nu2)
    # central moments must be translation and scale invariant
    assert_almost_equal(hu, hu2, decimal=1)
Ejemplo n.º 2
0
def test_moments_hu():
    image = np.zeros((20, 20), dtype=np.double)
    image[13:15, 13:17] = 1
    mu = moments_central(image, (13.5, 14.5))
    nu = moments_normalized(mu)
    hu = moments_hu(nu)
    # shift image by dx=2, dy=3, scale by 0.5 and rotate by 90deg
    image2 = np.zeros((20, 20), dtype=np.double)
    image2[11, 11:13] = 1
    image2 = image2.T
    mu2 = moments_central(image2, (11.5, 11))
    nu2 = moments_normalized(mu2)
    hu2 = moments_hu(nu2)
    # central moments must be translation and scale invariant
    assert_almost_equal(hu, hu2, decimal=1)
Ejemplo n.º 3
0
def get_hu_moment_from_image(image):
    """
    Compute the 7 Hu's moments from an image.
    This set of moments is proofed to be translation, scale and rotation invariant.

    Parameters
    ----------
    image: array-like
        a 2d array of double or uint8 corresponding to an image

    Returns
    -------
    (7, 1) array of double
        7 Hu's moments

    References
    ----------
    http://scikit-image.org/docs/dev/api/skimage.measure.html#skimage.measure.moments
    """
    order = 7
    raw_moments = moments(image, order=order)
    cr = raw_moments[0, 1] / raw_moments[0, 0]
    cc = raw_moments[1, 0] / raw_moments[0, 0]
    central_moments = moments_central(image, cr, cc, order=order)
    normalized_moments = moments_normalized(central_moments, order)
    hu_moments = moments_hu(normalized_moments)
    return hu_moments
Ejemplo n.º 4
0
def collect(path, mean, std):
    img = io.imread('./images/' + path + '.bmp')
    hist = exposure.histogram(img)
    th = get_threshold('./images/' + path + '.bmp')
    img_binary = (img < th).astype(np.double)
    img_label = label(img_binary, background=0)
    regions = regionprops(img_label)
    boxes = []
    features = []
    for props in regions:
        box = []
        minr, minc, maxr, maxc = props.bbox
        if maxc - minc < 10 or maxr - minr < 10 or maxc - minc > 120 or maxr - minr > 120:
            continue
        box.append(minr)
        box.append(maxr)
        box.append(minc)
        box.append(maxc)
        boxes.append(box)

        roi = img_binary[minr:maxr, minc:maxc]
        m = moments(roi)
        cr = m[0, 1] / m[0, 0]
        cc = m[1, 0] / m[0, 0]
        mu = moments_central(roi, cr, cc)
        nu = moments_normalized(mu)
        hu = moments_hu(nu)
        features.append(hu)

    feature_arr = normalize(features, mean, std)
    return (boxes, feature_arr)
Ejemplo n.º 5
0
	def __compute_moments(self, data, centroid, radius):
		""" Compute moments"""

		# - Compute central moments
		mom_c= moments_central(data, center=centroid, order=3)

		# - Compute normalized moments
		mom_norm= moments_normalized(mom_c, 3)

		# - Compute Hu moments
		mom_hu= moments_hu(mom_norm)

		# - Flatten moments
		mom_c= mom_c.flatten()

		# - Compute Zernike moments
		#   NB: mahotas takes only positive pixels and rescale image by sum(pix) internally
		poldeg= 4
		nmom_zernike= 9
		mom_zernike= [-999]*nmom_zernike
		try:
			mom_zernike = mahotas.features.zernike_moments(data, radius, degree=poldeg, cm=centroid)
			##mom_zernike = mahotas.features.zernike_moments(mask, radius, degree=poldeg, cm=centroid)
		except Exception as e:
			logger.warn("Failed to compute Zernike moments (err=%s)!" % (str(e)))

		#print("--> mom_zernike")
		#print(mom_zernike)
		
		return (mom_c, mom_hu, mom_zernike)
Ejemplo n.º 6
0
def get_hu_moments(samples):
    print "getting hu moments..."
    features = []
    for sample in samples:
        '''
        sample = np.array(sample)
        th = 200
        img_binary = (sample < th).astype(np.double)
        img_label = label(img_binary, background=255)
        regions = regionprops(img_label)
        if regions == []:
            print "no regions"
        for props in regions:
            minr, minc, maxr, maxc = props.bbox
            roi = img_binary[minr:maxr, minc:maxc]

        '''
        sample = np.array(sample)
        sample = sample.astype(np.double)
        m = moments(sample)
        cr = m[0, 1] / m[0, 0]
        cc = m[1, 0] / m[0, 0]
        mu = moments_central(sample, cr, cc)
        nu = moments_normalized(mu)
        hu = moments_hu(nu)
        features.append(hu)
    return features
def extract_features(roi, props):

    features = []

    m = moments(roi)
    # print(m)

    cr = m[0, 1] / m[0, 0]
    cc = m[1, 0] / m[0, 0]

    mu = moments_central(roi, (cr, cc))
    nu = moments_normalized(mu)

    #finding Seven Features
    hu = moments_hu(nu)

    # seven features to be put into feature list
    features.extend(hu)

    # print(features)

    features.append(roi.shape[1]/roi.shape[0])
    features.append(props.eccentricity)
    features.append(props.convex_area/props.area)
    features.append(props.orientation)
    features.append(props.euler_number)
    
    return np.array([features])
Ejemplo n.º 8
0
def featuresExtractor_Hu(image):
    img = rgb2gray(image)
    hu = moments_central(img)
    hu = moments_normalized(hu)
    hu = moments_hu(hu)
    l = [norm(f) for f in hu]
    return l
Ejemplo n.º 9
0
def get_data(x, y, img):

    # Create data and 5x5 image slice
    data = []
    new_image = img[x - 2:x + 2, y - 2:y + 2]

    # Append location of pixel
    data.append(x)
    data.append(y)

    # Append pixel values to data
    for pixel_val in new_image.ravel():
        data.append(pixel_val)

    # Append central moments to data
    central_moments = measure.moments_central(new_image)
    for central_moment in central_moments.ravel():
        data.append(central_moment)

    # Append hu moments to data
    hu_moments = measure.moments_hu(
        measure.moments_normalized(central_moments))
    for hu_moment in hu_moments:
        data.append(hu_moment)

    # Append variation of pixel values to data
    variation = np.var(new_image)
    data.append(variation)

    return data
Ejemplo n.º 10
0
Archivo: hu.py Proyecto: rgaiacs/pythia
def hist(image):
    """Create histogram"""
    return moments_hu(
        moments_normalized(
            moments_central(image)
        )
    )
Ejemplo n.º 11
0
def moments_hu(nu):
    """Calculate Hu's set of image moments (2D-only).

    Note that this set of moments is proofed to be translation, scale and
    rotation invariant.

    Parameters
    ----------
    nu : (M, M) array
        Normalized central image moments, where M must be >= 4.

    Returns
    -------
    nu : (7,) array
        Hu's set of image moments.

    Notes
    -----
    Due to the small array sizes, this function will be faster on the CPU.
    Consider transfering ``nu`` to the host and running
    ``skimage.measure.moments_hu`` if the moments are not needed on the
    device.

    References
    ----------
    .. [1] M. K. Hu, "Visual Pattern Recognition by Moment Invariants",
           IRE Trans. Info. Theory, vol. IT-8, pp. 179-187, 1962
    .. [2] Wilhelm Burger, Mark Burge. Principles of Digital Image Processing:
           Core Algorithms. Springer-Verlag, London, 2009.
    .. [3] B. Jähne. Digital Image Processing. Springer-Verlag,
           Berlin-Heidelberg, 6. edition, 2005.
    .. [4] T. H. Reiss. Recognizing Planar Objects Using Invariant Image
           Features, from Lecture notes in computer science, p. 676. Springer,
           Berlin, 1993.
    .. [5] https://en.wikipedia.org/wiki/Image_moment

    Examples
    --------
    >>> import cupy as cp
    >>> from cucim.skimage.measure import (moments_central, moments_hu,
    ...                                      moments_normalized)
    >>> image = cp.zeros((20, 20), dtype=np.float64)
    >>> image[13:17, 13:17] = 0.5
    >>> image[10:12, 10:12] = 1
    >>> mu = moments_central(image)
    >>> nu = moments_normalized(mu)
    >>> moments_hu(nu)
    array([7.45370370e-01, 3.51165981e-01, 1.04049179e-01, 4.06442107e-02,
           2.64312299e-03, 2.40854582e-02, 4.33680869e-19])
    """
    try:
        from skimage.measure import moments_hu
    except ImportError:
        raise ImportError("moments_hu requires scikit-image.")

    # CuPy Backend: TODO: Due to small arrays involved, just transfer to/from
    #                     the CPU implementation.
    float_dtype = nu.dtype if nu.dtype.kind == 'f' else cp.float64
    return cp.asarray(moments_hu(cp.asnumpy(nu)), dtype=float_dtype)
Ejemplo n.º 12
0
def extract_features(path, show, tag):
    img = io.imread('./images/' + path + '.bmp')
    hist = exposure.histogram(img)
    th = get_threshold('./images/' + path + '.bmp')
    img_binary = (img < th).astype(np.double)
    img_label = label(img_binary, background=0)

    # Show images
    if show == 1:
        io.imshow(img)
        plt.title('Original Image')
        io.show()

        plt.bar(hist[1], hist[0])
        plt.title('Histogram')
        plt.show()

        io.imshow(img_binary)
        plt.title('Binary Image')
        io.show()

        io.imshow(img_label)
        plt.title('Labeled Image')
        io.show()

    regions = regionprops(img_label)
    if show == 1:
        io.imshow(img_binary)
        ax = plt.gca()

    features = []

    for props in regions:
        minr, minc, maxr, maxc = props.bbox
        if maxc - minc < 10 or maxr - minr < 10 or maxc - minc > 120 or maxr - minr > 120:
            continue
        if show == 1:
            ax.add_patch(
                Rectangle((minc, minr),
                          maxc - minc,
                          maxr - minr,
                          fill=False,
                          edgecolor='red',
                          linewidth=1))
        roi = img_binary[minr:maxr, minc:maxc]
        m = moments(roi)
        cr = m[0, 1] / m[0, 0]
        cc = m[1, 0] / m[0, 0]
        mu = moments_central(roi, cr, cc)
        nu = moments_normalized(mu)
        hu = moments_hu(nu)
        features.append(hu)
        if (len(path) == 1):
            tag.append(ord(path))

    if show == 1:
        plt.title('Bounding Boxes')
        io.show()
    return features
Ejemplo n.º 13
0
 def compute_hu_moments(i):
     b = cells_aligned_padded[i].astype(np.uint8)
     m = moments(b, order=1)
     hu = moments_hu(
         moments_normalized(
             moments_central(b, cc=m[0, 1] / m[0, 0],
                             cr=m[1, 0] / m[0, 0])))
     return hu
Ejemplo n.º 14
0
def test_moments_hu_dtype(dtype):
    image = np.zeros((20, 20), dtype=np.double)
    image[13:15, 13:17] = 1
    mu = moments_central(image, (13.5, 14.5))
    nu = moments_normalized(mu)
    hu = moments_hu(nu.astype(dtype))

    assert hu.dtype == dtype
Ejemplo n.º 15
0
def get_moments(image: np.ndarray, position: Tuple[int, int]):
    x, y = position[0], position[1]
    d = 3
    part = image[x - d:x + d + 1, y - d:y + d + 1]
    h = moments_hu(part)
    # print(h)
    c = moments_central(part, order=4)
    # print(c)
    return *(c.reshape((25, 1))), *h
Ejemplo n.º 16
0
    def describe(self, image):

        #calculate daisy feature descriptors
        mc = measure.moments_central(image)
        mn = measure.moments_normalized(mc)
        mh = measure.moments_hu(mn)

        # return Hu moments
        return mh
Ejemplo n.º 17
0
def testKNN():
    trainFeatures, trainLebels = extractFeatures()
    knn = neighbors.KNeighborsClassifier()
    knn.fit(trainFeatures, trainLebels)
    #score = knn.score(trainFeatures, trainLebels)
    testNames = ['test1', 'test2']
    #testNames = ['test2']
    testFeatures = []
    testLabels = []
    testTruth = []
    correct = 0
    #textPosition = []
    for i in range(len(testNames)):
        classes, locations = readPkl(testNames[i])
        img = io.imread(testNames[i] + '.bmp')
        #testTruth = ['a']*7+['d']*7+['m']*7+['n']*7+['o']*7+['p']*7+['q']*7+['r']*7+['u']*7+['w']*7
        ret, binary = cv.threshold(img, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
        #ret, binary = cv.threshold(img, 0, 255, cv.THRESH_BINARY | cv.THRESH_TRIANGLE)
        th = ret
        img_binary = (img < th).astype(np.double)
        img_dilation = morphology.binary_dilation(img_binary, selem=None)
        img_erosion = morphology.binary_erosion(img_binary, selem=None)
        img_label = label(img_binary, background=0)
        regions = regionprops(img_label)
        io.imshow(img_binary)
        ax = plt.gca()
        thresholdR = 15
        thresholdC = 15
        for props in regions:
            minr, minc, maxr, maxc = props.bbox
            # Computing Hu Moments and Removing Small Components
            if (maxr - minr) >= thresholdR and (maxc - minc) >= thresholdC:
                #textPosition.append((maxc, minr))
                roi = img_binary[minr:maxr, minc:maxc]
                m = moments(roi)
                cr = m[0, 1] / m[0, 0]
                cc = m[1, 0] / m[0, 0]
                mu = moments_central(roi, cr, cc)
                nu = moments_normalized(mu)
                hu = moments_hu(nu)
                testFeatures.append(hu)
                
                testLabels.append(knn.predict([testFeatures[-1]]))
                
                indexFix = locationFix(locations, minr, minc, maxr, maxc)
                if indexFix is not None:
                    if testLabels[-1] == classes[indexFix]:
                        correct += 1
                
                plt.text(maxc, minr, testLabels[-1][0], bbox=dict(facecolor='white', alpha=0.5))
                ax.add_patch(Rectangle((minc, minr), maxc - minc, maxr - minr, fill=False, edgecolor='red', linewidth=1))
        plt.title('Bounding Boxes')
        io.show()
    print correct, len(testLabels)
    correctRate = correct / len(testLabels)
    print correctRate
def extractFeature(name, showall, showbb, flag):

    (img, regions, ax, rthre, cthre) = extractImage(name, showall, showbb,
                                                    flag)

    Features = []
    boxes = []

    for props in regions:
        tmp = []
        minr, minc, maxr, maxc = props.bbox
        if maxc - minc < cthre or maxr - minr < rthre or maxc - minc > cthre * 9 or maxr - minr > rthre * 9:
            continue
        tmp.append(minr)
        tmp.append(minc)
        tmp.append(maxr)
        tmp.append(maxc)
        boxes.append(tmp)
        if showbb == 1:
            ax.add_patch(
                Rectangle((minc, minr),
                          maxc - minc,
                          maxr - minr,
                          fill=False,
                          edgecolor='red',
                          linewidth=1))
        # computing hu moments and removing small components
        roi = img[minr:maxr, minc:maxc]
        m = moments(roi)
        cr = m[0, 1] / m[0, 0]
        cc = m[1, 0] / m[0, 0]
        mu = moments_central(roi, cr, cc)
        nu = moments_normalized(mu)
        hu = moments_hu(nu)

        area = (maxr - minr) * (maxc - minc)
        # add convexity
        p = perimeter(img[minr:maxr, minc:maxc])
        con = (area / (p * p)) * 4 * math.pi
        convex = np.array([con])
        hu = np.concatenate((hu, convex))

        # add density
        den = area / float(props.convex_area)
        dense = np.array([den])
        hu = np.concatenate((hu, dense))

        Features.append(hu)

    # print boxes

    plt.title('Bounding Boxes')
    if showbb == 1:
        io.show()

    return Features, boxes,
Ejemplo n.º 19
0
    def extract_features(image):
        """this method applies the hu moments and the haralick method on a given image

        Arguments:
            image {ndarray} -- the image whose the features will be extracted

        Returns:
            numpy.array -- an array with the extracted features
        """
        return np.r_[moments_hu(image), haralick(image).flatten()]
Ejemplo n.º 20
0
 def _describe(self, binary, steps=None):
     clipped = binary.clip(max=1)
     m = measure.moments(clipped)
     cr = m[0, 1] / m[0, 0]
     cc = m[1, 0] / m[0, 0]
     central = measure.moments_central(clipped, cr, cc)
     normalized = measure.moments_normalized(central)
     moments = measure.moments_hu(normalized)
     # nan determines, that moment could not be described,
     # but is hard to handle in prediction, set to zero instead
     moments[np.isnan(moments)] = 0
     return moments
Ejemplo n.º 21
0
def match_shapes(img_a: ndarray, img_b: ndarray):
    ''' 
        This function takes in input two images and returns
        the distances between the images using the HU moments.
    '''

    # calculating the hu moments
    hu_a = moments_hu(moments_normalized(moments_central(img_a)))
    hu_b = moments_hu(moments_normalized(moments_central(img_b)))

    # changing to log scale
    hu_a = -1 * sign(hu_a) * log10(abs(hu_a))
    hu_b = -1 * sign(hu_b) * log10(abs(hu_b))

    # calculating 3 distaces
    d1 = sum(abs((1 / hu_b) - (1 / hu_a)))
    d2 = sum(abs(hu_b - hu_a))
    d3 = sum(divide(abs(hu_a - hu_b), abs(hu_a)))

    # returning the distances
    return d1, d2, d3
Ejemplo n.º 22
0
def extract_features(img):
    # This function extract our features out of an image. It basically
    # computes the 8 (and not 7) Hu geometrical moments. To do this we
    # first compute the moments, centralize and normalize them before
    # computing Hu moments
    m = moments(img)
    cr = m[0,1] / m[0,0]
    cc = m[1,0] / m[0,0]
    mc = moments_central(img, cr, cc)
    mn = moments_normalized(mc)
    hu = moments_hu(mn)
    i8 = mn[1, 1] * ( (mn[3, 0] + mn[1, 2])**2 - (mn[0,3]+mn[2,1])**2 ) - (mn[2,0] - mn[0,2]) * (mn[3,0] + mn[1,2]) * (mn[0,3] + mn[2,1])
    return append(hu, [i8])
Ejemplo n.º 23
0
def test_moments_dtype(dtype):
    image = np.zeros((20, 20), dtype=dtype)
    image[13:15, 13:17] = 1

    expected_dtype = _supported_float_type(image)
    mu = moments_central(image, (13.5, 14.5))
    assert mu.dtype == expected_dtype

    nu = moments_normalized(mu)
    assert nu.dtype == expected_dtype

    hu = moments_hu(nu)
    assert hu.dtype == expected_dtype
Ejemplo n.º 24
0
def extract_features(roi, props):
    features = []
    m = moments(roi)
    cr = m[0, 1] / m[0, 0]
    cc = m[1, 0] / m[0, 0]
    mu = moments_central(roi, cr, cc)
    nu = moments_normalized(mu)
    hu = moments_hu(nu)
    features.extend(hu)
    features.append(roi.shape[1] / roi.shape[0])
    features.append(props.eccentricity)
    features.append(props.convex_area / props.area)
    features.append(props.orientation)
    features.append(props.euler_number)

    return np.array([features])
Ejemplo n.º 25
0
def moments_features(image):
    """calculates the set of moments for each channel
    
    Calculates the intertia tensor, intertia tensor eigenvalues, as well as 
    the moments of the image (https://en.wikipedia.org/wiki/Image_moment)

    Parameters
    ----------
    image : 3D array, shape (M, N, C)
        The input image with multiple channels.

    Returns
    -------
    features :  dict  
        dictionary including percentiles, moments and sum per channel 

    """
    # storing the feature values
    features = dict()
    for ch in range(image.shape[2]):
        hu_moments = moments_hu(image[:, :, ch])
        for i in range(len(hu_moments)):
            features["moments_hu_" + str(i + 1) + "_Ch" +
                     str(ch + 1)] = hu_moments[i]

        inertia_tensor_calculated = inertia_tensor(image[:, :, ch]).ravel()
        features["inertia_tensor_1_ch" +
                 str(ch + 1)] = inertia_tensor_calculated[0]
        features["inertia_tensor_2_ch" +
                 str(ch + 1)] = inertia_tensor_calculated[1]
        features["inertia_tensor_3_ch" +
                 str(ch + 1)] = inertia_tensor_calculated[3]

        inertia_tensor_eigvalues = inertia_tensor_eigvals(image[:, :, ch])
        features["inertia_tensor_eigvalues_1_ch" +
                 str(ch + 1)] = inertia_tensor_eigvalues[0]
        features["inertia_tensor_eigvalues_2_ch" +
                 str(ch + 1)] = inertia_tensor_eigvalues[1]

        the_moments = moments(image[:, :, ch], order=5).ravel()

        for i in range(len(the_moments)):
            features["moments_" + str(i + 1) + "_Ch" +
                     str(ch + 1)] = the_moments[i]

    return features
Ejemplo n.º 26
0
def apply_threshold(img, fname, classes, locations, Features):
    th = 230
    img_binary = (img < th).astype(np.double)
    io.imshow(img_binary)
    plt.title('Binary Image')
    io.show()

    img_label = label(img_binary, background=0)
    io.imshow(img_label)
    plt.title('Labeled Image')
    io.show()
    print np.amax(img_label)

    regions = regionprops(img_label)
    io.imshow(img_binary)
    ax = plt.gca()

    ypred = []

    for props in regions:
        minr, minc, maxr, maxc = props.bbox
        ax.add_patch(
            Rectangle((minc, minr),
                      maxc - minc,
                      maxr - minr,
                      fill=False,
                      edgecolor='red',
                      linewidth=1))
        roi = img_binary[minr:maxr, minc:maxc]
        m = moments(roi)
        cr = m[0, 1] / m[0, 0]
        cc = m[1, 0] / m[0, 0]
        mu = moments_central(roi, cr, cc)
        nu = moments_normalized(mu)
        hu = moments_hu(nu)
        pred_coord = np.array(list(props.centroid))
        ypred.append(np.flipud(pred_coord))

    ax.set_title('Bounding Boxes')
    plt.savefig('bounding_boxes_image_' + fname + '.png')
    io.show()

    t = np.array(ypred)
    x = t.astype(int)
    D = cdist(x, locations)
    print_matrix(D)
Ejemplo n.º 27
0
    def _hu_moments(roi: coo_matrix) -> np.ndarray:
        """Returns the 7 Hu moments for an ROI image. See 
        https://scikit-image.org/docs/0.17.x/api/skimage.measure.html#moments-hu        # noqa
        for more information.

        Returns
        -------
        7-element, 1d np.array of Hu's image moments

        References
        ----------
        M. K. Hu, “Visual Pattern Recognition by Moment Invariants”, 
        IRE Trans. Info. Theory, vol. IT-8, pp. 179-187, 1962
        """
        roi_image = roi.toarray()
        mu = moments_central(roi_image)
        nu = moments_normalized(mu)
        return moments_hu(nu)
Ejemplo n.º 28
0
def apply_threshold(img, fname, show, th_value):
    global Features, label_img
    th = th_value
    img_binary = (img < th).astype(np.double)
    if show[2]:
        io.imshow(img_binary)
        plt.title('Binary Image')
        io.show()
    img_label = label(img_binary, background=0)
    if show[3]:
        io.imshow(img_label)
        plt.title('Labeled Image')
        io.show()
    print fname + str(th)
    print np.amax(img_label)

    regions = regionprops(img_label)
    io.imshow(img_binary)
    ax = plt.gca()
    for props in regions:
        minr, minc, maxr, maxc = props.bbox
        ax.add_patch(
            Rectangle((minc, minr),
                      maxc - minc,
                      maxr - minr,
                      fill=False,
                      edgecolor='red',
                      linewidth=1))
        roi = img_binary[minr:maxr, minc:maxc]
        m = moments(roi)
        cr = m[0, 1] / m[0, 0]
        cc = m[1, 0] / m[0, 0]
        mu = moments_central(roi, cr, cc)
        nu = moments_normalized(mu)
        hu = moments_hu(nu)
        Features.append(hu)
        label_img.append(fname)

    if show[4]:
        ax.set_title('Bounding Boxes')
        plt.savefig('bounding_boxes_image_' + fname + '.png')
        io.show()
    else:
        ax.cla()
Ejemplo n.º 29
0
    def calculate_features(self, feature_mode, debug=False):

        fruit_image = io.imread(self.path, as_gray=True)

        sigma = 0.005 * fruit_image.shape[0]
        filtered_fruit = filters.gaussian(fruit_image, sigma=sigma)

        # Apply triangle threshold to gaussian filtered image
        self.threshold = filters.threshold_triangle(filtered_fruit)
        thresholded_fruit = filtered_fruit < self.threshold

        fruit_central_moments = measure.moments_central(thresholded_fruit)
        hu_moments = measure.moments_hu(
            measure.moments_normalized(fruit_central_moments))
        # We only keep relevant hu moments, that is, components 1 and 3
        self.hu_moments = hu_moments[[1, 3]]

        # And we apply a log transform to them
        self.hu_moments = np.array([
            -1 * np.sign(j) * np.log10(np.abs(j)) for j in self.hu_moments[:]
        ])

        fruit_eigvalues = measure.inertia_tensor_eigvals(
            thresholded_fruit, mu=fruit_central_moments)
        self.moment_ratio = max(fruit_eigvalues) / min(fruit_eigvalues)

        if feature_mode == 'hu_plus_ratio':
            self.features = np.append(self.hu_moments, self.moment_ratio)
            self.feature_size = 3
        elif feature_mode == 'hu_only':
            self.features = np.array(self.hu_moments)
            self.feature_size = 2

        if debug:
            print("threshold: \n", self.threshold)
            print("Central moments: \n", fruit_central_moments)
            print("Hu moments:\n", hu_moments)
            print(
                "Normalized Hu moments:\n",
                [-1 * np.sign(j) * np.log10(np.abs(j)) for j in hu_moments[:]])
            print("Inertia tensor eigenvalues:\n", fruit_eigvalues)
            print("Moment ratio:\n", self.moment_ratio)
            print("Features: \n", self.features)
Ejemplo n.º 30
0
    def momentos(self):
        """
            Calcula os 7 momentos de Hu e os momentos raw e centralizados de ordem 1 e 2

        """

        m = measure.moments(self.imagemTonsDeCinza)

        valores_m = [m[p, q] for (p, q) in momentsOrder]
        nomes_m = [
            M + str(p) + str(q)
            for M, (p, q) in zip(['M_'] * len(momentsOrder), momentsOrder)
        ]

        row = m[0, 1] / m[0, 0]
        col = m[1, 0] / m[0, 0]

        mu = measure.moments_central(self.imagemTonsDeCinza, row, col)

        valores_mu = [mu[p, q] for (p, q) in momentsOrder]
        nomes_mu = [
            M + str(p) + str(q)
            for M, (p, q) in zip(['Mu_'] * len(momentsOrder), momentsOrder)
        ]

        nu = measure.moments_normalized(mu)
        hu = measure.moments_hu(nu)

        valores_hu = list(hu)
        nomes_hu = [
            m + n for m, n in zip(['Hu_'] * len(valores_hu),
                                  map(str, range(0, len(valores_hu))))
        ]

        valores = valores_m + valores_mu + valores_hu
        nomes = nomes_m + nomes_mu + nomes_hu

        tipos = [numerico] * len(nomes)

        return nomes, tipos, valores
Ejemplo n.º 31
0
    def momentos_hu(self):
        """
            Calcula os 7 momentos de Hu

        """

        m = measure.moments(self.imagemTonsDeCinza)

        row = m[0, 1] / m[0, 0]
        col = m[1, 0] / m[0, 0]

        mu = measure.moments_central(self.imagemTonsDeCinza,row,col)
        nu = measure.moments_normalized(mu)
        hu = measure.moments_hu(nu)

        valores = list(hu)

        nomes = [m+n for m,n in zip(['hu_'] * len(valores),map(str,range(0,len(valores))))]

        tipos = [numerico] * len(nomes)

        return nomes, tipos, valores
Ejemplo n.º 32
0
    def momentos_hu(self):
        """
            Calcula os 7 momentos de Hu

        """

        m = measure.moments(self.imagemTonsDeCinza)

        row = m[0, 1] / m[0, 0]
        col = m[1, 0] / m[0, 0]

        mu = measure.moments_central(self.imagemTonsDeCinza,row,col)
        nu = measure.moments_normalized(mu)
        hu = measure.moments_hu(nu)

        valores = list(hu)

        nomes = [m+n for m,n in zip(['hu_'] * len(valores),map(str,range(0,len(valores))))]

        tipos = [numerico] * len(nomes)

        return nomes, tipos, valores
 def compute_hu_moments(i):
     b = cells_aligned_padded[i].astype(np.uint8)
     m = moments(b, order=1)
     hu = moments_hu(moments_normalized(moments_central(b, cc=m[0,1]/m[0,0], cr=m[1,0]/m[0,0])))
     return hu
Ejemplo n.º 34
0
    def circularity_test(self,
                         result_row):  #, im_array, image_times, psf_sigma):

        im_array = self.im_array
        if result_row[0] % 5000 == 0.:
            print result_row[0]

        try:
            p_stamp = self.createPostageStamp(
                im_array, list([result_row[1]['t0_x'], result_row[1]['t0_y']]),
                np.array(list([result_row[1]['v_x'], result_row[1]['v_y']])),
                self.image_times, [25., 25.])[0]
            p_stamp = np.array(p_stamp)
            p_stamp[np.isnan(p_stamp)] = 0.
            p_stamp[np.isinf(p_stamp)] = 0.
            #p_stamp -= np.min(p_stamp)
            #p_stamp /= np.max(p_stamp)
            #p_stamp
            image_thresh = np.max(p_stamp) * 0.5
            image = (p_stamp > image_thresh) * 1.
            rprop = measure.regionprops(np.array(image, dtype=np.int),
                                        intensity_image=p_stamp)[0]
            label_test, max_label = measure.label(image, return_num=True)
            max_conn = 0
            keep_label = 1
            for label_num in range(1, max_label):
                if len(np.where(label_test == label_num)[0]) > max_conn:
                    max_conn = len(np.where(label_test == label_num)[0])
                    keep_label = label_num
            image = (label_test == keep_label) * 1.
            #pre_image = p_stamp > image_thresh
            #image = np.array(pre_image*1.)
            mom = measure.moments(image)
            if mom[0, 0] > 0.:
                cr = mom[0, 1] / mom[0, 0]
                cc = mom[1, 0] / mom[0, 0]
                #cr = 12
                #cc = 12
                #moments = measure.moments(image, order=3)
                #cr = moments[0,1]/moments[0,0]
                #cc = moments[1,0]/moments[0,0]
                cent_mom = measure.moments_central(image, cr, cc, order=4)
                norm_mom = measure.moments_normalized(cent_mom)
                hu_mom = measure.moments_hu(norm_mom)
                #p_stamp_arr.append(hu_mom)
                #print moments[0,0], measure.perimeter(image)
                #circularity = (4*np.pi*moments[0,0])/(measure.perimeter(image)**2.)
                #circularity = (cent_mom[0,0]**2.)/(2.*np.pi*(cent_mom[2,0] + cent_mom[0,2]))
                if hu_mom[0] > 0.:
                    #if rprop['perimeter'] > 0.:
                    circularity = (1 / (2. * np.pi)) * (1 / hu_mom[0])
                #                    circularity = (1/(2.*np.pi))*(1/rprop['weighted_moments_hu'][0])
                #    circularity = (4*np.pi*rprop['area'])/(rprop['perimeter']**2.)
                else:
                    circularity = 0.
            else:
                circularity = 0.
            #print result_row[0], circularity
            #circularity = (cent_mom[0,0]**2.)/(2*np.pi*(cent_mom[2,0] + cent_mom[0,2]))
            psf_sigma = self.psf_sigma
            gaussian_fwhm = psf_sigma * 2.35
            fwhm_area = np.pi * (gaussian_fwhm / 2.)**2.
            wcr, wcc = rprop['weighted_centroid']

            if ((circularity > 0.7) & (cr > 10.) & (cr < 14.) & (cc > 10.) &
                (cc < 14.) &
                    #            if ((circularity > 0.4) & (circularity < 4.) & (cr > 10.) & (cr < 14.) & (cc > 10.) & (cc < 14.) &
                (cent_mom[0, 0] < (9.0 * fwhm_area)) & (cent_mom[0, 0] > 4.0)
                ):  #Use 200% error margin on psf_sigma for now
                #    test_class.append(1.)
                #                print circularity, cr, cc, cent_mom[0,0]
                #else:
                #    test_class.append(0.)
                test_class = 1.
                #print circularity, cr, cc, cent_mom[0,0]
            else:
                test_class = 0.

        except:
            test_class = 0.

        return [result_row[0], test_class]
Ejemplo n.º 35
0
def getHuMoments(image):
    img = image.copy()
    return measure.moments_hu(measure.moments(img))
Ejemplo n.º 36
0
Archivo: uns.py Proyecto: vassiliou/uns
    def properties(self):
        """Return a set of metrics on the masks with units of distance """
        imgH, imgW = self.image.shape  # Image height, width

        # Don't overemphasize one dimension over the other by setting the max
        # dimenstion to equal 1
        imgL = np.max([imgH, imgW])
        imgA = imgH * imgW  # Total number of pixels
        if self._properties is None:
            # Must load contour into single variable before checking self.hasmask
            # If mask exists, only then can we access x,y components of contour
            C = self.contour
            if self.hasmask:
                D = {}
                D['hasmask'] = True


                # Area metric is normalize to number of image pixels.  Sqrt
                # converts units to distance
                D['maskarea'] = np.sqrt(np.count_nonzero(self.image)/imgA)
                # Contour-derived values
                x, y = C
                D['contxmin'] = np.min(x)/imgL
                D['contxmax'] = np.max(x)/imgL
                D['contymin'] = np.min(y)/imgL
                D['contymax'] = np.max(y)/imgL

                D['contW'] = D['contxmax'] - D['contxmin']
                D['contH'] = D['contymax'] - D['contymin']

                # Image moments
                m = measure.moments(self.image, order=5)
                D['moments'] = m
                D['centrow'] = (m[0, 1]/m[0, 0])/imgL
                D['centcol'] = (m[1, 0]/m[0, 0])/imgL

                # Hu, scale, location, rotation invariant (7, 1)
                mHu = measure.moments_hu(m)
                for i, Ii in enumerate(mHu):
                    D['moment_hu_I{}'.format(i)] = Ii

                # Contour SVD is converted to two coordinates
                # First normalize and centre the contours
                D['contour'] = self.contour

                contour = (self.contour.T/imgL - [D['centrow'], D['centcol']]).T
                D['unitcontour'] = contour

                _, s, v = np.linalg.svd(contour.T)

                D['svd'] = s*v
                D['svdx0'] = D['svd'][0,0]
                D['svdx1'] = D['svd'][0,1]
                D['svdy0'] = D['svd'][1,0]
                D['svdy1'] = D['svd'][1,1]

                # Width by medial axis
                skel, distance = morphology.medial_axis(self.image,
                                                        self.image,
                                                        return_distance=True)
                self.skel = skel
                self.distance = distance

                #
                D['skelpixels'] = np.sqrt((np.sum(skel)/imgA))  # number of pixels

                # distances should be restricted to within mask to avoid over-
                # counting the zeros outside the mask
                distances = distance[self.image>0]/imgL
                q = [10, 25, 50, 75, 90]
                keys = ['skeldist{:2d}'.format(n) for n in q]
                vals = np.percentile(distances, q)
                D.update(dict(zip(keys, vals)))
                D['skelavgdist'] = np.mean(distances)
                D['skelmaxdist'] = np.max(distances)

                self._properties = D
        return self._properties
Ejemplo n.º 37
0
def hu_feature(gray):

    moments = measure.moments(gray)
    hu = measure.moments_hu(moments)

    return normalize(hu)