예제 #1
0
    lut[msk] = np.arange(1, msk.sum()+1)
    mask = lut[lab].ravel()[rst].reshape(shp)
    return hist, lut[lab], mask

if __name__ == '__main__':
    from skimage.io import imread, imsave
    from skimage.data import coins, gravel
    from skimage.segmentation import find_boundaries
    
    import matplotlib.pyplot as plt
    from time import time

    import cellpose
    from cellpose import models, utils

    img = gravel()
    use_GPU = models.use_gpu()
    model = models.Cellpose(gpu=use_GPU, model_type='cyto')
    channels = [0, 0]
    mask, flow, style, diam = model.eval(
        img, diameter=30, rescale=None, channels=[0,0])
    start = time()
    water, core, msk = flow2msk(flow[1].transpose(1,2,0), None, 1.0, 20, 100)
    print('flow to mask cost:', time()-start)
    ax1, ax2, ax3, ax4, ax5, ax6 =\
        [plt.subplot(230+i) for i in (1,2,3,4,5,6)]
    ax1.imshow(img)
    ax2.imshow(flow[0])
    ax3.imshow(np.log(water+1))
    ax4.imshow(core)
    ax5.imshow(msk)
# prepare filter bank kernels
kernels = []
for theta in range(4):
    theta = theta / 4. * np.pi
    for sigma in (1, 3):
        for frequency in (0.05, 0.25):
            kernel = np.real(gabor_kernel(frequency, theta=theta,
                                          sigma_x=sigma, sigma_y=sigma))
            kernels.append(kernel)


shrink = (slice(0, None, 3), slice(0, None, 3))
brick = img_as_float(data.brick())[shrink]
grass = img_as_float(data.grass())[shrink]
gravel = img_as_float(data.gravel())[shrink]
image_names = ('brick', 'grass', 'gravel')
images = (brick, grass, gravel)

# prepare reference features
ref_feats = np.zeros((3, len(kernels), 2), dtype=np.double)
ref_feats[0, :, :] = compute_feats(brick, kernels)
ref_feats[1, :, :] = compute_feats(grass, kernels)
ref_feats[2, :, :] = compute_feats(gravel, kernels)

print('Rotated images matched against references using Gabor filter banks:')

print('original: brick, rotated: 30deg, match result: ', end='')
feats = compute_feats(ndi.rotate(brick, angle=190, reshape=False), kernels)
print(image_names[match(feats, ref_feats)])
예제 #3
0
# Numpy, Scipy, Scikit, OpenCV, Python Image Library(PIL)

#An image
#Can be represented by a 2D arrays
# (each grid/pixel of an array represents a pixel in the image)
#A digital image can be classified into 2 types of channels: grayscale, multichannel
#Grayscale - only grey shades(different tones of black and white)
#Multichannel RGB - has 3 layers (Red, Green, Blue)

#About Scikit
#Opensource
#A Python package
#Works with NumPy arrays

#To install a package,
#pip install scikit-image
#or
#file -> settings -> project -> + search for the package -> install

# Displaying an image from the library itself
from skimage import data, io

#scikit includes a file with some preloaded images in them(inside the 'data' module)
image = data.gravel()
#imshow() displays an image
io.imshow(image)
#show() displays the pending images queued by imshow(used when displaying images from non-interactive shells)
io.show()


예제 #4
0
blurred with a Gaussian kernel from a less-blurred image. This example shows
two applications of the Difference of Gaussians approach for band-pass
filtering.
"""

######################################################################
# Denoise image and reduce shadows
# ================================

import matplotlib.pyplot as plt
import numpy as np
from skimage.data import gravel
from skimage.filters import difference_of_gaussians, window
from scipy.fftpack import fftn, fftshift

image = gravel()
wimage = image * window('hann', image.shape)  # window image to improve FFT
filtered_image = difference_of_gaussians(image, 1, 12)
filtered_wimage = filtered_image * window('hann', image.shape)
im_f_mag = fftshift(np.abs(fftn(wimage)))
fim_f_mag = fftshift(np.abs(fftn(filtered_wimage)))

fig, ax = plt.subplots(nrows=2, ncols=2, figsize=(8, 8))
ax[0, 0].imshow(image, cmap='gray')
ax[0, 0].set_title('Original Image')
ax[0, 1].imshow(np.log(im_f_mag), cmap='magma')
ax[0, 1].set_title('Original FFT Magnitude (log)')
ax[1, 0].imshow(filtered_image, cmap='gray')
ax[1, 0].set_title('Filtered Image')
ax[1, 1].imshow(np.log(fim_f_mag), cmap='magma')
ax[1, 1].set_title('Filtered FFT Magnitude (log)')
    hist, _ = np.histogram(lbp, density=True, bins=n_bins, range=(0, n_bins))
    for name, ref in refs.items():
        ref_hist, _ = np.histogram(ref,
                                   density=True,
                                   bins=n_bins,
                                   range=(0, n_bins))
        score = kullback_leibler_divergence(hist, ref_hist)
        if score < best_score:
            best_score = score
            best_name = name
    return best_name


brick = data.brick()
grass = data.grass()
gravel = data.gravel()

refs = {
    'brick': local_binary_pattern(brick, n_points, radius, METHOD),
    'grass': local_binary_pattern(grass, n_points, radius, METHOD),
    'gravel': local_binary_pattern(gravel, n_points, radius, METHOD)
}

# classify rotated textures
print('Rotated images matched against references using LBP:')
print('original: brick, rotated: 30deg, match result: ',
      match(refs, rotate(brick, angle=30, resize=False)))
print('original: brick, rotated: 70deg, match result: ',
      match(refs, rotate(brick, angle=70, resize=False)))
print('original: grass, rotated: 145deg, match result: ',
      match(refs, rotate(grass, angle=145, resize=False)))
예제 #6
0
import numpy as np
from skimage import data
import matplotlib.pyplot as plt
import cv2

im = data.gravel()
im = im[:128,:128]

#Sobel Mask
Gx = cv2.Sobel(im,cv2.CV_16S,1,0,ksize=3)
Gy = cv2.Sobel(im,cv2.CV_16S,0,1,ksize=3)
#Magnitude and Theta
M = np.hypot(Gx,Gy)
max_m = np.amax(M)
theta = np.arctan2(Gy,Gx)
print(theta)
# Normalization
M *= 255 / max_m
theta += np.pi 
theta *= 90 /np.pi
print(theta)
#create HSV image
hsv = np.ndarray((im.shape[0],im.shape[1],3),dtype=np.uint8)
hsv[:,:,0] = theta
hsv[:,:,1] = np.full((im.shape[0],im.shape[1]),255)
hsv[:,:,2] = M
out = cv2.cvtColor(hsv,cv2.COLOR_HSV2RGB)
plt.imshow(out)