Beispiel #1
0
def apply_filters(images, sigmas):
    """ Apply multiple filters to 'images'
    """
    filtered_images = []
    for img in images:
        filtered = []
        for sigma in sigmas:
            for conv_filter in [
                    gaussian_filter, gaussian_laplace,
                    gaussian_gradient_magnitude
            ]:
                filtered.append(conv_filter(img, sigma))
            # *_eigenvals has changed from version 0.13 to 0.14.
            try:
                # v. 0.14
                eigs_struc = structure_tensor_eigvals(
                    *structure_tensor(img, sigma=sigma))
                eigs_hess = hessian_matrix_eigvals(
                    hessian_matrix(img, sigma=sigma, order="xy"))
            except TypeError as e:
                # v. 0.13
                eigs_struc = structure_tensor_eigvals(
                    *structure_tensor(img, sigma=sigma))
                eigs_hess = hessian_matrix_eigvals(
                    *hessian_matrix(img, sigma=sigma, order="xy"))
            for eig_h, eig_s in zip(eigs_struc, eigs_hess):
                filtered.append(eig_h)
                filtered.append(eig_s)

        filtered.append(equalize_hist(img))
        #selem = disk(30)
        #filtered.append(equalize(img, selem=selem))
        filtered_images.append(filtered)

    return np.array(filtered_images)
Beispiel #2
0
def get_feature_one(img, msk=None, para=para):
    chans, grade, w, items = para['chans'], para['grade'], para['w'], para[
        'items']
    feats, titles = [], []
    img = img.reshape(img.shape[:2] + (-1, ))
    if msk is None: msk = np.ones(img.shape[:2], dtype=np.bool)
    if chans is None: chans = range(img.shape[2])
    for c in [chans] if isinstance(chans, int) else chans:
        if 'ori' in items:
            feats.append(img[:, :, c][msk].astype(np.float32))
            titles.append('c%d_ori' % c)
        for o in range(grade):
            blurimg = ndimg.gaussian_filter(img[:, :, c],
                                            2**o,
                                            output=np.float32)
            feat_sobel = sobel(blurimg)[msk] if 'sob' in items else None
            if 'eig' in items:
                Axx, Axy, Ayy = structure_tensor(blurimg, w)
                l1, l2 = structure_tensor_eigvals(Axx, Axy, Ayy)
                feat_l1, feat_l2 = l1[msk], l2[msk]
            else:
                feat_l1 = feat_l2 = None
            feat_gauss = blurimg[msk] if 'blr' in items else None
            featcr = [feat_gauss, feat_sobel, feat_l1, feat_l2]
            title = [
                'c%d_s%d_%s' % (c, o, i)
                for i in ['gauss', 'sobel', 'l1', 'l2']
            ]
            titles.extend(
                [title[i] for i in range(4) if not featcr[i] is None])
            feats.extend(
                [featcr[i] for i in range(4) if not featcr[i] is None])
    return np.array(feats).T, titles
Beispiel #3
0
def local_shape_features_minmax(im, scaleStart):
    """ Creates features based on those in the Luca Fiaschi paper but on 5 scales independent of object size,
    but using a gaussian pyramid to calculate at multiple scales more efficiently
    and then linear upsampling to original image scale
    also includes gaussian smoothed image
    """
    # Smoothing and scale parameters chosen to approximate those in 'fine'
    # features

    imSizeC = im.shape[0]
    imSizeR = im.shape[1]
    f = np.zeros((imSizeC, imSizeR, 31))
    f[:, :, 0] = im

    # create pyramid structure
    pyr = skimage.transform.pyramid_gaussian(im,
                                             sigma=1.5,
                                             max_layer=5,
                                             downscale=2,
                                             multichannel=False)

    a = im
    for layer in range(0, 5):

        # calculate scale
        scale = [
            float(im.shape[0]) / float(a.shape[0]),
            float(im.shape[1]) / float(a.shape[1])
        ]

        # create features
        lap = scipy.ndimage.filters.laplace(a)

        [m, n] = np.gradient(a)
        ggm = np.hypot(m, n)

        x, y, z = skfeat.structure_tensor(a, 1)
        st = skfeat.structure_tensor_eigvals(x, y, z)

        maxim = scipy.ndimage.maximum_filter(a, 3)
        minim = scipy.ndimage.minimum_filter(a, 3)

        # upsample features to original image
        lap = scipy.ndimage.interpolation.zoom(lap, scale, order=1)
        ggm = scipy.ndimage.interpolation.zoom(ggm, scale, order=1)
        st0 = scipy.ndimage.interpolation.zoom(st[0], scale, order=1)
        st1 = scipy.ndimage.interpolation.zoom(st[1], scale, order=1)
        maxim = scipy.ndimage.interpolation.zoom(maxim, scale, order=1)
        minim = scipy.ndimage.interpolation.zoom(minim, scale, order=1)

        f[:, :, layer * 6 + 1] = lap
        f[:, :, layer * 6 + 2] = ggm
        f[:, :, layer * 6 + 3] = st0
        f[:, :, layer * 6 + 4] = st1
        f[:, :, layer * 6 + 5] = minim
        f[:, :, layer * 6 + 6] = maxim
        # get next layer
        a = next(pyr)

    return f
