def test_watershed_ift07(self): shape = (7, 6) data = np.zeros(shape, dtype=np.uint8) data = data.transpose() data[...] = np.array([[0, 1, 0, 0, 0, 1, 0], [0, 1, 0, 0, 0, 1, 0], [0, 1, 0, 0, 0, 1, 0], [0, 1, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]], np.uint8) markers = np.array([[-1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 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, 0, 0, 0, 0, 0, 0]], np.int8) out = np.zeros(shape, dtype=np.int16) out = out.transpose() ndimage.watershed_ift(data, markers, structure=[[1, 1, 1], [1, 1, 1], [1, 1, 1]], output=out) expected = [[-1, 1, 1, 1, 1, 1, -1], [-1, 1, 1, 1, 1, 1, -1], [-1, 1, 1, 1, 1, 1, -1], [-1, 1, 1, 1, 1, 1, -1], [-1, -1, -1, -1, -1, -1, -1], [-1, -1, -1, -1, -1, -1, -1]] assert_array_almost_equal(out, expected)
def do_watershed(image, markers, tfile, shape, bstruct, algorithm, mg_size, use_ww_wl, wl, ww, q): mask = np.memmap(tfile, shape=shape, dtype='uint8', mode='r+') if use_ww_wl: if algorithm == 'Watershed': tmp_image = ndimage.morphological_gradient( get_LUT_value(image, ww, wl).astype('uint16'), mg_size) tmp_mask = watershed(tmp_image, markers.astype('int16'), bstruct) else: tmp_image = get_LUT_value(image, ww, wl).astype('uint16') #tmp_image = ndimage.gaussian_filter(tmp_image, self.config.mg_size) #tmp_image = ndimage.morphological_gradient( #get_LUT_value(image, ww, wl).astype('uint16'), #self.config.mg_size) tmp_mask = watershed_ift(tmp_image, markers.astype('int16'), bstruct) else: if algorithm == 'Watershed': tmp_image = ndimage.morphological_gradient( (image - image.min()).astype('uint16'), mg_size) tmp_mask = watershed(tmp_image, markers.astype('int16'), bstruct) else: tmp_image = (image - image.min()).astype('uint16') #tmp_image = ndimage.gaussian_filter(tmp_image, self.config.mg_size) #tmp_image = ndimage.morphological_gradient((image - image.min()).astype('uint16'), self.config.mg_size) tmp_mask = watershed_ift(tmp_image, markers.astype('int8'), bstruct) mask[:] = tmp_mask mask.flush() q.put(1)
def do_watershed(image, markers, tfile, shape, bstruct, algorithm, mg_size, use_ww_wl, wl, ww, q): mask = np.memmap(tfile, shape=shape, dtype='uint8', mode='r+') if use_ww_wl: if algorithm == 'Watershed': tmp_image = ndimage.morphological_gradient( get_LUT_value(image, ww, wl).astype('uint16'), mg_size) tmp_mask = watershed(tmp_image, markers.astype('int16'), bstruct) else: tmp_image = get_LUT_value(image, ww, wl).astype('uint16') #tmp_image = ndimage.gaussian_filter(tmp_image, self.config.mg_size) #tmp_image = ndimage.morphological_gradient( #get_LUT_value(image, ww, wl).astype('uint16'), #self.config.mg_size) tmp_mask = watershed_ift(tmp_image, markers.astype('int16'), bstruct) else: if algorithm == 'Watershed': tmp_image = ndimage.morphological_gradient((image - image.min()).astype('uint16'), mg_size) tmp_mask = watershed(tmp_image, markers.astype('int16'), bstruct) else: tmp_image = (image - image.min()).astype('uint16') #tmp_image = ndimage.gaussian_filter(tmp_image, self.config.mg_size) #tmp_image = ndimage.morphological_gradient((image - image.min()).astype('uint16'), self.config.mg_size) tmp_mask = watershed_ift(tmp_image, markers.astype('int8'), bstruct) mask[:] = tmp_mask mask.flush() q.put(1)
def guess_corners(bw): """ Infer the corners of an image using a Sobel filter to find the edges and a Harris filter to find the corners. Takes only as single color chanel. Parameters ---------- bw : (m x n) ndarray of ints Returns ------- corners : pixel coordinates of plot corners, unsorted outline : (m x n) ndarray of bools True -> plot area """ assert len(bw.shape) == 2 bw = img_as_uint(bw) e_map = ndimage.sobel(bw) markers = np.zeros(bw.shape, dtype=int) markers[bw < 30] = 1 markers[bw > 150] = 2 seg = ndimage.watershed_ift(e_map, np.asarray(markers, dtype=int)) outline = ndimage.binary_fill_holes(1 - seg) corners = harris(np.asarray(outline, dtype=int)) corners = approximate_polygon(corners, 1) return corners, outline
def test_watershed_ift02(self): data = np.array([[0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 1, 1, 1, 0], [0, 1, 0, 0, 0, 1, 0], [0, 1, 0, 0, 0, 1, 0], [0, 1, 0, 0, 0, 1, 0], [0, 1, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]], np.uint8) markers = np.array([[-1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 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, 0, 0, 0, 0, 0, 0]], np.int8) out = ndimage.watershed_ift(data, markers) expected = [[-1, -1, -1, -1, -1, -1, -1], [-1, -1, 1, 1, 1, -1, -1], [-1, 1, 1, 1, 1, 1, -1], [-1, 1, 1, 1, 1, 1, -1], [-1, 1, 1, 1, 1, 1, -1], [-1, -1, 1, 1, 1, -1, -1], [-1, -1, -1, -1, -1, -1, -1], [-1, -1, -1, -1, -1, -1, -1]] assert_array_almost_equal(out, expected)
def test_watershed_ift05(self): data = np.array([[0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 1, 1, 1, 0], [0, 1, 0, 1, 0, 1, 0], [0, 1, 0, 1, 0, 1, 0], [0, 1, 0, 1, 0, 1, 0], [0, 1, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0]], np.uint8) markers = 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, 3, 0, 2, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, -1]], np.int8) out = ndimage.watershed_ift(data, markers, structure=[[1, 1, 1], [1, 1, 1], [1, 1, 1]]) expected = [[-1, -1, -1, -1, -1, -1, -1], [-1, 3, 3, 2, 2, 2, -1], [-1, 3, 3, 2, 2, 2, -1], [-1, 3, 3, 2, 2, 2, -1], [-1, 3, 3, 2, 2, 2, -1], [-1, 3, 3, 2, 2, 2, -1], [-1, -1, -1, -1, -1, -1, -1]] assert_array_almost_equal(out, expected)
def watershed_fixed(im,labels,d=0,suppression=3,intensity=False): import pymorph def grad_mag(img): x,y = np.gradient(img.astype('float')) return np.clip( np.round( np.sqrt( np.square(x)+np.square(y)) ) , 0 , 255 ).astype('uint8') if d > 0: new_labels = np.zeros_like(labels) for l in range(1,labels.max()+1): new_labels[erode_by(labels==l, d, min_size=1)] = l labels=new_labels # scipy.misc.imsave("dilate_test.png", labels_to_edges(labels)) im = pymorph.hmin(im if not intensity else grad_mag(im),suppression) return ndimage.watershed_ift(im, labels)
def test_watershed_ift08(self): # Test cost larger than uint8. See gh-10069. shape = (2, 2) data = np.array([[256, 0], [0, 0]], np.uint16) markers = np.array([[1, 0], [0, 0]], np.int8) out = ndimage.watershed_ift(data, markers) expected = [[1, 1], [1, 1]] assert_array_almost_equal(out, expected)
def test_watershed09(self): """Test on an image of reasonable size This is here both for timing (does it take forever?) and to ensure that the memory constraints are reasonable """ image = np.zeros((1000, 1000)) coords = np.random.uniform(0, 1000, (100, 2)).astype(int) markers = np.zeros((1000, 1000), int) idx = 1 for x, y in coords: image[x, y] = 1 markers[x, y] = idx idx += 1 image = ndi.gaussian_filter(image, 4) watershed(image, markers, self.eight) ndi.watershed_ift(image.astype(np.uint16), markers, self.eight)
def watershed(im,labels,d=0,suppression=3): import pymorph if d > 0: # hack: dilate edges instead of eroding structures edges = dilate(labels_to_edges(labels),d) # grad = np.gradient(labels) # edges = dilate(np.maximum(abs(grad[0]),abs(grad[1]))>0,d) labels[edges] = 0 im = pymorph.hmin(im,suppression) return ndimage.watershed_ift(im, labels)
def watershed(I, markers, thresh=0.5): I -= np.min(I) I = I / np.max(I) from skimage import img_as_ubyte I = img_as_ubyte(I) # xm, ym, zm = np.ogrid[0:I.shape[0]:10, 0:I.shape[1]:10, 0:I.shape[2]:10] markers = ((markers > 0) * 1.0).astype(np.int16) markers = ndimage.label(markers)[0] # markers[xm, ym, zm]= np.arange(xm.size*ym.size*zm.size).reshape((xm.size,ym.size, zm.size)) ws = ndimage.watershed_ift(I, markers) return ws
def _mask_on_fwhm(self): self.brind = np.copy(self.rind) # Mask the pixels below the threshold self.brind[self.rind != self.cnum] = 0 self.brind[self.rind == self.cnum] = 1 self.brind[self.mflux < self.fwhm] = 0 # Closing to dilate and erode the masked pixels self.fwhm_mask = binary_closing(self.brind) # Watershed to select pixels contiguous with the peak pixel markers = np.zeros(self.fwhm_mask.size).reshape(self.fwhm_mask.shape) markers[self.max1, self.max0] = 2 markers[~self.fwhm_mask] = 1 self.fwhm_mask = watershed_ift(self.fwhm_mask.astype(np.uint8), markers.astype(np.int16)) # Remask where watershed self.fwhm_mask[self.fwhm_mask != 2] = 0 self.fwhm_mask[self.fwhm_mask == 2] = 1
def watershed(affs, seed_method, has_background=True, curr_log_dir=''): affs_xy = 1.0 - 0.5 * (affs[1] + affs[2]) depth = affs_xy.shape[0] # (z, y, x) fragments = np.zeros_like(affs[0]).astype(np.uint64) next_id = 1 distances = np.zeros_like(affs_xy) seeds_list = np.zeros_like(affs_xy) for z in range(depth): seed_data = get_seeds(affs_xy[z], next_id=next_id, method=seed_method) if seed_method == 'maxima_distance': seeds, num_seeds, distance = seed_data if has_background: min_dist = np.min(distance) lowest = np.array(np.where(distance == min_dist)) chosen_points = np.random.choice(np.arange(0, lowest.shape[1]), 20) background_points = np.take(lowest, chosen_points, axis=1) background_points = tuple(background_points[i] for i in range(len(seeds.shape))) num_seeds += 1 + next_id print(f'background_point {background_points}') print(f"num_seeds {num_seeds}") print(f"max_seed {np.max(seeds)}") seeds[background_points] = num_seeds distances[z] = distance seeds_list[z] = seeds else: seeds, num_seeds = seed_data if use_mahotas_watershed: fragments[z] = mahotas.cwatershed(affs_xy[z], seeds) else: fragments[z] = ndimage.watershed_ift( (255.0 * affs_xy[z]).astype(np.uint8), seeds) next_id += num_seeds return fragments, affs_xy, distances, seeds_list
def watershed(affs, seed_method, use_mahotas_watershed=True): affs_xy = 1.0 - 0.5 * (affs[1] + affs[2]) depth = affs_xy.shape[0] fragments = np.zeros_like(affs[0]).astype(np.uint64) next_id = 1 for z in range(depth): seeds, num_seeds = get_seeds(affs_xy[z], next_id=next_id, method=seed_method) if use_mahotas_watershed: fragments[z] = mahotas.cwatershed(affs_xy[z], seeds) else: fragments[z] = ndimage.watershed_ift( (255.0 * affs_xy[z]).astype(np.uint8), seeds) next_id += num_seeds return fragments
def epi_mask(in_file, out_file=None): """Use grayscale morphological operations to obtain a quick mask of EPI data.""" from pathlib import Path import nibabel as nb import numpy as np from scipy import ndimage from skimage.morphology import ball if out_file is None: out_file = Path("mask.nii.gz").absolute() img = nb.load(in_file) data = img.get_fdata(dtype="float32") # First open to blur out the skull around the brain opened = ndimage.grey_opening(data, structure=ball(3)) # Second, close large vessels and the ventricles closed = ndimage.grey_closing(opened, structure=ball(2)) # Window filter on percentile 30 closed -= np.percentile(closed, 30) # Window filter on percentile 90 of data maxnorm = np.percentile(closed[closed > 0], 90) closed = np.clip(closed, a_min=0.0, a_max=maxnorm) # Calculate index of center of masses cm = tuple( np.round(ndimage.measurements.center_of_mass(closed)).astype(int)) # Erode the picture of the brain by a lot eroded = ndimage.grey_erosion(closed, structure=ball(5)) # Calculate the residual wshed = opened - eroded wshed -= wshed.min() wshed = np.round(1e3 * wshed / wshed.max()).astype(np.uint16) markers = np.zeros_like(wshed, dtype=int) markers[cm] = 2 markers[0, 0, -1] = -1 # Run watershed labels = ndimage.watershed_ift(wshed, markers) hdr = img.header.copy() hdr.set_data_dtype("uint8") nb.Nifti1Image( ndimage.binary_dilation(labels == 2, ball(2)).astype("uint8"), img.affine, hdr).to_filename(out_file) return out_file
def watershed2(ndsm, initialMask, initialMarkers, veggieMask): veggieMask2 = morphology.opening(veggieMask, morphology.square(3)) # veggieMask3 = morphology.erosion(veggieMask2,morphology.square(2)) ndsm[veggieMask2 == 0] = 0 ndsmNorm = normalizeRange(ndsm, 0, 255) initialMarkers[initialMask == 0] = 0 # Bigger background markers initialMarkers[ndsm < 2] = -1 wshed = ndimage.watershed_ift(input=ndsmNorm, markers=initialMarkers) wshed[ndsm < 3] = 0 wshed[veggieMask == 0] = 0 wshed[wshed < 0] = 0 return wshed
def watershed_segment_2(M,click_coords): """Use watershed segmentation on an array. Return an array where regions are given different integer labels""" # todo: choose these structures based on aspect ratio of M and input parameters sel = np.ones((4,10)) # for opening sel2 = np.ones((15,75)) # for local thresholding sel3 = np.ones((2,5)) # for erosion # get a few points in the center of each blob # threshold #bw = ((M>=ndi.percentile_filter(M,80,footprint=sel2)) & (M>=scoreatpercentile(M.flatten(),60))) score = stats.percentileofscore(M.flatten(),M[int(click_coords[0][1]),int(click_coords[0][0])]) bw = (M>=stats.scoreatpercentile(M.flatten(),score)) # open and erode #bools = sp.zeros((M.shape[0],M.shape[1]),int) #bools[int(click_coords[0]),int(click_coords[1])] = 1 #blobs = sp.where(bools == 1,True,False) blobs = snm.binary_opening(bw,structure=sel) blobs = snm.binary_dilation(blobs,iterations=3) blobs = snm.binary_erosion(blobs,structure=sel3) # label labels,_ = ndi.label(blobs) labels[labels > 0] += 1 #labels[0,0] = 1 # rescale and cast to int16, then use watershed M2 = rescaled(M,0,65000).astype(np.uint16) newlabels = ndi.watershed_ift(M2,labels) # get rid of groups unless they have the right number of pixels counts = np.bincount(newlabels.flatten()) old2new = np.arange(len(counts)) old2new[(counts < 100) | (counts > 600)] = 0 newlabels = old2new[newlabels] return newlabels
def watershed(ndsm, initialMask, initialMarkers, veggieMask): veggieMask2 = morphology.opening(veggieMask, morphology.square(3)) # veggieMask3 = morphology.erosion(veggieMask2,morphology.square(2)) ndsm[veggieMask2 == 0] = 0 ndsmNorm = normalizeRange(ndsm, 0, 255) initialMarkers[initialMask == 0] = 0 # Look for first 0 pixel, mark with -1 for background for y in xrange(len(ndsm)): for x in xrange(len(ndsm)): if (ndsm[y][x] == 0): break initialMarkers[y][x] = -1 wshed = ndimage.watershed_ift(input=ndsmNorm, markers=initialMarkers) # wshed[ndsm<3] = 0 # wshed[veggieMask==0] = 0 # wshed[wshed<0] = 0 return wshed
def autoMask(img, perc_radius=None, debug_bool=False): from scipy import ndimage da = np.uint16(img) COG = tuple(map(int, ndimage.center_of_mass(img))) markers = np.zeros(da.shape, dtype=np.int8) #set outside brain to 1 markers.flat[0] = 1 #set inside brain to 2 markers.flat[np.ravel_multi_index(COG, markers.shape)] = 2 mask = ndimage.watershed_ift(da, markers)-1 if debug_bool: print len(np.flatnonzero(mask)) if isinstance(perc_radius, float): #find distance to background dm = ndimage.distance_transform_bf(mask) #find radius of phantom (i.e. centre is most distant from background) radius = dm.flatten().max() #select only pixel outside range desired sel = np.flatnonzero(dm.flatten() <= (1.-perc_radius)*radius) #set those pixels to zero in the final mask mask.flat[sel] = 0 if debug_bool: print len(np.flatnonzero(mask)), radius, (1. -perc_radius)*radius return np.uint8(mask)
def findColorRegions(picture, res): print "Counting Color Regions:", os.path.basename(picture) , '(', res, ')...', struct = np.array([[0,1,0], [1,1,1], [0,1,0]]) ##open image im = Image.open(picture) #im.show() im = im.convert("L") xsize, ysize = im.size if (xsize*res < 60) or (ysize*res < 60): print "Image to small for given resolution..." res = 60.0 / min([xsize, ysize]) print "New resolution for this image:", res im = im.resize((xsize*res, ysize*res),Image.ANTIALIAS) xsize, ysize = im.size im = fromimage(im) im = scipy.transpose(im) ##reduce colors im = scipy.divide(im, 10) im = im *10 ##make markers mark = 0 markers = np.zeros_like(im).astype('int') pd = 0 opd = 0 for x in range(xsize): pd = int(x/xsize*.5) if pd > opd: print '.', opd = pd for y in range(ysize): if (x%(int(xsize/30)) == 0) and (y%(int(ysize/30)) == 0): mark += 1 markers[x,y] = mark ##run watershed water = ndimage.watershed_ift(im.astype('uint8'), markers, structure = struct) ##make some masks and count the size of each region sizecount = [] marks = range(mark+1) for index in range(len(marks)): sizecount.append([]) pd = 0 opd = 0 for x in range(0,xsize): for y in range(0,ysize): sizecount[marks.index(water[x,y])].append((x,y)) ##make markers based on large regions mark = 0 shapes = 0 markers = np.zeros_like(im).astype('int') for mark in marks: if len(sizecount[marks.index(mark)]) >= (xsize/30 + ysize/30)/2: #should be a better ratio shapes += 1 print shapes return shapes
# The watershed algorithm relies on the *flooding* of different basins, so # we need to put markers in the image to initiate the flooding. If one # knows approximately where the objects are, and there are only a few # objects, it is possible to set the markers by hand # # <codecell> #!python >>> markers = np.zeros_like(a).astype(int16) >>> markers[0, 0] = 1 >>> markers[200, 100] = 2 >>> markers[350, 400] = 3 >>> markers[260, 200] = 4 >>> res1 = ndimage.watershed_ift(a.astype(uint8), markers) >>> np.unique1d(res1) # pixels are tagged according to the object they belong to array([1, 2, 3, 4], dtype=int16) >>> imshow(res1, cmap=cm.jet) # central plot in the image above # <markdowncell> # If you don't know where to put the markers, and you know the minimal # size of the objects, you can spread a lot of markers on a grid finer # than the objects. # # <codecell> #!python >>> xm, ym = np.ogrid[0:512:10, 0:512:10]
# nbrs = NearestNeighbors(n_neighbors=2, algorithm='ball_tree').fit(myarray) # distances, indices = nbrs.kneighbors(myarray) # print myarray # print distances # print indices # print nbrs src = "/media/vortex/16DE-33641/LORENZO/test_clipped/m1995.tif" ds = gdal.Open(src) print ds myarray = np.array(ds.GetRasterBand(1).ReadAsArray()) x, y = myarray.shape test = np.random.randint(5, size=(x, y)) #print test import numpy as np from scipy import ndimage input = np.array( [[0, 0, 0, 0, 0, 0, 0], [0, 1, 100, 1, 1, 1, 0], [0, 1, 0, 0, 0, 1, 0], [0, 1, 77, 0, 0, 1, 0], [0, 1, 0, 0, 0, 1, 0], [0, 100, 1, 1, 100, 1, 0], [0, 89, 0, 100, 0, 0, 100]], np.uint8) markers = np.array( [[1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 2, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 3, 0], [0, 0, 0, 0, 0, 0, 3]], np.int8) #print ndimage.watershed_ift(input, markers) print ndimage.watershed_ift(myarray, test)
def cluster(array, method='watershed', seed='local_min', seed_min_d=1, **filter_args): """ Segment image in area around selected seeds :Input: array: The input array to label method: The clustering method. One of: 'gradient' - call shortest_path_cluster(...) => require skimage 'nearest' - cluster pixels to closest seed 'watershed' (default) - use scipy.ndimage.watershed_ift seed: The seed to cluster image around. Can be either an interger array where seeds are none zeros cells, or a string indicating which method to use to select seeds. In the later case, the value must be one of the valid 'method' of the find_seed() function - See find_seed() documentation - seed_min_d: Optional arguments passed to find_seed(...) Other key-value arguments can be given for initial filtering, which are passed to ndarray.filter.apply(...) function. Note that the order in which filters are processed is not garantied :Output: the labeled image, interger array of values in [0,N] the number of segmented area (N) :Example of postprocessing: #To have the minimum, maximum and mean value of label area import numpy as np import scipy.ndimage as nd label,N = label(img) lab_max = np.array(nd.maximum(img,label,index=np.arange(0,N+1))) lab_min = np.array(nd.minimum(img,label,index=np.arange(0,N+1))) lab_mean = np.array(nd.mean (img,label,index=np.arange(0,N+1))) # to get a map of these values onto the labeled area # (eg. for visualisation or further image processing) min_map = lab_min [label] max_map = lab_max [label] mean_map = lab_mean[label] See scipy.ndimage for other processing of label map """ # filter image image = _filter(array, **filter_args) # find seeds if isinstance(seed, basestring): seed, N = find_seed(image, method=seed, min_distance=seed_min_d) else: N = seed.max() # label the image using selected clustering method if cluster == 'gradient': # cluster pixels to seed such that connecting path minize sum of gradient norm try: label = shortest_path_cluster(_grad_norm(image), seed, geometric=True) except ImportError: print 'Error importing skimage, switch to "watershed" method' label, N = label(image, cluster='ift', seed=seed, seed_min_d=1) elif cluster == 'nearest': # indices map to the closest seed label = _fill(seed, seed != 0) else: # cluster=='watershed' # use scipy.ndimage.watershed_ift image = _np.asarray(_linear_map(image, min=0, max=1) * 255, dtype='uint8') ## image auto-conversion tool label = _nd.watershed_ift(input=image, markers=seed) return label, N
def cluster(array, method='watershed', seed='local_min', seed_min_d=1, **filter_args): """ Segment image in area around selected seeds :Input: array: The input array to label method: The clustering method. One of: 'gradient' - call shortest_path_cluster(...) => require skimage 'nearest' - cluster pixels to closest seed 'watershed' (default) - use scipy.ndimage.watershed_ift seed: The seed to cluster image around. Can be either an interger array where seeds are none zeros cells, or a string indicating which method to use to select seeds. In the later case, the value must be one of the valid 'method' of the find_seed() function - See find_seed() documentation - seed_min_d: Optional arguments passed to find_seed(...) Other key-value arguments can be given for initial filtering, which are passed to ndarray.filter.apply(...) function. Note that the order in which filters are processed is not garantied :Output: the labeled image, interger array of values in [0,N] the number of segmented area (N) :Example of postprocessing: #To have the minimum, maximum and mean value of label area import numpy as np import scipy.ndimage as nd label,N = label(img) lab_max = np.array(nd.maximum(img,label,index=np.arange(0,N+1))) lab_min = np.array(nd.minimum(img,label,index=np.arange(0,N+1))) lab_mean = np.array(nd.mean (img,label,index=np.arange(0,N+1))) # to get a map of these values onto the labeled area # (eg. for visualisation or further image processing) min_map = lab_min [label] max_map = lab_max [label] mean_map = lab_mean[label] See scipy.ndimage for other processing of label map """ # filter image image = _filter(array,**filter_args) # find seeds if isinstance(seed,basestring): seed,N = find_seed(image,method=seed,min_distance=seed_min_d) else: N = seed.max() # label the image using selected clustering method if cluster=='gradient': # cluster pixels to seed such that connecting path minize sum of gradient norm try: label = shortest_path_cluster(_grad_norm(image),seed,geometric=True) except ImportError: print 'Error importing skimage, switch to "watershed" method' label,N = label(image,cluster='ift',seed=seed,seed_min_d=1) elif cluster=='nearest': # indices map to the closest seed label = _fill(seed,seed!=0) else: # cluster=='watershed' # use scipy.ndimage.watershed_ift image = _np.asarray(_linear_map(image,min=0,max=1)*255,dtype='uint8') ## image auto-conversion tool label = _nd.watershed_ift(input=image, markers=seed) return label,N
# lst.append() img_gray = coins() #分水岭不可以用高斯滤波,只能用锐化 #img_gray=gaussian_filter(img_gray,2) fig1 = plt.figure('原图') edge = sobel(img_gray).astype(np.uint8) # 制作掩膜 markers = np.zeros_like(img_gray).astype(np.int16) markers[img_gray < 30] = 1 markers[img_gray > 150] = 2 img = np.zeros_like(img_gray, dtype=np.int16) markers.astype(np.uint8) #调用sicpy的分水岭 watershed_ift(input=img_gray.astype(np.uint8), markers=markers, output=img) plt.subplot(121) plt.imshow(img) img = img // 2 plt.subplot(121) img_fill = np.zeros_like(img, dtype=np.uint8) binary_fill_holes(img, output=img_fill) img_fill *= 255 plt.imshow(img_fill) img_lab = np.zeros_like(img_fill, dtype=np.uint8) #创建标签 label(img_fill, generate_binary_structure(2, 1), output=img_lab) plt.subplot(122) #img_lab[img_lab==2]=0 #对像素点小于100的进行过滤
def generateInitialMask(ndsm,classified,slope,numret): print "Preparing Watershed base..." ndsm_gray = normalizeRange(ndsm,0,255) # Prepare Watershed Base # Remove Vegetation ndsm_noveg = copy.deepcopy(ndsm_gray) ndsm_noveg[classified==5] = 0 # Perform Morphology ndsm_noveg_open = morphology.opening(ndsm_noveg,morphology.square(3)) slope_thresh = slope > 60 edges = copy.deepcopy(ndsm_gray) edges[~slope_thresh] = 0 wshed_base = copy.deepcopy(ndsm_noveg_open) for y in xrange(len(ndsm_noveg_open)): for x in xrange(len(ndsm_noveg_open[0])): if slope_thresh[y][x] == True: # print "HERE" wshed_base[y][x] = ndsm_noveg_open[y][x] # Prepare Markers print "Preparing markers..." ndsm_thresh = ndsm > 2 slope_thresh = slope > 30 slope_morph = morphology.closing(slope_thresh,morphology.square(4)) markers_level1 = ndsm_thresh & ~slope_morph vegetation = classified == 5 markers_level2 = markers_level1 & ~vegetation markers_thresh = markers_level2!=0 markers_small_removed = morphology.remove_small_objects(markers_thresh,10) markers_level3,num_labels = ndimage.label(markers_small_removed) markers_level3[ndsm<1] = -1 markers_level3[classified==5] = -1 # Perform Watershed segmentation print "Performing region growing..." wshed = ndimage.watershed_ift(input=wshed_base,markers=markers_level3) wshed[wshed==-1] = 0 # Remove river artifacts print "Removing river artifacts..." returns = numret > 0 labels = list(np.unique(wshed)) labels = labels[1:] #1454, 741 # labels = [1454] area_thresh = 30 for label in labels: print label clone = copy.deepcopy(wshed) obj_slice = ndimage.find_objects(clone==label) # print obj_slice obj = clone[obj_slice[0][0],obj_slice[0][1]] obj[obj!=label] = 0 obj = obj.astype(bool) area_orig = np.bincount(obj.flatten())[1] if area_orig < area_thresh: wshed[wshed==label] = 0 returns_slice = returns[obj_slice[0][0],obj_slice[0][1]] intersect = returns_slice & obj intersect = intersect*1 area_intersect = np.count_nonzero(intersect) ratio = float(area_intersect)/float(area_orig) if ratio <0.2: wshed[wshed==label] = 0 return wshed
src = "/media/vortex/16DE-33641/LORENZO/test_clipped/m1995.tif" ds = gdal.Open(src) print ds myarray = np.array(ds.GetRasterBand(1).ReadAsArray()) x, y = myarray.shape test = np.random.randint(5, size=(x, y)) #print test import numpy as np from scipy import ndimage input = np.array([[0, 0, 0, 0, 0, 0, 0], [0, 1, 100, 1, 1, 1, 0], [0, 1, 0, 0, 0, 1, 0], [0, 1, 77, 0, 0, 1, 0], [0, 1, 0, 0, 0, 1, 0], [0, 100, 1, 1, 100, 1, 0], [0, 89, 0, 100, 0, 0, 100]], np.uint8) markers = np.array([[1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 2, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 3, 0], [0, 0, 0, 0, 0, 0, 3]], np.int8) #print ndimage.watershed_ift(input, markers) print ndimage.watershed_ift(myarray, test)
inputs = list() inputs.append( "/Users/pete/Temp/Hyperforest/Kersselaerspleyn_LiDAR_05m_pmfgrd_chmNN_median5_morphgrad_minima.env" ) inputs.append( "/Users/pete/Temp/Hyperforest/Kersselaerspleyn_LiDAR_05m_pmfgrd_chmNN_median5_morphgrad.env" ) outfile = "/Users/pete/Temp/Hyperforest/Kersselaerspleyn_LiDAR_05m_pmfgrd_chmNN_median5_watershed.img" reader = ImageReader(inputs, windowxsize=1000, windowysize=1000, overlap=100) writer = None # read through each block and apply scaling # and write into output file for (info, blocks) in reader: block1, block2 = blocks seeds = np.int32(block1) grad = np.uint16(block2) out = np.expand_dims(ndimage.watershed_ift(grad[0], seeds[0]), 0) if writer is None: writer = ImageWriter(outfile, info=info, firstblock=out) else: writer.write(out) print info.getPercent(), '%\r', print '100%\r', writer.close(calcStats=False)
def findShapes(picture, res): print "Counting Shapes:", os.path.basename(picture), '(', res, ')...', struct = np.array([[0,1,0], [1,1,1], [0,1,0]]) ##open image im = Image.open(picture) #im.show() im = im.convert("L") xsize, ysize = im.size if (xsize*res < 60) or (ysize*res < 60): print "Image to small for given resolution..." res = 60.0 / min([xsize, ysize]) print "New resolution for this image:", res im = im.resize((xsize*res, ysize*res), Image.ANTIALIAS) xsize, ysize = im.size im = im.filter(ImageFilter.FIND_EDGES) im = fromimage(im) im = scipy.transpose(im) im = scipy.divide(im, 10) im = im *10 ##make markers mark = 0 markers = np.zeros_like(im).astype('int') for x in range(xsize): for y in range(ysize): if (x%(int(xsize/30)) == 0) and (y%(int(ysize/30)) == 0): mark += 1 markers[x,y] = mark ##run watershed water = ndimage.watershed_ift(im.astype('uint8'), markers, structure = struct) #toimage(water).save("sw"+ os.path.basename(picture)) #debug output ##make some masks and count the size of each region sizecount = [] marks = range(mark+1) for index in range(len(marks)): sizecount.append(0) for x in range(0,xsize): for y in range(0,ysize): sizecount[marks.index(water[x,y])] += 1 ##make markers based on large regions mark = 0 shapes = 0 markers = np.zeros_like(im).astype('int') for mark in marks: if sizecount[marks.index(mark)] >= (xsize/30 + ysize/30)/2: shapes += 1 print shapes return shapes
if i>=0 and i<n and j>=0 and j<m and (i,j) != c] peak = N.sum(im[c] > goodlist) == len(goodlist) if peak: iniposn.append(c); inipeak.append(im[c]) nmulsrc = len(iniposn) if nmulsrc > 1: markers = N.zeros(im.shape, int) markers[0,0] = 1 for ipk in range(nmulsrc): pk = inipeak[ipk]; x, y = iniposn[ipk] markers[int(round(x)), int(round(y))] = ipk+2 im2 = N.zeros(im.shape, int) im1 = im1 - im1.min() im1 = im1/im1.max()*255 im1 = 255-im1 nd.watershed_ift(N.array(im1, N.uint8), markers, output = im2) fcn = MGFunction(im, isl.mask_active, 1) fit = lmder_fit gg1 = gg() for ipk in range(nmulsrc): ind = ipk+2 mom = func.momanalmask_gaus(im, im2, ind, 1.0, True) indd = N.where(im2==ind) mom[3] = 3.0; mom[4]=3.0 g = [float(N.max(im[indd])), int(round(mom[1])), int(round(mom[2])), mom[3]/fwsig, mom[4]/fwsig, mom[5]] gg1.add_gaussian(fcn, g, dof = isl.size_active) print g fit(fcn, final=0, verbose=True) print fcn.parameters import pylab as pl pl.figure()
def waterPanelize(picture): print "Water Panalizing:", os.path.basename(picture) struct = np.array([[0,1,0], [1,1,1], [0,1,0]]) ##open image im = Image.open(picture) imcopy = im #im.show() im = im.convert("L") xsize, ysize = im.size #prescaling im = im.resize((xsize*ops.scaling, ysize*ops.scaling)) xsize, ysize = im.size print "xSize:", xsize, "ySize:", ysize print '!' ##reduce colors im = scipy.divide(im, 30) im = im *30 #transpose for scipy im = scipy.transpose(im) print "im shape:", im.shape ##make markers mark = 0 markers = np.zeros_like(im).astype('int') for x in range(xsize): for y in range(ysize): if (x%(ops.markRes) == 0) and (y%(ops.markRes) == 0): mark += 1 markers[x,y] = mark ##run watershed water = ndimage.watershed_ift(im.astype('uint8'), markers, structure = struct) #water[xm, ym] = water[xm-1, ym-1] # remove the isolate seeds #make diff mask for 'gutter' bgwc = water[1,1] ##assumes that (1,1) is part of the gutter blackbg = np.zeros_like(im).astype('uint8') for x in range(xsize): for y in range(ysize): if not (water[x,y] == bgwc): blackbg[x,y] = im[x,y] #subtract balck bg mask im = im - blackbg ##run watershed water = ndimage.watershed_ift(im.astype('uint8'), markers, structure = struct) ##make some masks and count the size of each region sizecount = [] masks = [] marks = range(mark+1) for index in range(len(marks)): sizecount.append(0) masks.append([]) for x in range(0,xsize,ops.res): print int(float(x)/xsize*100), '%' for y in range(0,ysize,ops.res): sizecount[marks.index(water[x,y])] += 1 masks[marks.index(water[x,y])].append((x,y)) ##make markers based on large regions mark = 0 markers = np.zeros_like(im).astype('int') for mark in marks: if sizecount[marks.index(mark)] >= 200*200/ops.res/ops.res*ops.scaling: markers[masks[marks.index(mark)][int(200*200/ops.res/ops.res*ops.scaling)]] = mark print 'panel found' ##run watershed water = ndimage.watershed_ift(im.astype('uint8'), markers, structure = struct) ##retranspose and postscale water = scipy.transpose(water) water = water/ops.scaling ##save and view if ops.view: toimage(water).resize((xsize/2,ysize/2)).show() if ops.save: print (os.path.join(ops.outDir,os.path.basename(picture)+'_mask.jpg'), "JPEG") toimage(water).convert('RGB').save(os.path.join(ops.outDir,os.path.basename(picture)+'_mask.jpg'), "JPEG") return os.path.join(ops.outDir,os.path.basename(picture)+'_mask.jpg'), "JPEG"
#Make some different structuring arrays. 3x3x3, 5x5x5 and 7x7x7 if not full_connectivity: structure1=ndimage.morphology.generate_binary_structure(3, 1) else: structure1=np.ones((3,3,3), dtype="bool8") labels1,labelcount1=ndimage.label(mask,structure1) found_objects=ndimage.find_objects(labels1) #Requires uint8 or uint16 data labelmap=ndimage.watershed_ift(npdata, labels1, structure1) #OUTPUT DATA TO FILE reload(main) outputfiledir="/mnt/scratch/tkk3/joinedImages/uint16/" outputfilename="rec_J29-33_33D_PTAwt_16p2_30mm_w4Dd_int32_labelmap_xy293_z1047_10.0um.raw" f=open((outputfiledir+outputfilename),mode='wb') f.write(labelmap.tostring()) f.close()
def on_segment(self, evt): o = np.zeros_like(self._markers) watershed_ift(self.img[:,:, 0], self._markers, output=o) o = o * 255 imshow(o.astype('uint8'))
def get_clusters(self): dataset = self.radarData.curr() data = dataset['vals'][0] flat_data = data[data >= -40] clustLabels = np.empty(data.shape, dtype=int) clustLabels[:] = -1 if np.nanmin(flat_data) == np.nanmax(flat_data): # can't cluster data with no change return clustLabels, 0 bad_data = (np.isnan(data) | (data <= 0.0)) bins = np.linspace(np.nanmin(flat_data), np.nanmax(flat_data), 2**8) data_digitized = np.digitize(data.flat, bins) data_digitized.shape = data.shape data_digitized = data_digitized.astype('uint8') markers = np.zeros(data.shape, dtype=int) for index, feat in enumerate(self.state._features[self.frameIndex]): if 'contour' in feat.objects: contr = feat.objects['contour'] res = points_inside_poly(zip(self.xs.flat, self.ys.flat), contr.get_xy()) res.shape = self.xs.shape markers[res] = index + 1 # No contour available? Then fall back to just a point elif 'center' in feat.objects: cent = feat.objects['center'] gridx, gridy = self._xy2grid(cent.center[0], cent.center[1]) markers[gridy, gridx] = index + 1 # TODO: work from an ellipse, if it exists? else: raise ValueError("Empty feature?") markers[bad_data] = -1 ndimg.watershed_ift(data_digitized, markers, output=clustLabels) clustCnt = len(self.state._features[self.frameIndex]) cents = ndimg.center_of_mass(data**2, clustLabels, range(1, clustCnt + 1)) ellipses = FitEllipses(clustLabels, range(1, clustCnt + 1), self.xs, self.ys) for center, ellip, feat in zip(cents, ellipses, self.state._features[self.frameIndex]): # Remove any other objects that may exist before adding # new objects to the feature. feat.cleanup(['contour']) if ellip is None: continue cent_indx = tuple(np.floor(center).astype(int).tolist()) # TODO: clean this up! newPoint = self.state._new_point(self.xs[cent_indx], self.ys[cent_indx]) self.ax.add_artist(ellip) self.ax.add_artist(newPoint) feat.objects['center'] = newPoint feat.objects['ellip'] = ellip if feat.track is not None: feat.track.update_frame(self.frameIndex) #print "clust count:", clustCnt return clustLabels, clustCnt
def get_clusters(self) : dataset = self.radarData.curr() data = dataset['vals'][0] flat_data = data[data >= -40] clustLabels = np.empty(data.shape, dtype=int) clustLabels[:] = -1 if np.nanmin(flat_data) == np.nanmax(flat_data) : # can't cluster data with no change return clustLabels, 0 bad_data = (np.isnan(data) | (data <= 0.0)) bins = np.linspace(np.nanmin(flat_data), np.nanmax(flat_data), 2**8) data_digitized = np.digitize(data.flat, bins) data_digitized.shape = data.shape data_digitized = data_digitized.astype('uint8') markers = np.zeros(data.shape, dtype=int) for index, feat in enumerate(self.state._features[self.frameIndex]) : if 'contour' in feat.objects : contr = feat.objects['contour'] res = points_inside_poly(zip(self.xs.flat, self.ys.flat), contr.get_xy()) res.shape = self.xs.shape markers[res] = index + 1 # No contour available? Then fall back to just a point elif 'center' in feat.objects : cent = feat.objects['center'] gridx, gridy = self._xy2grid(cent.center[0], cent.center[1]) markers[gridy, gridx] = index + 1 # TODO: work from an ellipse, if it exists? else : raise ValueError("Empty feature?") markers[bad_data] = -1 ndimg.watershed_ift(data_digitized, markers, output=clustLabels) clustCnt = len(self.state._features[self.frameIndex]) cents = ndimg.center_of_mass(data**2, clustLabels, range(1, clustCnt + 1)) ellipses = FitEllipses(clustLabels, range(1, clustCnt + 1), self.xs, self.ys) for center, ellip, feat in zip(cents, ellipses, self.state._features[self.frameIndex]) : # Remove any other objects that may exist before adding # new objects to the feature. feat.cleanup(['contour']) if ellip is None : continue cent_indx = tuple(np.floor(center).astype(int).tolist()) # TODO: clean this up! newPoint = self.state._new_point(self.xs[cent_indx], self.ys[cent_indx]) self.ax.add_artist(ellip) self.ax.add_artist(newPoint) feat.objects['center'] = newPoint feat.objects['ellip'] = ellip if feat.track is not None : feat.track.update_frame(self.frameIndex) #print "clust count:", clustCnt return clustLabels, clustCnt
if peak: iniposn.append(c) inipeak.append(im[c]) nmulsrc = len(iniposn) if nmulsrc > 1: markers = N.zeros(im.shape, int) markers[0, 0] = 1 for ipk in range(nmulsrc): pk = inipeak[ipk] x, y = iniposn[ipk] markers[int(round(x)), int(round(y))] = ipk + 2 im2 = N.zeros(im.shape, int) im1 = im1 - im1.min() im1 = im1 / im1.max() * 255 im1 = 255 - im1 nd.watershed_ift(N.array(im1, N.uint8), markers, output=im2) fcn = MGFunction(im, isl.mask_active, 1) fit = lmder_fit gg1 = gg() for ipk in range(nmulsrc): ind = ipk + 2 mom = func.momanalmask_gaus(im, im2, ind, 1.0, True) indd = N.where(im2 == ind) mom[3] = 3.0 mom[4] = 3.0 g = [ float(N.max(im[indd])), int(round(mom[1])), int(round(mom[2])), mom[3] / fwsig, mom[4] / fwsig, mom[5] ] gg1.add_gaussian(fcn, g, dof=isl.size_active)
def generateInitialMask(ndsm, classified, slope, numret): print "Preparing Watershed base..." ndsm_gray = normalizeRange(ndsm, 0, 255) # Prepare Watershed Base # Remove Vegetation ndsm_noveg = copy.deepcopy(ndsm_gray) ndsm_noveg[classified == 5] = 0 # Perform Morphology ndsm_noveg_open = morphology.opening(ndsm_noveg, morphology.square(3)) slope_thresh = slope > 60 edges = copy.deepcopy(ndsm_gray) edges[~slope_thresh] = 0 wshed_base = copy.deepcopy(ndsm_noveg_open) for y in xrange(len(ndsm_noveg_open)): for x in xrange(len(ndsm_noveg_open[0])): if slope_thresh[y][x] == True: # print "HERE" wshed_base[y][x] = ndsm_noveg_open[y][x] # Prepare Markers print "Preparing markers..." ndsm_thresh = ndsm > 2 slope_thresh = slope > 30 slope_morph = morphology.closing(slope_thresh, morphology.square(4)) markers_level1 = ndsm_thresh & ~slope_morph vegetation = classified == 5 markers_level2 = markers_level1 & ~vegetation markers_thresh = markers_level2 != 0 markers_small_removed = morphology.remove_small_objects(markers_thresh, 10) markers_level3, num_labels = ndimage.label(markers_small_removed) markers_level3[ndsm < 1] = -1 markers_level3[classified == 5] = -1 # Perform Watershed segmentation print "Performing region growing..." wshed = ndimage.watershed_ift(input=wshed_base, markers=markers_level3) wshed[wshed == -1] = 0 # Remove river artifacts print "Removing river artifacts..." returns = numret > 0 labels = list(np.unique(wshed)) labels = labels[1:] #1454, 741 # labels = [1454] area_thresh = 30 for label in labels: print label clone = copy.deepcopy(wshed) obj_slice = ndimage.find_objects(clone == label) # print obj_slice obj = clone[obj_slice[0][0], obj_slice[0][1]] obj[obj != label] = 0 obj = obj.astype(bool) area_orig = np.bincount(obj.flatten())[1] if area_orig < area_thresh: wshed[wshed == label] = 0 returns_slice = returns[obj_slice[0][0], obj_slice[0][1]] intersect = returns_slice & obj intersect = intersect * 1 area_intersect = np.count_nonzero(intersect) ratio = float(area_intersect) / float(area_orig) if ratio < 0.2: wshed[wshed == label] = 0 return wshed
def runWatershed(markers,arr): arr=np.logical_not(arr) markers=np.array(markers, dtype=(np.int16)) arr=np.array(arr, dtype=np.uint8) res = watershed_ift(arr,markers) return res
def run_final_classifier(directory_path): f = open("img_classes_pickled.dat", "rb") p = cPickle.Unpickler(f) img_classes = p.load() f.close() test_data = [] for dirname, dirnames, filenames in os.walk(directory_path): for filename in filenames: if not filename[-4:] == ".jpg": continue fullfilename = os.path.join(dirname, filename) img = mpimg.imread(fullfilename, "rb") ## The following bug is not fixed yet: imread flips the image upside-down img = np.flipud(img) if len(img.shape) == 2: img = np.dstack((img, img, img)) features = [] ######## Compute 15 features ######## ###### No. 1: image size, or number of pixels ------------------------------------ num_pixels = img[..., 0].size features.append(num_pixels) ###### No. 2-4: avg of the RGB channel intensity --------------------------------- avg_R = np.mean(img[..., 0]) features.append(avg_R) avg_G = np.mean(img[..., 1]) features.append(avg_G) avg_B = np.mean(img[..., 2]) features.append(avg_B) ###### No. 5-8: GLCM for corner patches ------------------------------------------ # RGB --> gray try: r, g, b = np.rollaxis(img, axis=-1) except: print fullfilename gray = (0.299 * r + 0.587 * g + 0.114 * b).astype("int") PATCH_SIZE = 20 # select some corner patches TL = (0, 0) TR = (0, gray.shape[1] - 1 - PATCH_SIZE) BL = (gray.shape[0] - 1 - PATCH_SIZE, 0) BR = (gray.shape[0] - 1 - PATCH_SIZE, gray.shape[1] - 1 - PATCH_SIZE) locs = [TL, TR, BL, BR] corner_patches = [] for loc in locs: corner_patches.append(gray[loc[0] : loc[0] + PATCH_SIZE, loc[1] : loc[1] + PATCH_SIZE]) # compute some GLCM properties each patch xs = [] ys = [] for i, patch in enumerate(corner_patches): glcm = greycomatrix(patch, [5], [0], 256, symmetric=True, normed=True) xs.append(greycoprops(glcm, "dissimilarity")[0, 0]) ys.append(greycoprops(glcm, "correlation")[0, 0]) features.append(np.mean(xs)) features.append(np.var(xs)) features.append(np.mean(ys)) features.append(np.var(ys)) ###### No. 9-12: GLCM for central patches ---------------------------------------- # RGB --> gray # r,g,b = np.rollaxis(img,axis=-1) gray = (0.299 * r + 0.587 * g + 0.114 * b).astype("int") PATCH_SIZE = 20 # select some center patches center = (gray.shape[0] / 2, gray.shape[1] / 2) TL = (center[0] - PATCH_SIZE, center[1] - PATCH_SIZE) TR = (center[0] - PATCH_SIZE, center[1] + PATCH_SIZE) BL = (center[0] + PATCH_SIZE, center[1] - PATCH_SIZE) BR = (center[0] + PATCH_SIZE, center[1] + PATCH_SIZE) locs = [TL, TR, BL, BR] corner_patches = [] for loc in locs: corner_patches.append(gray[loc[0] : loc[0] + PATCH_SIZE, loc[1] : loc[1] + PATCH_SIZE]) # compute some GLCM properties each patch xs = [] ys = [] for i, patch in enumerate(corner_patches): glcm = greycomatrix(patch, [5], [0], 256, symmetric=True, normed=True) xs.append(greycoprops(glcm, "dissimilarity")[0, 0]) ys.append(greycoprops(glcm, "correlation")[0, 0]) features.append(np.mean(xs)) features.append(np.var(xs)) features.append(np.mean(ys)) features.append(np.var(ys)) ###### No. 13-16: Edges (Sobel & Canny) ------------------------------------------ # Proportion of image that are edges # sobel works only with float32, not uint8 # gray = 0.299 * r + 0.587 * g + 0.114 * b edges_sobel = sobel(gray) SOBEL_THRESHOLD_1 = np.max(edges_sobel.flat) / 10 edges_sobel_size = edges_sobel[edges_sobel > SOBEL_THRESHOLD_1].size edges_sobel_ratio_1 = edges_sobel_size * 1.0 / gray.size SOBEL_THRESHOLD_2 = np.max(edges_sobel.flat) / 5 edges_sobel_size = edges_sobel[edges_sobel > SOBEL_THRESHOLD_2].size edges_sobel_ratio_2 = edges_sobel_size * 1.0 / gray.size SOBEL_THRESHOLD_3 = np.max(edges_sobel.flat) / 3 edges_sobel_size = edges_sobel[edges_sobel > SOBEL_THRESHOLD_3].size edges_sobel_ratio_3 = edges_sobel_size * 1.0 / gray.size edges_canny = canny(gray).astype("int") edges_canny_ratio = np.sum(edges_canny.astype(int)) * 1.0 / gray.size features.append(edges_sobel_ratio_1) features.append(edges_sobel_ratio_2) features.append(edges_sobel_ratio_3) features.append(edges_canny_ratio) ###### No. 17-19: Segmentation (label count) ------------------------------------- gray = (0.299 * r + 0.587 * g + 0.114 * b).astype("int") elevation_map = sobel(gray) elevation_map = elevation_map / np.max(elevation_map) elevation_map = (elevation_map * 255).astype("uint8") markers = np.zeros_like(gray) markers[gray < 30] = 1 markers[gray > 150] = 2 segmentation = watershed_ift(elevation_map, markers) segmentation = ndimage.binary_fill_holes(segmentation - 1) labeled_elements, count = ndimage.label(segmentation) features.append(count) markers = np.zeros_like(gray) markers[gray < 80] = 1 markers[gray > 180] = 2 segmentation = watershed_ift(elevation_map, markers) segmentation = ndimage.binary_fill_holes(segmentation - 1) labeled_elements, count = ndimage.label(segmentation) features.append(count) markers = np.zeros_like(gray) markers[gray < 100] = 1 markers[gray > 200] = 2 segmentation = watershed_ift(elevation_map, markers) segmentation = ndimage.binary_fill_holes(segmentation - 1) labeled_elements, count = ndimage.label(segmentation) features.append(count) ###### Store features into data -------------------------------------------------- test_data.append(features) test_data = np.array(test_data, dtype="float16") # Load pickled classifier f = open("clf_pickled.dat", "rb") p = cPickle.Unpickler(f) rf_clf = p.load() f.close() # make predictions for testing set predictions = rf_clf.predict(test_data) print "filename predicted_class" print "------------------------------------" # assuming order of the predictions is the same as the order of the filenames... for dirname, dirnames, filenames in os.walk(directory_path): counter = 0 for filename in filenames: if not filename[-4:] == ".jpg": continue ind = int(predictions[counter]) counter += 1 for key, val in img_classes.iteritems(): if val == ind: print filename + " " + key