def _region_features_for(histone, dna, region): pixels0 = histone[region].ravel() pixels1 = dna[region].ravel() bin0 = pixels0 > histone.mean() bin1 = pixels1 > dna.mean() overlap = [np.corrcoef(pixels0, pixels1)[0, 1], (bin0 & bin1).mean(), (bin0 | bin1).mean()] spi = mh.sobel(histone, just_filter=1) sp = spi[mh.erode(region)] sdi = mh.sobel(dna, just_filter=1) sd = sdi[mh.erode(region)] sobels = [ np.dot(sp, sp) / len(sp), np.abs(sp).mean(), np.dot(sd, sd) / len(sd), np.abs(sd).mean(), np.corrcoef(sp, sd)[0, 1], np.corrcoef(sp, sd)[0, 1] ** 2, sp.std(), sd.std(), ] return np.concatenate( [ [region.sum()], haralick(histone * region, ignore_zeros=True).mean(0), haralick(dna * region, ignore_zeros=True).mean(0), overlap, sobels, haralick(mh.stretch(sdi * region), ignore_zeros=True).mean(0), haralick(mh.stretch(spi * region), ignore_zeros=True).mean(0), ] )
def _region_features_for(histone, dna, region): pixels0 = histone[region].ravel() pixels1 = dna[region].ravel() bin0 = pixels0 > histone.mean() bin1 = pixels1 > dna.mean() overlap = [ np.corrcoef(pixels0, pixels1)[0, 1], (bin0 & bin1).mean(), (bin0 | bin1).mean(), ] spi = mh.sobel(histone, just_filter=1) sp = spi[mh.erode(region)] sdi = mh.sobel(dna, just_filter=1) sd = sdi[mh.erode(region)] sobels = [ np.dot(sp, sp) / len(sp), np.abs(sp).mean(), np.dot(sd, sd) / len(sd), np.abs(sd).mean(), np.corrcoef(sp, sd)[0, 1], np.corrcoef(sp, sd)[0, 1]**2, sp.std(), sd.std(), ] return np.concatenate([ [region.sum()], haralick(histone * region, ignore_zeros=True).mean(0), haralick(dna * region, ignore_zeros=True).mean(0), overlap, sobels, haralick(mh.stretch(sdi * region), ignore_zeros=True).mean(0), haralick(mh.stretch(spi * region), ignore_zeros=True).mean(0), ])
def get_focus(movie, direction): x = [] s = movie.shape if direction == 0: for i in range(s[0]): x.append(mh.sobel(movie[i, :, :], just_filter=True)) return np.array(x) elif direction == 1: for i in range(s[1]): x.append(mh.sobel(movie[:, i, :], just_filter=True)) return np.array(x) elif direction == 2: for i in range(s[2]): x.append(mh.sobel(movie[:, :, i], just_filter=True)) return np.array(x)
def filter_channel(im): '''Compute filtered versions of an image Parameters ---------- im : ndarray input image (2D) Returns ------- fs : ndarray filtered version of the image (3D array) ''' import numpy as np import mahotas as mh Tg = im > im.mean() T2g = im > (im.mean() + 2*im.std()) zim = im-im.mean() zim /= im.std() z8 = mh.gaussian_filter(zim, 8) z4 = mh.gaussian_filter(zim, 4) z12 = mh.gaussian_filter(zim, 12) z16 = mh.gaussian_filter(zim, 16) zdiff = z16 - z8 sob = mh.sobel(im, just_filter=True) return np.dstack([ Tg, T2g, zim, z4, z8, z12, z16, zdiff, sob])
def filter_channel(im): '''Compute filtered versions of an image Parameters ---------- im : ndarray input image (2D) Returns ------- fs : ndarray filtered version of the image (3D array) ''' import numpy as np import mahotas as mh Tg = im > im.mean() T2g = im > (im.mean() + 2 * im.std()) zim = im - im.mean() zim /= im.std() z8 = mh.gaussian_filter(zim, 8) z4 = mh.gaussian_filter(zim, 4) z12 = mh.gaussian_filter(zim, 12) z16 = mh.gaussian_filter(zim, 16) zdiff = z16 - z8 sob = mh.sobel(im, just_filter=True) return np.dstack([Tg, T2g, zim, z4, z8, z12, z16, zdiff, sob])
def edginess_sobel(image): ''' edgi = edginess_sobel(image) Measure the "edginess" of an image ''' edges = mh.sobel(image, just_filter=True) edges = edges.ravel() return np.sqrt(np.dot(edges, edges))
def stack(image): stack,h,w = image.shape focus = np.array([mh.sobel(t, just_filter=True) for t in image]) best = np.argmax(focus, 0) image = image.reshape((stack,-1)) image = image.transpose() r = image[np.arange(len(image)), best.ravel()] r = r.reshape((h,w)) return r
def test_overlay(): im = mh.demos.load('luispedro', as_grey=1) im = mh.stretch(im) assert np.all(mh.overlay(im).max(2) == im) edges = mh.sobel(im) im3 = mh.overlay(im, green=edges) assert np.all(im3[:, :, 0] == im) assert np.all(im3[:, :, 2] == im) assert np.all(im3[:, :, 1] >= im)
def test_overlay(): im = mh.demos.load('luispedro', as_grey=1) im = mh.stretch(im) assert np.all(mh.overlay(im).max(2) == im) edges = mh.sobel(im) im3 = mh.overlay(im, green=edges) assert np.all(im3[:,:,0] == im) assert np.all(im3[:,:,2] == im) assert np.all(im3[:,:,1] >= im )
def edginess_sobel(image): """ Computes edges of the image using Sobel's algorithm and lighter areas appear more edgier. Computed edge points are squared and added to give one global feature per image. """ edges = mh.sobel(image,just_filter=True) edges = edges.ravel() return np.sqrt(np.dot(edges,edges))
def edginess_sobel(image): '''Measure the "edginess" of an image image should be a 2d numpy array (an image) Returns a floating point value which is higher the "edgier" the image is. ''' edges = mh.sobel(image, just_filter=True) edges = edges.ravel() return np.sqrt(np.dot(edges, edges))
def sobel(i): #what does a sobel look like? x = np.zeros((45, 384, 3)) for j in range(3): slc = load_in_movie(path_to_movie)[i, :, :, j] s = mh.sobel(slc, just_filter=True) s = ((s - np.min(s)) / (np.max(s) - np.min(s))) * 255 x[..., j] = 255 - s arr = Image.fromarray(x.astype("uint8")) arr.save("/Users/loganjaeger/Desktop/aerogel/sobel/{}.png".format( str(i + 1)))
def stack_per_channel(movie): stack, h, w = movie.shape focus = np.array( [mh.sobel(t, just_filter=True) for t in movie] ) #Using sobel to determine "infocusness of each pixel". This measure is somewhat arbitrary, and I tried it using laplacian filter and entropy as well best = np.argmax(focus, 0) image = movie image = image.reshape((stack, -1)) # image is now (stack, nr_pixels) image = image.transpose() # image is now (nr_pixels, stack) r = image[np.arange(len(image)), best.ravel()] # Select the right pixel at each location r = r.reshape((h, w)) # reshape to get final result return r * 255
def stack_one_channel(channel, save=False): movie = load_in_movie(path_to_movie)[:, :, :, channel] d, h, w = movie.shape focus = np.array([mh.sobel(t, just_filter=True) for t in movie]) print(focus.shape) best = np.argmax(focus, 0) movie = movie.reshape((d, -1)) # movie is now (d, nr_pixels) movie = movie.transpose() # movie is now (nr_pixels, d) r = movie[np.arange(len(movie)), best.ravel()] # Select the right pixel at each location r = r.reshape((h, w)) # reshape to get final result if save: r = Image.fromarray(r) r.save(os.path.join(save_path, "stacked_img.png")) return r
def sobel(dirs): # Create acutance feature: features = [] # store local feature descriptors for idir in range(len(dirs)): files = [name for name in os.listdir(dirs[idir]) if os.path.isfile(os.path.join(dirs[idir], name))] for ifile in range(len(files)): if files[ifile][-3:] != "jpg": # ignore non-image files continue image = mh.imread(dirs[idir] + files[ifile]).astype(np.uint8) # read image if len(image.shape) == 3: # convert to gray if colored image = mh.colors.rgb2gray(image, dtype=np.uint8) # Calculate the object edges edges = mh.sobel(image, just_filter=True) edges = edges.ravel() # flatten the array features.append(np.sqrt(np.dot(edges, edges))) features = np.array(features) return features
def edginess_sobel(im): edges = mh.sobel(im, just_filter=True) edges = edges.ravel() return np.sqrt(np.dot(edges, edges))
import mahotas import numpy as np from pylab import imshow, gray, show, subplot from os import path lena_image = path.join( path.dirname(path.abspath(__file__)), 'data', 'lena.jpg') photo = mahotas.imread(lena_image, as_grey=True) photo = photo.astype(np.uint8) gray() subplot(131) imshow(photo) edge_sobel = mahotas.sobel(photo) subplot(132) imshow(edge_sobel) edge_dog = mahotas.dog(photo) subplot(133) imshow(edge_dog) show()
# This code is supporting material for the book # Building Machine Learning Systems with Python # by Willi Richert and Luis Pedro Coelho # published by PACKT Publishing # # It is made available under the MIT License from matplotlib import pyplot as plt import numpy as np import mahotas as mh image = mh.imread('../1400OS_10_01.jpeg') image = mh.colors.rgb2gray(image, dtype=np.uint8) image = image[::4, ::4] thresh = mh.sobel(image) filtered = mh.sobel(image, just_filter=True) thresh = mh.dilate(thresh, np.ones((7, 7))) filtered = mh.dilate(mh.stretch(filtered), np.ones((7, 7))) h, w = thresh.shape canvas = 255 * np.ones((h, w * 2 + 64), np.uint8) canvas[:, :w] = thresh * 255 canvas[:, -w:] = filtered mh.imsave('../1400OS_10_09+.jpg', canvas)
def test_sobel_pure(): f = np.random.random((64, 128)) f2 = f.copy() _ = mh.sobel(f) assert np.all(f == f2)
def test_find_edge(): f = np.zeros((32,48)) f[:,16:] = 1 f = mh.gaussian_filter(f,4) fs = mh.sobel(f) assert np.sum(fs[:,15:17] > 0) >= 32
def test_find_edge(): f = np.zeros((32,48)) f[:,16:] = 1 f = mh.gaussian_filter(f,4) fs = mh.sobel(f) assert np.all(fs[:,15] > 0)
import mahotas import numpy as np from pylab import imshow, gray, show, subplot from os import path lena_image = path.join(path.dirname(path.abspath(__file__)), 'data', 'lena.jpg') photo = mahotas.imread(lena_image, as_grey=True) photo = photo.astype(np.uint8) gray() subplot(131) imshow(photo) edge_sobel = mahotas.sobel(photo) subplot(132) imshow(edge_sobel) edge_dog = mahotas.dog(photo) subplot(133) imshow(edge_dog) show()