Beispiel #4
0
def test_structure_tensor_eigvals():
    square = np.zeros((5, 5))
    square[2, 2] = 1
    A_elems = structure_tensor(square, sigma=0.1, order='rc')
    with expected_warnings(['structure_tensor_eigvals is deprecated']):
        eigvals = structure_tensor_eigvals(*A_elems)
    eigenvalues = structure_tensor_eigenvalues(A_elems)
    assert_array_equal(eigvals, eigenvalues)
Beispiel #5
0
 def run(self, ips, snap, img, para=None):
     axx, axy, ayy = structure_tensor(snap, sigma=para['sigma'])
     l1, l2 = structure_tensor_eigvals(axx, axy, ayy)
     if para['axis'] == 'major': rst = l1
     elif para['axis'] == 'minor': rst = l2
     else: rst = (l1**2 + l2**2)**0.5
     if para['log']:
         rst += 1
         np.log(rst, out=rst)
     img[:] = scale(rst, ips.range[0], ips.range[1])
Beispiel #6
0
def calculate_skfeat_eigenvalues(im, s):
    #designed to be similar to vigra use of vigra.filters.structureTensorEigenvalues
    #slight differences in results
    gim = scipy.ndimage.filters.gaussian_filter(im,
                                                0.5 * s,
                                                mode='reflect',
                                                truncate=4)
    x, y, z = skfeat.structure_tensor(gim, 1 * s, mode='reflect')
    st_skfeat = skfeat.structure_tensor_eigvals(x, y, z)

    return ((st_skfeat[0] / (50 + s)), (st_skfeat[1] / (50 + s)))
Beispiel #7
0
def test_structure_tensor_eigvals():
    square = np.zeros((5, 5))
    square[2, 2] = 1
    Axx, Axy, Ayy = structure_tensor(square, sigma=0.1)
    l1, l2 = structure_tensor_eigvals(Axx, Axy, Ayy)
    assert_array_equal(
        l1, np.array([[0, 0, 0, 0, 0], [0, 2, 4, 2, 0], [0, 4, 0, 4, 0], [0, 2, 4, 2, 0], [0, 0, 0, 0, 0]])
    )
    assert_array_equal(
        l2, np.array([[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]])
    )
Beispiel #8
0
def test_structure_tensor_eigvals():
    square = np.zeros((5, 5))
    square[2, 2] = 1
    Axx, Axy, Ayy = structure_tensor(square, sigma=0.1)
    l1, l2 = structure_tensor_eigvals(Axx, Axy, Ayy)
    assert_array_equal(
        l1,
        np.array([[0, 0, 0, 0, 0], [0, 2, 4, 2, 0], [0, 4, 0, 4, 0],
                  [0, 2, 4, 2, 0], [0, 0, 0, 0, 0]]))
    assert_array_equal(
        l2,
        np.array([[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0],
                  [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]))
Beispiel #9
0
def stru(im):
    """Attempts to find edges in the image through use of structure tensor.
    
    Computes a mask of values that are higher mostly on edges by using the 
    square difference of the structure tensor eigen values. 
    Args: 
        im: input greyscale image
    Returns:
        mask of values higher on edges than interiors (see above)
    """
    A = structure_tensor(im, sigma = 0.8, mode="reflect")
    e1, e2 = structure_tensor_eigvals(*A)
    d = e1 + e2
    st = np.where(d > np.finfo(np.float16).eps, (e1 - e2) ** 2, 0)
    return st[..., np.newaxis]
Beispiel #10
0
    def read_image(self, image_name, size=None):
        options = self.get_options()

        if size:
            im = np.array(Image.open(image_name).convert("L").resize(size))
        else:
            im = np.array(Image.open(image_name).convert("L"))

        options["image"] = im
        Axx, Axy, Ayy = structure_tensor(im)
        feature = structure_tensor_eigvals(Axx, Axy, Ayy)[0]
        # plt.imshow(feature)
        # plt.show()

        return feature.reshape((1, feature.shape[0] * feature.shape[1]))[0]
Beispiel #11
0
def _get_structure_tensor_features(im, features_window_size, sigma_st):
    features_arr = np.empty(0)
    assert im.shape[0] == im.shape[1], "image must have a square shape"
    center = im_center(im)
    Axx, Axy, Ayy = feature.structure_tensor(im, sigma=sigma_st)
    l1, l2 = feature.structure_tensor_eigvals(Axx, Axy, Ayy)
    l1 = utils.extract_pad_image(input_img=l1,
                                 pt=center,
                                 window_size=features_window_size)
    l2 = utils.extract_pad_image(input_img=l2,
                                 pt=center,
                                 window_size=features_window_size)
    l1 = arr_to_vec(l1)
    l2 = arr_to_vec(l2)
    features_arr = np.append(features_arr, l1)
    features_arr = np.append(features_arr, l2)
    return features_arr
Beispiel #12
0
def local_shape_features_fine_imhist(im, scaleStart):
    """As per pyramid features but with histogram equalisation"""

    imSizeC = im.shape[0]
    imSizeR = im.shape[1]
    f = np.zeros((imSizeC, imSizeR, 26))
    im = exposure.equalize_hist(im)
    #im=exposure.equalize_adapthist(im, kernel_size=5)
    f[:, :, 0] = im

    # set up pyramid
    pyr = skimage.transform.pyramid_gaussian(im,
                                             sigma=1.5,
                                             max_layer=5,
                                             downscale=2,
                                             multichannel=False)
    a = im

    for layer in range(0, 5):

        scale = [
            float(im.shape[0]) / float(a.shape[0]),
            float(im.shape[1]) / float(a.shape[1])
        ]

        lap = scipy.ndimage.filters.laplace(a)

        [m, n] = np.gradient(a)
        ggm = np.hypot(m, n)

        x, y, z = skfeat.structure_tensor(a, 1)
        st = skfeat.structure_tensor_eigvals(x, y, z)

        lap = scipy.ndimage.interpolation.zoom(lap, scale, order=1)
        ggm = scipy.ndimage.interpolation.zoom(ggm, scale, order=1)
        st0 = scipy.ndimage.interpolation.zoom(st[0], scale, order=1)
        st1 = scipy.ndimage.interpolation.zoom(st[1], scale, order=1)
        up = scipy.ndimage.interpolation.zoom(a, scale, order=1)

        f[:, :, layer * 5 + 1] = lap
        f[:, :, layer * 5 + 2] = ggm
        f[:, :, layer * 5 + 3] = st0
        f[:, :, layer * 5 + 4] = st1
        f[:, :, layer * 5 + 5] = up
        a = next(pyr)
    return f
Beispiel #13
0
def create_features(p, x, y, r):
    """Create feature for patch

	Tested features:

	+ Mean intensity of RGB CIE[:,:,0]
	+ Mean intensity of RGB CIE[:,:,1]
	+ Mean intensity of RGB CIE[:,:,2]
	+ Mean intensity of HSV[:,:,0]
	+ Canny edge image
	+ Eigenvalues from structure tensor
	"""

    features = []

    # Get patch/patches
    structure_img = p.get_version("structure")[:, :, 2]
    structure_patch = structure_img[int(y - r):int(y + r),
                                    int(x - r):int(x + r)]

    rgb_img = p.get_version("contrast_rgb_cie")
    rgb_patch = rgb_img[int(y - r):int(y + r), int(x - r):int(x + r)]

    # Color quantization with k_means
    # patch = quantize_colors(patch)

    # ==== FEATURES =======

    # Median values of the individual color channels
    features.append(np.median(rgb_patch[:, :, 0]))
    features.append(np.median(rgb_patch[:, :, 1]))
    features.append(np.median(rgb_patch[:, :, 2]))

    # Edges
    edges = canny(structure_patch)
    features += edges.astype(float).ravel().tolist()

    # Structure Tensor
    Axx, Axy, Ayy = structure_tensor(structure_patch)
    l1, l2 = structure_tensor_eigvals(Axx, Axy, Ayy)

    features += l1.ravel().tolist()
    features += l2.ravel().tolist()

    return features
def create_features(p, x, y, r):
	"""Create feature for patch

	Tested features:

	+ Mean intensity of RGB CIE[:,:,0]
	+ Mean intensity of RGB CIE[:,:,1]
	+ Mean intensity of RGB CIE[:,:,2]
	+ Mean intensity of HSV[:,:,0]
	+ Canny edge image
	+ Eigenvalues from structure tensor
	"""

	features = []

	# Get patch/patches
	structure_img = p.get_version("structure")[:,:,2]
	structure_patch = structure_img[int(y-r):int(y+r), int(x-r):int(x+r)]

	rgb_img = p.get_version("contrast_rgb_cie")
	rgb_patch = rgb_img[int(y-r):int(y+r), int(x-r):int(x+r)]

	# Color quantization with k_means
	# patch = quantize_colors(patch)

	# ==== FEATURES =======

	# Median values of the individual color channels
	features.append(np.median(rgb_patch[:,:,0]))
	features.append(np.median(rgb_patch[:,:,1]))
	features.append(np.median(rgb_patch[:,:,2]))

	# Edges
	edges = canny(structure_patch)
	features += edges.astype(float).ravel().tolist()

	# Structure Tensor
	Axx, Axy, Ayy = structure_tensor(structure_patch)
	l1, l2 = structure_tensor_eigvals(Axx, Axy, Ayy)

	features += l1.ravel().tolist()
	features += l2.ravel().tolist()

	return features
Beispiel #15
0
def normalize_image(img):
    """Normalize images.
    
    Normalize images by gaussian smoothing for noise, push background values to 
    0, and nuclei values toward 1 with sigmoid. The set parameters seemed not to 
    damage boudnary information between nuclei too much. 
    Args: 
        img: greyscale image data
    Returns: 
        normalized image
    """
    img_2 = gaussian(img, sigma = 1)
    cut = np.mean(img_2)
    sig = adjust_sigmoid(img_2, 
                         cutoff = cut,
                         gain = 5,
                         inv = (cut > 0.5),
                         )
    sig = rescale_intensity(sig)
    
    sig_2 = gaussian(sig, sigma = 1)
    A = structure_tensor(sig_2, sigma = 0.8, mode = "reflect")
    e1, e2 = structure_tensor_eigvals(*A)
    d = e1 + e2
    str_corr = np.where(d > np.finfo(np.float16).eps, 
                        (e1 - e2) ** 2,
                        0,
                        )
    bge = str_corr > np.mean(str_corr) * .01
    
    bdy_grad_eigen = opening(remove_small_objects(bge, 
                                                  min_size = 64, 
                                                  connectivity = 2,
                                                  ),
                             ) 
    gaus = gaussian(sig, sigma =1 )
    out = np.where(bdy_grad_eigen, sig, gaus)
    out = rescale_intensity(out)
    return out.astype(np.float32)
def computeStructureFeatures(Xdata, isTraining, path):
    path = path + '/structFeatures'
    print("\nComputing: All Structure Features")
    print("is Training? %r" %isTraining)
    start = time.time()
    # Separate color channel
    RChannel = Xdata[:,:,0]
    GChannel = Xdata[:,:,1]
    BChannel = Xdata[:,:,2]
    # Calculate Laplacian of Gaussian (sigma = 1.6)
    print("Computing: Laplacian of Gaussian")
    gaussianLF_R = gaussian_laplace(RChannel, 1.6)
    gaussianLF_G = gaussian_laplace(GChannel, 1.6)
    gaussianLF_B = gaussian_laplace(BChannel, 1.6)
    gaussianLF = np.dstack((gaussianLF_R, gaussianLF_G, gaussianLF_B))
    gaussianLF = np.resize(gaussianLF, 
                             (gaussianLF.shape[0]*gaussianLF.shape[1], gaussianLF.shape[2]))
    end = time.time()
    if (isTraining):
        print("Computed: gaussianLapFeatures_Tr in %f seconds" %(end-start))
        
#        with open('gaussianLapFeatures_Tr.pkl','wb') as outfile:
#            pickle.dump(gaussianLF, outfile, pickle.HIGHEST_PROTOCOL)
#        np.savetxt("/Users/wuwenjun/GitHub/CSE-577-Melanoma-Nuclei-Segmentation/gaussianLapFeatures_Tr.csv", 
#                   gaussianLF, delimiter=",")
    else:
        print("Computed: gaussianLapFeatures_Ts in %f seconds" %(end-start))
#        with open('gaussianLapFeatures_Ts.pkl','wb') as outfile:
#            pickle.dump(gaussianLF, outfile, pickle.HIGHEST_PROTOCOL)
#        np.savetxt("/Users/wuwenjun/GitHub/CSE-577-Melanoma-Nuclei-Segmentation/gaussianLapFeatures_Ts.csv", 
#                   gaussianLF, delimiter=",")
        
    # Calculate eigenvalues of structure tensor  (sigma =1.6, 3.5)
    print("Computing: eigenvalues of structure tensor")
    start = time.time()
    
    Axx_R1, Axy_R1, Ayy_R1 = structure_tensor(RChannel, sigma = 1.6)
    larger_R1, smaller_R1 = structure_tensor_eigvals(Axx_R1, Axy_R1, Ayy_R1)
    Axx_R2, Axy_R2, Ayy_R2 = structure_tensor(RChannel, sigma = 3.5)
    larger_R2, smaller_R2 = structure_tensor_eigvals(Axx_R2, Axy_R2, Ayy_R2)
    Axx_G1, Axy_G1, Ayy_G1 = structure_tensor(GChannel, sigma = 1.6)
    larger_G1, smaller_G1 = structure_tensor_eigvals(Axx_G1, Axy_G1, Ayy_G1)
    Axx_G2, Axy_G2, Ayy_G2 = structure_tensor(GChannel, sigma = 3.5)
    larger_G2, smaller_G2 = structure_tensor_eigvals(Axx_G2, Axy_G2, Ayy_G2)
    Axx_B1, Axy_B1, Ayy_B1 = structure_tensor(BChannel, sigma = 1.6)
    larger_B1, smaller_B1 = structure_tensor_eigvals(Axx_B1, Axy_B1, Ayy_B1)
    Axx_B2, Axy_B2, Ayy_B2 = structure_tensor(BChannel, sigma = 3.5)
    larger_B2, smaller_B2 = structure_tensor_eigvals(Axx_B2, Axy_B2, Ayy_B2)
    eigenST = np.dstack((larger_R1, smaller_R1, larger_R2, smaller_R2, 
                              larger_G1, smaller_G1, larger_G2, smaller_G2, 
                              larger_B1, smaller_B1, larger_B2, smaller_B2))
    eigenST = np.resize(eigenST, 
                             (eigenST.shape[0]*eigenST.shape[1], eigenST.shape[2]))
    end = time.time()
    if (isTraining):
        print("Computed: eigenStructFeatures_Tr in %f seconds" %(end-start))
        
#        with open('eigenStructFeatures_Tr.pkl','wb') as outfile:
#            pickle.dump(eigenST, outfile, pickle.HIGHEST_PROTOCOL)
#        np.savetxt("/Users/wuwenjun/GitHub/CSE-577-Melanoma-Nuclei-Segmentation/eigenStructFeatures_Tr.csv", 
#                   eigenST, delimiter=",")
    else:
        print ("Computed: eigenStructFeatures_Ts in %f seconds" %(end-start))        
#        with open('eigenStructFeatures_Ts.pkl','wb') as outfile:
#            pickle.dump(eigenST, outfile, pickle.HIGHEST_PROTOCOL)
#        np.savetxt("/Users/wuwenjun/GitHub/CSE-577-Melanoma-Nuclei-Segmentation/eigenStructFeatures_Ts.csv", 
#                   eigenST, delimiter=",")
        
    # Calculate eigenvalues of Hessian matrix
    print("Computing: eigenvalues of Hessian matrix")
    start = time.time()
    Hrr_R1, Hrc_R1, Hcc_R1 = hessian_matrix(RChannel, sigma = 1.6, order='rc')
    larger_R1, smaller_R1 = hessian_matrix_eigvals(Hrr_R1, Hrc_R1, Hcc_R1)
    Hrr_R2, Hrc_R2, Hcc_R2 = hessian_matrix(RChannel, sigma = 3.5, order='rc')
    larger_R2, smaller_R2 = hessian_matrix_eigvals(Hrr_R2, Hrc_R2, Hcc_R2)
    Hrr_G1, Hrc_G1, Hcc_G1 = hessian_matrix(GChannel, sigma = 1.6, order='rc')
    larger_G1, smaller_G1 = hessian_matrix_eigvals(Hrr_G1, Hrc_G1, Hcc_G1)
    Hrr_G2, Hrc_G2, Hcc_G2 = hessian_matrix(GChannel, sigma = 3.5, order='rc')
    larger_G2, smaller_G2 = hessian_matrix_eigvals(Hrr_G2, Hrc_G2, Hcc_G2)
    Hrr_B1, Hrc_B1, Hcc_B1 = hessian_matrix(BChannel, sigma = 1.6, order='rc')
    larger_B1, smaller_B1 = hessian_matrix_eigvals(Hrr_B1, Hrc_B1, Hcc_B1)
    Hrr_B2, Hrc_B2, Hcc_B2 = hessian_matrix(BChannel, sigma = 3.5, order='rc')
    larger_B2, smaller_B2 = hessian_matrix_eigvals(Hrr_B2, Hrc_B2, Hcc_B2)
    eigenHess = np.dstack((larger_R1, smaller_R1, larger_R2, smaller_R2,
                                larger_G1, smaller_G1, larger_G2, smaller_G2,
                                larger_B1, smaller_B1, larger_B2, smaller_B2))
    eigenHess = np.resize(eigenHess, 
                             (eigenHess.shape[0]*eigenHess.shape[1], eigenHess.shape[2]))
    end = time.time()
    if (isTraining):
        print("Computed: eigenHessFeatures_Tr in %f seconds" % (end-start))
#        with open('eigenHessFeatures_Tr.pkl','wb') as outfile:
#            pickle.dump(eigenHess, outfile, pickle.HIGHEST_PROTOCOL)
#        np.savetxt("/Users/wuwenjun/GitHub/CSE-577-Melanoma-Nuclei-Segmentation/eigenHessFeatures_Tr.csv", 
#                   eigenHess, delimiter=",")
    else:
        print("Computed: eigenHessFeatures_Ts in %f seconds" % (end-start))
#        with open('eigenHessFeatures_Ts.pkl','wb') as outfile:
#            pickle.dump(eigenHess, outfile, pickle.HIGHEST_PROTOCOL)
#        np.savetxt("/Users/wuwenjun/GitHub/CSE-577-Melanoma-Nuclei-Segmentation/eigenHessFeatures_Ts.csv", 
#                   eigenHess, delimiter=",")
    
    # Calculate Gaussian gradient magnitude (sigma = 1.6)
    print("Computing: Gaussian gradient magnitude")
    start = time.time()
    gaussian_grad_R = gaussian_gradient_magnitude(RChannel, sigma = 1.6)
    gaussian_grad_G = gaussian_gradient_magnitude(GChannel, sigma = 1.6)
    gaussian_grad_B = gaussian_gradient_magnitude(BChannel, sigma = 1.6)
    gaussian_grad = np.dstack((gaussian_grad_R, gaussian_grad_G, 
                                    gaussian_grad_B))
    gaussian_grad = np.resize(gaussian_grad, 
                             (gaussian_grad.shape[0]*gaussian_grad.shape[1], 
                              gaussian_grad.shape[2]))
    end = time.time()
    if (isTraining):
        print("Computed: gaussianGradFeatures_Tr in %f seconds" % (end-start))
        
#        with open('gaussianGradFeatures_Tr.pkl','wb') as outfile:
#            pickle.dump(gaussian_grad, outfile, pickle.HIGHEST_PROTOCOL)
#        np.savetxt("/Users/wuwenjun/GitHub/CSE-577-Melanoma-Nuclei-Segmentation/gaussianGradFeatures_Tr.csv", 
#                   gaussian_grad, delimiter=",")
    else:
        print("Computed: gaussianGradFeatures_Ts in %f seconds" % (end-start))
        
#        with open('gaussianGradFeatures_Ts.pkl','wb') as outfile:
#            pickle.dump(gaussian_grad, outfile, pickle.HIGHEST_PROTOCOL)
#        np.savetxt("/Users/wuwenjun/GitHub/CSE-577-Melanoma-Nuclei-Segmentation/gaussianGradFeatures_Ts.csv", 
#                   gaussian_grad, delimiter=",")
    
    All = np.concatenate((gaussianLF, eigenST, eigenHess, gaussian_grad),axis = 1)
    if (isTraining):
        path = path + '_Tr'
        np.savez_compressed(path, data = All)
        print("\nFINISHED: Structure Features for Training data\n")

    else:   
        path = path + '_Ts'
        np.savez_compressed(path, data = All)
        print("\nFINISHED: Structure Features for Testing data\n")
            #with timeit("canny"):
            im1 = feature.canny(im,
                                sigma=2.5,
                                low_threshold=0.7,
                                high_threshold=0.99,
                                use_quantiles=True)  #edge detection
            #im1 = feature.canny(im, sigma=2.5, low_threshold=8, high_threshold=10, use_quantiles=False) #edge detection

            im2 = morphology.binary_fill_holes(im1, structure=struct).astype(
                int)  #fill holes
            #im2 = morphology.binary_fill_holes(im1).astype(int) #fill holes
            im3 = morphology.binary_erosion(im2, structure=struct).astype(
                int)  #erode to remove lines and small schmutz
            Axx, Axy, Ayy = feature.structure_tensor(
                im, sigma=0.5, mode='nearest')  #structure
            im4 = feature.structure_tensor_eigvals(Axx, Axy,
                                                   Ayy)[0]  #structure
            im4 = im4 * morphology.binary_erosion(
                im3, structure=struct, iterations=4).astype(int)  #structure

            label_image = label(im3)  #label all ellipses (and other objects)

            if display == 2:  #debug mode
                ax2.clear()
                ax2.set_axis_off()
                ax2.imshow(im4, cmap='gray')
                ax3.clear()
                ax3.set_axis_off()
                ax3.imshow(im2, cmap='gray')
                ax4.clear()
                ax4.set_axis_off()
                #                ax4.imshow(im4,cmap='jet', vmin = 0, vmax = 3000 )
Beispiel #18
0
def image_preprocess(filename, nfeatures, original_image_array, sigmas=SIGMAS):

    global preprocess_counter
    x = 0

    # For performance reasons, pre-allocate an empty 2D array of the right shape
    #  and dimensions to contain all of the given image's pixels and features
    images = numpy.empty((nfeatures, original_image_array.size),
                         dtype=numpy.uint8)

    # normalize our (32-bit grayscale) image to an 8-bit unsigned representation
    #  to conserve memory and increase performance
    img = cv2.normalize(original_image_array, None, 0, 255, cv2.NORM_MINMAX,
                        cv2.CV_8U)

    # the first feature is just the original normalized pixel value
    images[x] = img.flatten(order='F')
    x += 1  # insert original image

    # a set of features per pixel based on the image's filename which
    #  encodes metadata related to the age class of the cell
    for age in AGE_CLASSES:
        img = label_age(original_image_array, age, filename)
        images[x] = img.flatten(order='F')
        x += 1

    # call a series of parameterized image filters to extract features
    for s in sigmas:
        # Gaussian Smoothing
        img = gaussian_filter(original_image_array, sigma=s)
        img = cv2.normalize(img, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U)
        images[x] = img.flatten(order='F')
        x += 1

        # Sobel Edge Detection
        img = scipy.ndimage.sobel(original_image_array,
                                  mode='constant',
                                  cval=s)
        img = cv2.normalize(img, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U)
        images[x] = img.flatten(order='F')
        x += 1

        # Laplacian of Gaussian Edge Detection
        img = scipy.ndimage.gaussian_laplace(original_image_array, sigma=s)
        img = cv2.normalize(img, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U)
        images[x] = img.flatten(order='F')
        x += 1

        # Gaussian Gradient Magnitude Edge Detection
        img = scipy.ndimage.gaussian_gradient_magnitude(original_image_array,
                                                        sigma=s)
        img = cv2.normalize(img, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U)
        images[x] = img.flatten(order='F')
        x += 1

        # Difference of Gaussians
        k = 1.7  # determined by trial and error
        tmp1_array = scipy.ndimage.gaussian_filter(original_image_array,
                                                   sigma=s * k)
        tmp2_array = scipy.ndimage.gaussian_filter(original_image_array,
                                                   sigma=s)
        img = (tmp1_array - tmp2_array)
        img = cv2.normalize(img, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U)
        images[x] = img.flatten(order='F')
        x += 1

        # Structure Tensor Eigenvalues
        Axx, Axy, Ayy = feature.structure_tensor(
            Image.fromarray(original_image_array), sigma=s)
        large_array, small_array = feature.structure_tensor_eigvals(
            Axx, Axy, Ayy)
        img = cv2.normalize(large_array, None, 0, 255, cv2.NORM_MINMAX,
                            cv2.CV_8U)
        images[x] = img.flatten(order='F')
        x += 1
        img = cv2.normalize(small_array, None, 0, 255, cv2.NORM_MINMAX,
                            cv2.CV_8U)
        images[x] = img.flatten(order='F')
        x += 1

        # Hessian Matrix
        Hrr, Hrc, Hcc = feature.hessian_matrix(
            Image.fromarray(original_image_array), sigma=s, order='rc')
        img = cv2.normalize(Hrr, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U)
        images[x] = img.flatten(order='F')
        x += 1
        img = cv2.normalize(Hrc, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U)
        images[x] = img.flatten(order='F')
        x += 1
        img = cv2.normalize(Hcc, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U)
        images[x] = img.flatten(order='F')
        x += 1

    # Generate proximity map (to center of image)
    newimg = center_proximity(original_image_array)
    img = cv2.normalize(newimg, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U)
    images[x] = img.flatten(order='F')
    x += 1

    # Simple Intensity Threshholding
    img = cv2.GaussianBlur(original_image_array, (5, 5), cv2.BORDER_WRAP)
    img = cv2.normalize(img, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U)
    ret, img = cv2.threshold(img, 205, 255, cv2.THRESH_BINARY)
    images[x] = img.flatten(order='F')
    x += 1

    # Flip the orientation of the flattened 2D array
    images = numpy.transpose(images, (1, 0))

    preprocess_counter += 1
    print("Processed raw image %d" % preprocess_counter)

    return (images)
Beispiel #19
0
#                        cells_per_block=(3, 3), 
#                        visualise=None, transform_sqrt=False, 
#                        feature_vector=True)#没看懂 一维矩阵 长度不定
        
        glcm=feature.greycomatrix(r+g+b, [1],[0,np.pi/4,np.pi/2,3*np.pi/4,np.pi], levels=3*256)#
        greycghg=feature.greycoprops(glcm, prop='homogeneity')/(256*256*9)#size1*5
        greycgcl=feature.greycoprops(glcm, prop='correlation')/(256*256*9)
        greycgeg=feature.greycoprops(glcm, prop='energy')/(256*256*9)
        greycgasm=feature.greycoprops(glcm, prop='ASM')/(256*256*9)
        greycgctt=feature.greycoprops(glcm, prop='contrast')/(256*256*9)
        
        
        lbp=feature.local_binary_pattern(imgrey, 8, np.pi/4)#size同图片
        plm=feature.peak_local_max(imgrey, min_distance=1)#多个坐标对
        st=feature.structure_tensor(imgrey, sigma=1, mode='constant', cval=0)#三个同图片一样大的矩阵
        ste=feature.structure_tensor_eigvals(st[0],st[1],st[2])#两个个同图片一样大的矩阵
#        hmf=feature.hessian_matrix(image, sigma=1, mode='constant', 
#                                   cval=0, order=None)#6个三通道的原图大小矩阵
        hmd=feature.hessian_matrix_det(imgrey, sigma=1)#原图大小矩阵
#        hme=feature.hessian_matrix_eigvals(hmf, Hxy=None, Hyy=None)
        si=feature.shape_index(imgrey, sigma=1, mode='constant', cval=0)#原图大小矩阵
#        ckr=feature.corner_kitchen_rosenfeld(image, mode='constant', cval=0) ##原图大小矩阵 三通道               
#        ch=feature.corner_harris(imgrey, method='k', k=0.05, eps=1e-06, sigma=1)#原图大小矩阵
#        cht=feature.corner_shi_tomasi(imgrey, sigma=1)#原图大小矩阵
#        cfs=feature.corner_foerstner(imgrey, sigma=1)#2个 #原图大小矩阵
#        csb=feature.corner_subpix(image, ch, window_size=11, alpha=0.99)
        cps=feature.corner_peaks(imgrey, min_distance=1, threshold_abs=None, 
                                 threshold_rel=0.1, exclude_border=True, indices=True, 
                                 footprint=None, labels=None)#一堆坐标值
#        cmr=feature.corner_moravec(imgrey, window_size=1)#原图大小矩阵
#        cft=feature.corner_fast(imgrey, n=12, threshold=0.15)#原图大小矩阵
from context import data

with rio.open(data.naip.lineaments, 'r') as src:
    image = src.read()

# This assumes a grayscale image. For simplicity, we'll just use RGB mean.
data = image.astype(float).mean(axis=0)

# Compute the structure tensor. This is basically local gradient similarity.
# We're getting three components at each pixel that correspond to a 2x2
# symmetric matrix. i.e. [[axx, axy],[axy, ayy]]
axx, axy, ayy = structure_tensor(data, sigma=2.5, mode='mirror')

# Then we'll compute the eigenvalues of that matrix.
v1, v2 = structure_tensor_eigvals(axx, axy, ayy)

# And calculate the eigenvector corresponding to the largest eigenvalue.
dx, dy = v1 - axx, -axy

# We have a vector at each pixel now.  However, we don't really care about all
# of them, only those with a large magnitude.  Also, we don't need to worry
# about every pixel, as adjacent values are very highly correlated. Therefore,
# let's only consider every 10th pixel in each direction.

# Top 10th percentile of magnitude
mag = np.hypot(dx, dy)
selection = mag > np.percentile(mag, 90)

# Every 10th pixel (skipping left edge due to boundary effects)
ds = np.zeros_like(selection)
Beispiel #21
0
    img_mult = img_mult.reshape(img.shape)
    return img_mult


#############
sift = cv2.xfeatures2d.SIFT_create()
kp1, des1 = feature.ORB(frame1,None)
kp2, des2 = sift.detectAndCompute(frame2,None)

#################
def ng_structure_tensor(frame):
    kernel = np.array([[-3,0,3],[-10,0,10],[-3,0,3]],dtype=np.float)/32.


Axx, Axy, Ayy = feature.structure_tensor(frame_sig, sigma=100)
l1, l2 = feature.structure_tensor_eigvals(Axx, Axy, Ayy)
l1_arr = np.array(l1)
l2_arr = np.array(l2)

#fig, ax = plt.subplots(3,2)
ax[0,0].imshow(frame_sig)
ax[1,0].imshow(l1_arr)
ax[2,0].imshow(l2_arr)
ax[0,1].imshow(Axx)
ax[1,1].imshow(Axy)
ax[2,1].imshow(Ayy)

#### Filtering ellipses
import pandas as pd

# from a list...
Beispiel #22
0
def image_preprocess(filename, nfeatures, original_image_array, sigmas=SIGMAS):

    x = 0

    #simage(original_image_array, "original.tif")

    images = numpy.empty((nfeatures, original_image_array.size),
                         dtype=numpy.uint8)

    img = cv2.normalize(original_image_array, None, 0, 255, cv2.NORM_MINMAX,
                        cv2.CV_8U)
    images[x] = img.flatten(order='F')
    x += 1

    for age in AGE_CLASSES:
        img = label_age(original_image_array, age, filename)
        #f = "AGE_" + age + ".tif";  simage(img, f)

        #img = cv2.normalize(img,None,0,255,cv2.NORM_MINMAX,cv2.CV_8U)
        images[x] = img.flatten(order='F')
        x += 1

    for s in sigmas:

        # Gaussian Smoothing
        img = gaussian_filter(original_image_array, sigma=s)

        #f = "gaussian_smoothing_" + str(s) + ".tif";   simage(img, f)

        img = cv2.normalize(img, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U)
        images[x] = img.flatten(order='F')
        x += 1

        # Sobel Edge Detection
        img = scipy.ndimage.sobel(original_image_array,
                                  mode='constant',
                                  cval=s)

        #f = "sobel_edge_detection_" + str(s) + ".tif";   simage(img, f)

        img = cv2.normalize(img, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U)
        images[x] = img.flatten(order='F')
        x += 1

        # Laplacian of Gaussian Edge Detection
        img = scipy.ndimage.gaussian_laplace(original_image_array, sigma=s)

        #f = "laplacian_of_gaussian_edge_detection_" + str(s) + ".tif";   simage(img, f)

        img = cv2.normalize(img, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U)
        images[x] = img.flatten(order='F')
        x += 1

        # Gaussian Gradient Magnitude Edge Detection
        img = scipy.ndimage.gaussian_gradient_magnitude(original_image_array,
                                                        sigma=s)

        #f = "gaussian_gradient_magnitude__edge_detection_" + str(s) + ".tif";   simage(img, f)

        img = cv2.normalize(img, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U)
        images[x] = img.flatten(order='F')
        x += 1

        # Difference of Gaussians
        k = 1.7
        tmp1_array = scipy.ndimage.gaussian_filter(original_image_array,
                                                   sigma=s * k)
        tmp2_array = scipy.ndimage.gaussian_filter(original_image_array,
                                                   sigma=s)
        img = (tmp1_array - tmp2_array)

        #f = "difference_of_gaussians_" + str(s) + ".tif";   simage(img, f)

        img = cv2.normalize(img, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U)
        images[x] = img.flatten(order='F')
        x += 1

        # Structure Tensor Eigenvalues
        Axx, Axy, Ayy = feature.structure_tensor(
            Image.fromarray(original_image_array), sigma=s)
        large_array, small_array = feature.structure_tensor_eigvals(
            Axx, Axy, Ayy)

        #f = "structure_tensor_eigenvalues_large_" + str(s) + ".tif";   simage(large_array, f)
        #f = "structure_tensor_eigenvalues_small_" + str(s) + ".tif";   simage(small_array, f)

        img = cv2.normalize(large_array, None, 0, 255, cv2.NORM_MINMAX,
                            cv2.CV_8U)
        images[x] = img.flatten(order='F')
        x += 1
        img = cv2.normalize(small_array, None, 0, 255, cv2.NORM_MINMAX,
                            cv2.CV_8U)
        images[x] = img.flatten(order='F')
        x += 1

        # Hessian Matrix
        Hrr, Hrc, Hcc = feature.hessian_matrix(
            Image.fromarray(original_image_array), sigma=s, order='rc')

        #f = "hessian_matrix_Hrr_" + str(s) + ".tif";   simage(Hrr, f)
        #f = "hessian_matrix_Hrc_" + str(s) + ".tif";   simage(Hrc, f)
        #f = "hessian_matrix_Hcc_" + str(s) + ".tif";   simage(Hcc, f)

        img = cv2.normalize(Hrr, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U)
        images[x] = img.flatten(order='F')
        x += 1
        img = cv2.normalize(Hrc, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U)
        images[x] = img.flatten(order='F')
        x += 1
        img = cv2.normalize(Hcc, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U)
        images[x] = img.flatten(order='F')
        x += 1

    # Wang points (radii of 1,3,5,7)
    newimg = wang_function(original_image_array, radius=1)

    #f = "wang_points_radius1.tif";   simage(newimg, f)

    img = cv2.normalize(newimg, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U)
    images[x] = img.flatten(order='F')
    x += 1
    newimg = wang_function(original_image_array, radius=3)

    #f = "wang_points_radius3.tif";   simage(newimg, f)

    img = cv2.normalize(newimg, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U)
    images[x] = img.flatten(order='F')
    x += 1
    newimg = wang_function(original_image_array, radius=5)

    #f = "wang_points_radius5.tif";   simage(newimg, f)

    img = cv2.normalize(newimg, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U)
    images[x] = img.flatten(order='F')
    x += 1
    newimg = wang_function(original_image_array, radius=7)

    #f = "wang_points_radius7.tif";   simage(newimg, f)

    img = cv2.normalize(newimg, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U)
    images[x] = img.flatten(order='F')
    x += 1

    # Median Neighborhood compared to overall
    newimg = median_hood(original_image_array, radius=4, factor=1.0)

    #f = "median_hood_1.0.tif";   simage(newimg, f)

    img = cv2.normalize(newimg, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U)
    images[x] = img.flatten(order='F')
    x += 1

    newimg = median_hood(original_image_array, radius=4, factor=1.25)

    #f = "median_hood_1.25.tif";   simage(newimg, f)

    img = cv2.normalize(newimg, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U)
    images[x] = img.flatten(order='F')
    x += 1

    newimg = median_hood(original_image_array, radius=4, factor=1.5)

    #f = "median_hood_1.5.tif";   simage(newimg, f)

    img = cv2.normalize(newimg, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U)
    images[x] = img.flatten(order='F')
    x += 1

    # Generate proximity map (to center of image)
    newimg = center_proximity(original_image_array)

    #f = "center_proximity.tif";   simage(newimg, f)

    img = cv2.normalize(newimg, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U)
    images[x] = img.flatten(order='F')
    x += 1

    images = numpy.transpose(images, (1, 0))

    print("processed %d features out of %d" % (x, nfeatures))

    return (images)