def correct_drift(self, ref, threshold=0.005): """Align images to correct for image drift. Detects common features on the images and tracks them moving. Parameters ---------- ref: KerrArray or ndarray reference image with zero drift threshold: float threshold for detecting imperfections in images (see skimage.feature.corner_fast for details) Returns ------- shift: array shift vector relative to ref (x drift, y drift) transim: KerrArray copy of self translated to account for drift""" refed=ref.clone refed=filters.gaussian(ref,sigma=1) refed=feature.corner_fast(refed,threshold=0.005) imed=self.clone imed=filters.gaussian(imed,sigma=1) imco=feature.corner_fast(imed,threshold=0.005) shift,err,phase=feature.register_translation(refed,imco,upsample_factor=50) #tform = SimilarityTransform(translation=(-shift[1],-shift[0])) #imed = transform.warp(im, tform) #back to original image self=self.translate(translation=(-shift[1],-shift[0])) return [shift,self]
def test_apply_parallel(): import dask.array as da # data a = np.arange(144).reshape(12, 12).astype(float) # apply the filter expected1 = threshold_local(a, 3) result1 = apply_parallel(threshold_local, a, chunks=(6, 6), depth=5, extra_arguments=(3,), extra_keywords={'mode': 'reflect'}) assert_array_almost_equal(result1, expected1) def wrapped_gauss(arr): return gaussian(arr, 1, mode='reflect') expected2 = gaussian(a, 1, mode='reflect') result2 = apply_parallel(wrapped_gauss, a, chunks=(6, 6), depth=5) assert_array_almost_equal(result2, expected2) expected3 = gaussian(a, 1, mode='reflect') result3 = apply_parallel( wrapped_gauss, da.from_array(a, chunks=(6, 6)), depth=5, compute=True ) assert isinstance(result3, np.ndarray) assert_array_almost_equal(result3, expected3)
def filter(data,filtType,par): if filtType == "sobel": filt_data = sobel(data) elif filtType == "roberts": filt_data = roberts(data) elif filtType == "canny": filt_data = canny(data) elif filtType == "lowpass_avg": from scipy import ndimage p=int(par) kernel = np.ones((p,p),np.float32)/(p*p) filt_data = ndimage.convolve(data, kernel) elif filtType == "highpass_avg": from scipy import ndimage p=int(par) kernel = np.ones((p,p),np.float32)/(p*p) lp_data = ndimage.convolve(data, kernel) filt_data = data - lp_data elif filtType == "lowpass_gaussian": filt_data = gaussian(data, sigma=float(par)) elif filtType == "highpass_gaussian": lp_data = gaussian(data, sigma=float(par)) filt_data = data - lp_data #elif filtType == "gradient": return filt_data
def punch(img): # Identifiying the Tissue punches in order to Crop the image correctly # Canny edges and RANSAC is used to fit a circe to the punch # A Mask is created distance = 0 r = 0 float_im, orig, ihc = create_bin(img) gray = rgb2grey(orig) smooth = gaussian(gray, sigma=3) shape = np.shape(gray) l = shape[0] w = shape[1] x = l - 20 y = w - 20 rows = np.array([[x, x, x], [x + 1, x + 1, x + 1]]) columns = np.array([[y, y, y], [y + 1, y + 1, y + 1]]) corner = gray[rows, columns] thresh = np.mean(corner) print thresh binar = (smooth < thresh - 0.01) bin = remove_small_holes(binar, min_size=100000, connectivity=2) bin1 = remove_small_objects(bin, min_size=5000, connectivity=2) bin2 = gaussian(bin1, sigma=3) bin3 = (bin2 > 0) # eosin = IHC[:, :, 2] edges = canny(bin3) coords = np.column_stack(np.nonzero(edges)) model, inliers = ransac(coords, CircleModel, min_samples=4, residual_threshold=1, max_trials=1000) # rr, cc = circle_perimeter(int(model.params[0]), # int(model.params[1]), # int(model.params[2]), # shape=im.shape) a, b = model.params[0], model.params[1] r = model.params[2] ny, nx = bin3.shape ix, iy = np.meshgrid(np.arange(nx), np.arange(ny)) distance = np.sqrt((ix - b)**2 + (iy - a)**2) mask = np.ma.masked_where(distance > r, bin3) return distance, r, float_im, orig, ihc, bin3
def _preprocess(self, frame, stretch_intensity=True, blur=1, denoise=0): """ 1. convert frame to grayscale 2. remove noise from frame. increase denoise value for more noise filtering 3. stretch contrast """ if len(frame.shape) != 2: frm = grayspace(frame) else: frm = frame / self.pixel_depth * 255 frm = frm.astype('uint8') # self.preprocessed_frame = frame # if denoise: # frm = self._denoise(frm, weight=denoise) # print 'gray', frm.shape if blur: frm = gaussian(frm, blur) * 255 frm = frm.astype('uint8') # frm1 = gaussian(self.preprocessed_frame, blur, # multichannel=True) * 255 # self.preprocessed_frame = frm1.astype('uint8') if stretch_intensity: frm = rescale_intensity(frm) # frm = self._contrast_equalization(frm) # self.preprocessed_frame = self._contrast_equalization(self.preprocessed_frame) return frm
def limpa_imagem(img_cinza): #binariza a imagem em escala de cinza img_bin_cinza = np.where(img_cinza < np.mean(img_cinza), 0, 255) # aplica lbp sobre a imagem em escala de cinza # lbp foi aplicado para evitar perda de informacao em regioes # proximas as regioes escuras (provaveis celulas) lbp_img = local_binary_pattern(img_cinza, 24, 3, method='uniform') # aplica efeito de blurring sobre a imagem resultante do lbp blur_img = gaussian(lbp_img,sigma=6) img_bin_blur = np.where(blur_img < np.mean(blur_img), 0, 255) # junta as duas regiões definidas pela binarizacao da imagem em escala # de cinza e a binarizacao do blurring mascara = np.copy(img_bin_cinza) for (a,b), valor in np.ndenumerate(img_bin_blur): if valor == 0: mascara[a][b] = 0 # aplica a mascara obtida sobre a imagem original (em escala de cinza) # para delimitar melhor as regiões que não fornecerao informacoes (regioes # totalmente brancas) img_limpa = np.copy(img_cinza) for (a,b), valor in np.ndenumerate(mascara): if valor == 255: img_limpa[a][b] = 255 return (img_limpa)
def create_background(m, shape, fstd=2, bstd=10): canvas = np.ones(shape) * m noise = np.random.randn(shape[0], shape[1]) * bstd noise = fi.gaussian(noise, fstd) #low-pass filter noise canvas += noise canvas = np.round(canvas).astype(np.uint8) return canvas
def get_h1(imgs): ff = fftn(imgs) h1 = np.absolute(ifftn(ff[1, :, :])) scale = np.max(h1) # h1 = scale * gaussian_filter(h1 / scale, 5) h1 = scale * gaussian(h1 / scale, 5) return h1
def from_points(cls, points, shape=(100, 100), scale=1.0, blur=1.): """Creates a pattern from a set of points. Currently only Gaussian peaks are implemented. Parameters ---------- points : Points, array_like Positions and intensities of the points in the array. shape : Shape of the final array. scale : float Maximum extent of the points. Should be less than 1. blur : float Level of gaussian blur to apply to the pattern. Returns ------- Pattern An array simulating a diffraction pattern. """ if not isinstance(points, Points): points = Points(points) positions = points.to_shape(shape, scale) dat = np.zeros(shape) x, y = np.mgrid[0: shape[0], 0: shape[1]] pos = np.empty(x.shape + (2,)) pos[:, :, 0] = x pos[:, :, 1] = y for position, intensity in zip(positions, points.intensities): dat += intensity * multivariate_normal.pdf(pos, mean=position, cov=1) dat = filters.gaussian(dat, sigma=blur) return dat.view(cls)
def run(args): probs_map = np.load(args.probs_map_path) X, Y = probs_map.shape resolution = pow(2, args.level) if args.sigma > 0: probs_map = filters.gaussian(probs_map, sigma=args.sigma) outfile = open(args.coord_path, 'w') while np.max(probs_map) > args.prob_thred: prob_max = probs_map.max() max_idx = np.where(probs_map == prob_max) x_mask, y_mask = max_idx[0][0], max_idx[1][0] x_wsi = int((x_mask + 0.5) * resolution) y_wsi = int((y_mask + 0.5) * resolution) outfile.write('{:0.5f},{},{}'.format(prob_max, x_wsi, y_wsi) + '\n') x_min = x_mask - args.radius if x_mask - args.radius > 0 else 0 x_max = x_mask + args.radius if x_mask + args.radius <= X else X y_min = y_mask - args.radius if y_mask - args.radius > 0 else 0 y_max = y_mask + args.radius if y_mask + args.radius <= Y else Y for x in range(x_min, x_max): for y in range(y_min, y_max): probs_map[x, y] = 0 outfile.close()
def k_means_classifier(image): n_clusters = 8 # blur and take local maxima blur_image = gaussian(image, sigma=8) blur_image = ndi.maximum_filter(blur_image, size=3) # get texture features feats = local_binary_pattern(blur_image, P=40, R=5, method="uniform") feats_r = feats.reshape(-1, 1) # cluster the texture features km = k_means(n_clusters=n_clusters, batch_size=500) clus = km.fit(feats_r) # copy relevant attributes labels = clus.labels_ clusters = clus.cluster_centers_ # reshape label arrays labels = labels.reshape(blur_image.shape[0], blur_image.shape[1]) # segment shadow img = blur_image.ravel() shadow_seg = img.copy() for i in range(0, n_clusters): # set up array of pixel indices matching cluster mask = np.nonzero((labels.ravel() == i) == True)[0] if len(mask) > 0: thresh = threshold_otsu(img[mask]) shadow_seg[mask] = shadow_seg[mask] < thresh shadow_seg = shadow_seg.reshape(*image.shape) return shadow_seg
def BlurImage(self, BlurSize): # Convolution of image with Gaussian kernel BlurSize self.BlurSize = BlurSize self.BlurredImage = filt.gaussian(self.Image, BlurSize) self.ManipulatedImage = self.BlurredImage if "-Blurred" not in self.TitleTag: self.TitleTag = self.TitleTag + "-Blurred" self.Show()
def test_RGB(): img = gaussian(data.text(), 1) imgR = np.zeros((img.shape[0], img.shape[1], 3)) imgG = np.zeros((img.shape[0], img.shape[1], 3)) imgRGB = np.zeros((img.shape[0], img.shape[1], 3)) imgR[:, :, 0] = img imgG[:, :, 1] = img imgRGB[:, :, :] = img[:, :, None] x = np.linspace(5, 424, 100) y = np.linspace(136, 50, 100) init = np.array([x, y]).T snake = active_contour(imgR, init, bc='fixed', alpha=0.1, beta=1.0, w_line=-5, w_edge=0, gamma=0.1) refx = [5, 9, 13, 17, 21, 25, 30, 34, 38, 42] refy = [136, 135, 134, 133, 132, 131, 129, 128, 127, 125] assert_equal(np.array(snake[:10, 0], dtype=np.int32), refx) assert_equal(np.array(snake[:10, 1], dtype=np.int32), refy) snake = active_contour(imgG, init, bc='fixed', alpha=0.1, beta=1.0, w_line=-5, w_edge=0, gamma=0.1) assert_equal(np.array(snake[:10, 0], dtype=np.int32), refx) assert_equal(np.array(snake[:10, 1], dtype=np.int32), refy) snake = active_contour(imgRGB, init, bc='fixed', alpha=0.1, beta=1.0, w_line=-5/3., w_edge=0, gamma=0.1) assert_equal(np.array(snake[:10, 0], dtype=np.int32), refx) assert_equal(np.array(snake[:10, 1], dtype=np.int32), refy)
def hsv_modulation(lesion_image): img_path = os.path.join('../', lesion_image.path) if not os.path.exists(img_path): print('no image found: ', lesion_image.name) return [] image = Image.open(img_path) mode = image.mode format = image.format height = image.height width = image.width image = array(image) if mode == 'RGBA': image = image[:,:,0:3] if lesion_image.source == 'DermQuest': image = image[0:-100, :] center = (int(height / 2), int(width / 2)) image_hsv = rgb2hsv(image) sigma = image.size/800000 oimage = np.copy(image) image = gaussian(image, sigma=sigma, multichannel=True) h = image_hsv[:,:,0] s = image_hsv[:,:,1] v = image_hsv[:,:,2] h = gaussian(h, sigma=sigma) p2, p98 = np.percentile(h, (2, 98)) h = exposure.rescale_intensity(h, in_range=(p2, p98)) s_inv_v = s * ((v * -1) + 1) s_inv_v_h = s * ((v * -1) + 1) * ((h * -1) + 1) slic_s = prep(oimage, s * 256) path_string = 'media/{0}.slic_s.jpeg'.format(lesion_image.name) media_path = path(path_string) imsave(media_path.abspath(), slic_s) return [{'name': 'foo'}, {'name': 'bar'}]
def infer(edge_image, edge_lengths, mu, phi, sigma2, update_slice=slice(None), scale_estimate=None, rotation=0, translation=(0, 0)): # edge_points = np.array(np.where(edge_image)).T # edge_points[:, [0, 1]] = edge_points[:, [1, 0]] # edge_score = edge_image.shape[0] * np.exp(-edge_lengths[edge_image] / (0.25 * edge_image.shape[0])).reshape(-1, 1) # edge_points = np.concatenate((edge_points, edge_score), axis=1) # # edge_nn = NearestNeighbors(n_neighbors=1).fit(edge_points) edge_near = scipy.ndimage.distance_transform_edt(~edge_image) edge_near_blur = gaussian(edge_near, 2) Gy, Gx = np.gradient(edge_near_blur) mag = np.sqrt(np.power(Gy, 2) + np.power(Gx, 2)) if scale_estimate is None: scale_estimate = min(edge_image.shape) * 4 mu = (mu.reshape(-1, 2) - mu.reshape(-1, 2).mean(axis=0)).reshape(-1, 1) average_distance = np.sqrt(np.power(mu.reshape(-1, 2), 2).sum(axis=1)).mean() scale_estimate /= average_distance * np.sqrt(2) h = np.zeros((phi.shape[1], 1)) psi = SimilarityTransform(scale=scale_estimate, rotation=rotation, translation=translation) while True: w = (mu + phi @ h).reshape(-1, 2) image_points = matrix_transform(w, psi.params)[update_slice, :] image_points = np.concatenate((image_points, np.zeros((image_points.shape[0], 1))), axis=1) # closest_edge_point_indices = edge_nn.kneighbors(image_points)[1].flatten() # closest_edge_points = edge_points[closest_edge_point_indices, :2] closest_edge_points = gradient_step(Gy, Gx, mag, image_points) w = mu.reshape(-1, 2) psi = estimate_transform('similarity', w[update_slice, :], closest_edge_points) image_points = matrix_transform(w, psi.params)[update_slice, :] image_points = np.concatenate((image_points, np.zeros((image_points.shape[0], 1))), axis=1) # closest_edge_point_indices = edge_nn.kneighbors(image_points)[1].flatten() # closest_edge_points = edge_points[closest_edge_point_indices, :2] closest_edge_points = gradient_step(Gy, Gx, mag, image_points) mu_slice = mu.reshape(-1, 2)[update_slice, :].reshape(-1, 1) K = phi.shape[-1] phi_full = phi.reshape(-1, 2, K) phi_slice = phi_full[update_slice, :].reshape(-1, K) h = update_h(sigma2, phi_slice, closest_edge_points, mu_slice, psi) w = (mu + phi @ h).reshape(-1, 2) image_points = matrix_transform(w, psi.params) update_slice = yield image_points, closest_edge_points
def preprocessing_filters(image, blur_params=None, temperature_params=None, low_contrast_params=None, center=True): """ Meta function for preprocessing images. Parameters ---------- image : ndarray input rgb image blur_band : int band of rgb to check for blur blur_params : dict or `None` parameters for `pyroots.detect_blur` temperature_params : dict or `None` parameters for `pyroots.calc_temperature_distance` low_contrast_params : dict or `None` parameters for `skimage.exposure.is_low_contrast` center : bool Take middle 25% of an image for blur detection? Returns ------- bool - should the image be pre-processed? Must pass all criteria given. """ try: if center is True: blur = detect_motion_blur(_center_image(image), **blur_params) else: blur = detect_motion_blur(image, **blur_params) except: blur = True if blur_params is not None: warn("Skipping motion blur check", UserWarning) pass try: bands = calc_temperature_distance(image, **temperature_params) except: bands = True if missing_band_params is not None: warn("Skipping temperature check", UserWarning) pass try: contrast = ~exposure.is_low_contrast(filters.gaussian(image, sigma=10, multichannel=True), **low_contrast_params) except: contrast = True if low_contrast_params is not None: warn("Skipping low contrast check", UserWarning) pass return(blur * bands * contrast)
def test(): image_series = glob.glob('full_dewar/puck*_*in*_200.jpg') templates = [n.replace('200', '*') for n in image_series] template_empty = imread('template_empty.jpg') h, w = template_empty.shape print 'len(templates)', len(templates) fig, axes = plt.subplots(3, 4) a = axes.ravel() k = 0 used = [] while k<12: #for template in templates[:12]: template = random.choice(templates) if template in used: pass else: used.append(template) original_image = img_as_float(combine(template, length=200)) ax = a[k] gray_image = color.rgb2gray(original_image) img_sharp = unsharp(gray_image) edges = canny(img_sharp, sigma=3.0, low_threshold=0.04, high_threshold=0.05) med_unsharp = median(img_sharp/img_sharp.max(), selem=disk(4)) sharp_med_unsharp = unsharp(med_unsharp) edges_med = canny(sharp_med_unsharp, sigma=7) match = match_template(gaussian(edges_med, 4), template_empty) print 'match.max()' print match.max() peaks = peak_local_max(gaussian(match, 3), threshold_abs=0.3, indices=True) print 'template', template print '# peaks', len(peaks) print peaks ax.imshow(original_image) #, cmap='gray') #ax.imshow(gaussian(edges_med, 3), cmap='gnuplot') for peak in peaks: y, x = peak rect = plt.Rectangle((x, y), w, h, edgecolor='g', linewidth=2, facecolor='none') ax.add_patch(rect) #ax[edges] = (0, 1, 0) #image = img_as_int(original_image) #image[edges==True] = (0, 255, 0) ax.set_title(template.replace('full_dewar/', '').replace('_*.jpg', '') + ' detected %s' % (16-len(peaks),)) k += 1 plt.show()
def get_saliency_image(self, image_fname): image, image_filtersize, targetsize = util.preprocess_image( image_fname, config.filtersize) saliency = self.convolution_function( image_filtersize.reshape((1, 1, image_filtersize.shape[0], image_filtersize.shape[1])))[0] saliency = gaussian(saliency[0, 0], sigma=3.) return saliency, image
def test_apply_parallel_wrap(): def wrapped(arr): return gaussian(arr, 1, mode='wrap') a = np.arange(144).reshape(12, 12).astype(float) expected = gaussian(a, 1, mode='wrap') result = apply_parallel(wrapped, a, chunks=(6, 6), depth=5, mode='wrap') assert_array_almost_equal(result, expected)
def BackgroundSubtraction(self): self.ManipulatedImage = self.BlurredImage for Index in self.BackgroundImages.keys(): self.ManipulatedImage = self.ManipulatedImage - filt.gaussian(self.BackgroundImages[Index], self.BlurSize) # Mask array, don't let ions oversubtract Mask = self.ManipulatedImage < 0. # find values that are negative self.ManipulatedImage[Mask] = 0. self.Show()
def test_end_points(): img = data.astronaut() img = rgb2gray(img) s = np.linspace(0, 2*np.pi, 400) x = 220 + 100*np.cos(s) y = 100 + 100*np.sin(s) init = np.array([x, y]).T snake = active_contour(gaussian(img, 3), init, bc='periodic', alpha=0.015, beta=10, w_line=0, w_edge=1, gamma=0.001, max_iterations=100) assert np.sum(np.abs(snake[0, :]-snake[-1, :])) < 2 snake = active_contour(gaussian(img, 3), init, bc='free', alpha=0.015, beta=10, w_line=0, w_edge=1, gamma=0.001, max_iterations=100) assert np.sum(np.abs(snake[0, :]-snake[-1, :])) > 2 snake = active_contour(gaussian(img, 3), init, bc='fixed', alpha=0.015, beta=10, w_line=0, w_edge=1, gamma=0.001, max_iterations=100) assert_allclose(snake[0, :], [x[0], y[0]], atol=1e-5)
def blur(image, x0, x1, y0, y1, sigma=1, imshowall=False): x0, x1 = min(x0, x1), max(x0, x1) y0, y1 = min(y0, y1), max(y0, y1) im = image.copy() sub_im = im[x0:x1,y0:y1].copy() if imshowall: imshow(sub_im) blur_sub_im = gaussian(sub_im, sigma=sigma) if imshowall: imshow(blur_sub_im) blur_sub_im = np.round(255 * blur_sub_im) im[x0:x1,y0:y1] = blur_sub_im return im
def smoothe_and_sharp(): image=data.astronaut() g3=filters.gaussian(image,3) io.imsave('astronautgaussian3.png',g3) g9=filters.gaussian(image,9) io.imsave('astronautgaussian9.png',g9) g15=filters.gaussian(image,15) io.imsave('astronautgaussian15.png',g15) image=io.imread('astronautgaussian3.png') sharprgb=image.copy() for i in range(3): l=np.abs(filters.laplace(image[:,:,i])) sharprgb[:,:,i]=np.uint8(np.minimum(image[:,:,i]+l/l.max()*55,255)) io.imsave('astronautsharprgb.png',sharprgb) sharphsv=color.rgb2hsv(image) l=np.abs(filters.laplace(sharphsv[:,:,2])) sharphsv[:,:,2]=np.minimum(l/l.max()*0.5+sharphsv[:,:,2],1) io.imsave('astronautsharphsv.png',color.hsv2rgb(sharphsv))
def test_fixed_reference(): img = data.text() x = np.linspace(5, 424, 100) y = np.linspace(136, 50, 100) init = np.array([x, y]).T snake = active_contour(gaussian(img, 1), init, bc='fixed', alpha=0.1, beta=1.0, w_line=-5, w_edge=0, gamma=0.1) refx = [5, 9, 13, 17, 21, 25, 30, 34, 38, 42] refy = [136, 135, 134, 133, 132, 131, 129, 128, 127, 125] assert_equal(np.array(snake[:10, 0], dtype=np.int32), refx) assert_equal(np.array(snake[:10, 1], dtype=np.int32), refy)
def draw_density_estimation(self, axis, title, samples, cmap): axis.clear() axis.set_xlabel(title) density_estimation = numpy.zeros((self.l_kde, self.l_kde)) for x, y in samples: if 0 < x < 1 and 0 < y < 1: density_estimation[int((1-y) / self.resolution)][int(x / self.resolution)] += 1 density_estimation = filters.gaussian(density_estimation, self.bw_kde_) axis.imshow(density_estimation, cmap=cmap) axis.xaxis.set_major_locator(pyplot.NullLocator()) axis.yaxis.set_major_locator(pyplot.NullLocator())
def test_free_reference(): img = data.text() x = np.linspace(5, 424, 100) y = np.linspace(70, 40, 100) init = np.array([x, y]).T snake = active_contour(gaussian(img, 3), init, bc='free', alpha=0.1, beta=1.0, w_line=-5, w_edge=0, gamma=0.1) refx = [10, 13, 16, 19, 23, 26, 29, 32, 36, 39] refy = [76, 76, 75, 74, 73, 72, 71, 70, 69, 69] assert_equal(np.array(snake[:10, 0], dtype=np.int32), refx) assert_equal(np.array(snake[:10, 1], dtype=np.int32), refy)
def get_subset(self, ind, dx, blurred=False, sigma=1.0): assert(len(ind) == 3) try: subset = self.image[ind[0]:ind[0]+dx[0], ind[1]:ind[1]+dx[1], ind[2]:ind[2]+dx[2]] except IndexError: raise "Indices out of bounds." if blurred: subset = gaussian(subset, sigma=sigma) return subset
def __call__(self, xs, tag3d_segmented): xs_bg = np.copy(xs) for i in range(len(xs)): x = xs[i, 0] istag = 1 - tag3d_segmented[i, 0] istag = gaussian(istag, self.segmentation_blur()) bg = random_backgrond(self.pyramid_weights, end_level=6) bg *= self.intensity_scale() bg += self.intensity_shift() xs_bg[i] = x*istag + bg*(1-istag) return np.clip(xs_bg, -1, 1)
def snake(img,origin=[0.0,0.0],r=0.3): s = np.linspace(0, 2*np.pi, 50) x = origin[0] + r*np.cos(s) y = origin[1] + r*np.sin(s) init = np.array([x, y]).T # snake = active_contour(gaussian(img, 1), # init, alpha=10.0, beta=100.0, gamma=0.01) snake = active_contour(gaussian(img, 1),init) return snake
def test_periodic_reference(): img = data.astronaut() img = rgb2gray(img) s = np.linspace(0, 2*np.pi, 400) x = 220 + 100*np.cos(s) y = 100 + 100*np.sin(s) init = np.array([x, y]).T snake = active_contour(gaussian(img, 3), init, alpha=0.015, beta=10, w_line=0, w_edge=1, gamma=0.001) refx = [299, 298, 298, 298, 298, 297, 297, 296, 296, 295] refy = [98, 99, 100, 101, 102, 103, 104, 105, 106, 108] assert_equal(np.array(snake[:10, 0], dtype=np.int32), refx) assert_equal(np.array(snake[:10, 1], dtype=np.int32), refy)
def get_tissue_mask(thumbnail_rgb, deconvolve_first=False, stain_matrix_method="PCA", n_thresholding_steps=1, sigma=0., min_size=500): """Get binary tissue mask from slide thumbnail. Parameters ----------- thumbnail_rgb : np array (m, n, 3) nd array of thumbnail RGB image deconvolve_first : bool use hematoxylin channel to find cellular areas? This will make things ever-so-slightly slower but is better in getting rid of sharpie marker (if it's green, for example). Sometimes things work better without it, though. stain_matrix_method : str see deconv_color method in seed_utils n_thresholding_steps : int number of gaussian smoothign steps sigma : float sigma of gaussian filter min_size : int minimum size (in pixels) of contiguous tissue regions to keep Returns -------- np bool array largest contiguous tissue region. np int32 array each unique value represents a unique tissue region """ if deconvolve_first: # deconvolvve to ge hematoxylin channel (cellular areas) # hematoxylin channel return shows MINIMA so we invert Stains, channel = _deconv_color( thumbnail_rgb, stain_matrix_method=stain_matrix_method) thumbnail = 255 - Stains[..., channel] else: # grayscale thumbnail (inverted) thumbnail = 255 - cv2.cvtColor(thumbnail_rgb, cv2.COLOR_BGR2GRAY) for _ in range(n_thresholding_steps): # gaussian smoothing of grayscale thumbnail if sigma > 0.0: thumbnail = gaussian(thumbnail, sigma=sigma, output=None, mode='nearest', preserve_range=True) # get threshold to keep analysis region try: thresh = threshold_otsu(thumbnail[thumbnail > 0]) except ValueError: # all values are zero thresh = 0 # replace pixels outside analysis region with upper quantile pixels thumbnail[thumbnail < thresh] = 0 # convert to binary mask = 0 + (thumbnail > 0) # find connected components labeled, _ = ndimage.label(mask) # only keep unique, counts = np.unique(labeled[labeled > 0], return_counts=True) discard = np.in1d(labeled, unique[counts < min_size]) discard = discard.reshape(labeled.shape) labeled[discard] = 0 # largest tissue region mask = labeled == unique[np.argmax(counts)] return labeled, mask
def Hough_edge_center(data_dcm_directory, Image_file, Image, Is_a_file, sig, pui, thr, resol_r=0.25, resol_x=0.25, resol_y=0.25): if Is_a_file: #Lecture des données pixel du fichier DICOM Lec = sitk.ReadImage(os.path.join(data_dcm_directory, Image_file)) #Conversion en array Image = sitk.GetArrayFromImage(Lec[:, :, 0]) space_y = Lec.GetSpacing()[0] space_x = Lec.GetSpacing()[1] #Conversion de l'image de non signé 16bits vers le type float Image = Image.astype(float) #Lissage de l'image par un filtre gaussien IG8 = filters.gaussian(Image, sigma=sig) #Augmentation du contraste de l'image filtrée IG8 = IG8 / (0.7 * np.max(IG8)) IG8 = IG8**pui # Obtention de l'image de gradient par filtre de Scharr Scharr = filters.scharr(IG8) # On recupère également l'information sur les gradients horizontaux et verticaux. scharr_v donne les contours verticaux, donc les gradients # horizontaux. Vice-versa pour scharr_h Gx = filters.scharr_v(IG8) Gy = filters.scharr_h(IG8) # Figures de contrôle # plt.figure() # plt.subplot(131) # plt.imshow(Scharr) # plt.subplot(132) # plt.imshow(Schx) # plt.subplot(133) # plt.imshow(Schy) #Définition du seuil pour l'image de gradient B = thr * np.max(Scharr) Thresh = Scharr * (Scharr > B) #Par soucis d'economie en temps de calcul et gestion de mémoire, on va rogner notre espace de travail. En effet, si on travaille avec une forme circulaire, travailler sur l'ensemble de l'image n'a pas d'intérêt : l'information est localisée. #On cherche donc les indices minimums et maximums selon les deux axes de l'image, et on travaille donc sur un espace de Hough réduit, et un remplissage depuis une image tronquée. y_min = np.min(np.nonzero(Thresh)[0]) y_max = np.max(np.nonzero(Thresh)[0]) x_min = np.min(np.nonzero(Thresh)[1]) x_max = np.max(np.nonzero(Thresh)[1]) Dx = x_max - x_min Dy = y_max - y_min Dxt = int(round(0.3 * (Dx))) # Marges en x et y au delà de xmin et xmax Dyt = int(round(0.3 * (Dy))) # #Figure de contrôle : Image de gradient seuillé # plt.figure() # plt.title("Image de seuil de " + str(Image_file)) # plt.imshow(Thresh[y_min-Dyt:y_max+Dyt+1,x_min-Dxt:x_max+Dxt+1]) # #Figure de contrôle : Image de base après pré-traitement # plt.figure() # plt.title("Image filtrée de " + str(Image_file)) # plt.imshow(IG8[y_min-Dyt:y_max+Dyt+1,x_min-Dxt:x_max+Dxt+1]) D = max(Dx, Dy) Dt = max(Dxt, Dyt) #Valeurs de contrôles affichées. # print(y_min,y_max,x_min,x_max,Dx,Dy,Dxt,Dyt,Nrt) #Création de l'espace de Hough Hough_space = np.zeros((int(Dt / resol_r), int( (2 * D) / resol_y) + 1, int((2 * D) / resol_x) + 1), dtype=float) # Création de l'espace de Hough beta_x = (1 / 2) * (1 - resol_x) beta_y = (1 / 2) * (1 - resol_y) for yi in range(Dy + 1): # Les xi et yi (image) ne sont non nuls for xi in range(Dx + 1): # que sur les True de l'image de seuil. T = Thresh[yi + y_min, xi + x_min] if (T != 0): for rh in range(0, int((Dt / resol_r))): #theta est l'angle entre l'orientation du gradient et l'horizontale. Géométriquement, on a tan(theta)=Gy/Gx theta = np.arctan2(Gy[yi + y_min, xi + x_min], Gx[yi + y_min, xi + x_min]) # theta est en plus signé, grâce aux méthodes scharr_v et scharr_h. On a donc seulement un seul point à considérer, # à l'intérieur de l'image de grad, et sur l'axe défini par l'orientation du gradient xh = ((xi + D / 2)) / resol_x + ( (rh * resol_r + (D / 2) - Dt) * np.cos(theta)) / resol_x + beta_x yh = ((yi + D / 2)) / resol_y + ( (rh * resol_r + (D / 2) - Dt) * np.sin(theta)) / resol_y + beta_y #Enfin, puisque cette méthode est sensible au bruit, on n'ajoute pas un seul point mais un kernel 3*3 centré sur ce point. Hough_space[int(rh), int(round(yh)) - 1:int(round(yh)) + 2, int(round(xh)) - 1:int(round(xh)) + 2] += 0.001 # Augmentation du contraste de l'espace de Hough # Hough_space=Hough_space/np.percentile(Hough_space, 97) Hough_space_10 = Hough_space**5 #Valeur max de l'espace de Hough : le cdm n'a de sens qu'a proximité Max_Hough = np.max(Hough_space_10) #On retire donc toutes les valeurs trop éloignées du max Hough_trunc = np.where(Hough_space_10 > 0.01 * Max_Hough, Hough_space_10, 0) #Centre de masse de l'espace de Hough cdm_10 = ndimage.measurements.center_of_mass(Hough_trunc) #Figures de contrôle # plt.figure() # plt.title("Espace de Hough proche du max de "+str(Image_file)) # plt.subplot(121) # plt.imshow(Hough_space_10[int(np.ceil(cdm_10[0])),:,:]) # plt.subplot(122) # plt.imshow(Hough_space_10[int(np.floor(cdm_10[0])),:,:]) r_est_10 = cdm_10[0] * resol_r + D / 2 - 2 * Dt y_est_10 = (cdm_10[1]) * resol_y + y_min - (D / 2) - beta_y x_est_10 = (cdm_10[2]) * resol_x + x_min - (D / 2) - beta_x if not Is_a_file: space_y = 0.336 space_x = 0.336 print("x_est = ", round(x_est_10, 3), ", y_est = ", round(y_est_10, 3), ", r_est = ", round(r_est_10, 3)) # Coordonnées en pixel dans l'image return (x_est_10, y_est_10, space_x, space_y)
results_p_dir = os.path.join(results_dir, "image", f'{i}') os.makedirs(results_p_dir, exist_ok=True) io.imsave(fname=os.path.join(results_p_dir, '01 00 Original.jpg'), arr=image) logging.info('Resizing') resize_factor = 8 image_original_list.append( resize(image, (int(image.shape[0] / resize_factor), (image.shape[1] / resize_factor)), anti_aliasing=True)) logging.info('Gaussian filter') image = apply_on_normalized_luminance( operation=lambda img: gaussian(img, sigma=2), image_rgb=image) io.imsave(fname=os.path.join(results_p_dir, f'01 01 - Gaussian filter.jpg'), arr=image) logging.info('CLAHE') image = apply_on_normalized_luminance( lambda img: equalize_adapthist(img, clip_limit=0.02), image_rgb=image) io.imsave(fname=os.path.join(results_p_dir, f'01 02 - CLAHE.jpg'), arr=image) image_lab = rgb2lab(image) logging.info('Resizing') resize_factor = 8
def gaussian_blur(x, severity=1): c = [1, 1.8, 2.6, 3.4, 4.0][severity - 1] x = gaussian(np.array(x) / 255., sigma=c, multichannel=True) return np.clip(x, 0, 1) * 255
ax[1].set_title('Histogram of grey values') #####################################################################################3 '''Problem 2''' n_cols = 4 cmap = 'gray' edge_sobel = sobel(image) edge_roberts = roberts(image) edge_prewitt = prewitt(image) img_list = [(image, 'Original'), (edge_sobel, 'Sobel filter'), (edge_roberts, 'Roberts filter'), (edge_prewitt, 'Prewitt filter')] #edge_sobel = np.reshape(edge_sobel,image.shape) fig, ax = plt.subplots(ncols=n_cols, figsize=(14, 7)) for i, im in enumerate(img_list): ax[i].imshow(im[0], cmap=cmap) ax[i].axis('off') ax[i].set_title(im[1]) ############################################################################################## '''Problem 3''' fig, ax = plt.subplots(ncols=n_cols, figsize=(14, 7)) sigma_list = [0.3, 0.8, 1.0] ax[0].imshow(image, cmap=cmap) ax[0].axis('off') ax[0].set_title('Original') for i, im in enumerate(sigma_list): gauss = gaussian(image, sigma=im) ax[i + 1].imshow(gauss, cmap=cmap) ax[i + 1].axis('off') ax[i + 1].set_title('Sigma = ' + str(im))
def gaussian_blur(x, severity=1): c = [.4, .6, 0.7, .8, 1][severity - 1] x = gaussian(np.array(x) / 255., sigma=c, multichannel=True) return np.clip(x, 0, 1) * 255
i=1 X_new = [] plt.figure(figsize=(5,10)) for el in glob.glob('./images_signs/*.png') + glob.glob('./images_signs/*.jpg'): print(el) img = io.imread(el) # img = transform.resize(img,(32,32), order=3) # img = gaussian(img,.4,multichannel=True) plt.subplot(1,6,i) plt.imshow(img) i+=1 plt.axis('off') img = transform.resize(img,(32,32), order=3) img = gaussian(img,.4,multichannel=True) X_new.append(img) plt.savefig('plots/new_images.png',bbox_inches='tight') #plt.imshow(X_new[0]) ### print('Processing Image from RGB color to grayscale, resize to 32x32, save into tensor friendly array format... ') def pre_process_image(image): image = np.uint8(image) image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) return image
def gaussian_blur(x, severity=1): c = [.5, .75, 1, 1.25, 1.5][severity - 1] x = gaussian(np.array(x) / 255., sigma=c, multichannel=True) return np.clip(x, 0, 1) * 255
# Blurring to reduce noise # In this exercise you will reduce the sharpness of an image of a building taken during a London trip, through filtering. # Building in Lodon # Image loaded as building_image. # Instructions # 100 XP # Import the Gaussian filter. # Apply the filter to the building_image, set the multichannel parameter to the correct value. # Show the original building_image and resulting gaussian_image. # Import Gaussian filter from skimage.filters import gaussian # Apply filter gaussian_image = gaussian(building_image, multichannel=True) # Show original and resulting image to compare show_image(building_image, "Original") show_image(gaussian_image, "Reduced sharpness Gaussian")
'06_index', '07_ok', '08_palm_moved', '09_c', '10_down', ] # 75% to train for i in range(10): for label in labels: for j in range(1, 151): # 201): label.split() image = io.imread('data/leapGestRecog/0' + str(i) + '/' + label + '/frame_0' + str(i) + '_' + label[0:2] + '_' + str(j).zfill(4) + '.png') img_blurred = gaussian(image, sigma=1.65) dataset.inputs.append( resize(img_blurred, (60, 160), preserve_range=True).flatten()) dataset.targets.append(label) # shuffle data x_train, y_train = shuffle(dataset.inputs, dataset.targets) # Model Selection print('Model Selection...') testScores = {} myMaxScore = 0 bestFunctionPrameters = ""
def test_acc(image_name): car_id = image_name.split('_')[0] angle_indicator = image_name.split('_')[1] nnet_model = joblib.load('nnet_20_10_5_2_adam_logistic_' + angle_indicator + '.pkl') image = misc.imread('test_sample/' + image_name + '.jpg', flatten=True).astype(float) image_rgb = misc.imread('test_sample/' + image_name + '.jpg') image_float = image_rgb.astype(float) image_mask_all_angles = misc.imread('train_all_masks/' + car_id + '.jpg', flatten=True) / 255. image_mask = misc.imread('train_masks/' + image_name + '_mask.gif', flatten=True) / 255 #io.imshow(image_mask) image_index = np.where(image >= 0) sobel = filters.sobel(image) # working #io.imshow(sobel) sobel_blurred = filters.gaussian(sobel, sigma=1) # Working #io.imshow(sobel_blurred) canny_filter_image = canny(image / 255.) #io.imshow(canny_filter_image) # threshold_niblack_11 = filters.threshold_niblack(sobel_blurred,201) #io.imshow(threshold_niblack) threshold_li = filters.threshold_li(image) mask_li = image > threshold_li #io.imshow(mask) sobel_h = filters.sobel_h(image) sobel_v = filters.sobel_v(image) laplace = filters.laplace(image) threshold_local_51 = filters.threshold_local(image, 51) mask_local_51 = image > threshold_local_51 #io.imshow(mask) df = pd.DataFrame() # df['y'] = (image_index[0] - 639.5)/639.5 # df['x'] = (image_index[1] - 958.5)/958.5 df['l1_dist_y'] = abs((image_index[0] - 639.5) / 639.5) df['l1_dist_x'] = abs((image_index[1] - 958.5) / 958.5) df['l2_dist'] = np.sqrt((df['l1_dist_x'])**2 + (df['l1_dist_y'])**2) / np.sqrt(2) df['grey_values'] = image.reshape((1, 1918 * 1280))[0] / 255. df['red_values'] = image_rgb.reshape((3, 1918 * 1280))[0] / 255. df['blue_values'] = image_rgb.reshape((3, 1918 * 1280))[1] / 255. df['green_values'] = image_rgb.reshape((3, 1918 * 1280))[2] / 255. df['red_float'] = image_float.reshape((3, 1918 * 1280))[0] / 255. df['blue_float'] = image_float.reshape((3, 1918 * 1280))[1] / 255. df['green_float'] = image_float.reshape((3, 1918 * 1280))[2] / 255. df['sobel_blurred'] = sobel_blurred.reshape((1, 1918 * 1280))[0] / 255. df['canny_filter_image'] = canny_filter_image.reshape( (1, 1918 * 1280))[0].astype(int) df['sobel_h'] = sobel_h.reshape((1, 1918 * 1280))[0] / 255. df['sobel_v'] = sobel_v.reshape((1, 1918 * 1280))[0] / 255. df['laplace'] = laplace.reshape((1, 1918 * 1280))[0] / 511. df['threshold_local_51'] = mask_local_51.reshape( (1, 1918 * 1280))[0].astype(int) # df['threshold_niblack_11'] = threshold_niblack_11.reshape((1,1918*1280))[0]#/255. df['threshold_li'] = mask_li.reshape((1, 1918 * 1280))[0].astype(int) df['mask'] = image_mask.reshape((1, 1918 * 1280))[0] df['mask'] = df['mask'].astype('category') df['pred_mask'] = nnet_model.predict( X=df[[col for col in df.columns if col != 'mask']]) z = skm.confusion_matrix(df['mask'], df['pred_mask']) dice_coeff = 2 * (z[1][1]) / float(2 * z[1][1] + z[0][1] + z[1][0]) print 'Dice Coefficient:', dice_coeff accuracy = 100 * (z[0][0] + z[1][1]) / float(sum(sum(z))) print 'Accuracy:', accuracy precision = 100 * (z[1][1]) / float(z[0][1] + z[1][1]) print 'Precision:', precision recall = 100 * (z[1][1]) / float(z[1][0] + z[1][1]) print 'Recall:', recall act_mask = np.array(df['mask']) act_mask = act_mask.reshape((1280, 1918)) pred_mask = np.array(df['pred_mask']).astype(float) pred_mask = pred_mask.reshape((1280, 1918)) return df, pred_mask, image, act_mask
def blur(image, target): return torch.from_numpy( filters.gaussian(image, sigma=1., multichannel=True)), target
fig, ax = plt.subplots(1, 10, figsize=(18, 6)) label_list = labels.tolist() for label_id, category in enumerate(categories): ax[label_id].imshow(images[label_list.index(label_id)]) ax[label_id].set_title(category) plt.show() """## Convolutional Filters As a first step we apply some simple filters on the images. In particular, we use convolutional filters, that can be expressed as convolution of a kernel with the image, which will be important for the concept of Convolutional Neural Networks that we will introduce later. For now, we will use some filter available in [skimage.filters](https://scikit-image.org/docs/dev/api/skimage.filters.html). """ image = images[0] filtered_gaussian = filters.gaussian(image, sigma=1., multichannel=True) filtered_laplacian = filters.laplace(image) fig, ax = plt.subplots(1, 3) ax[0].imshow(image) ax[1].imshow(filtered_gaussian) ax[2].imshow(filtered_laplacian) """## Preparation for pytorch In order to use the CIFAR data to with pytorch, we need to transform the data into the compatible data structures. In particular, pytorch expects all numerical data as [torch.tensor](https://pytorch.org/docs/stable/tensors.html). To provide the data as tensors, we will wrap them in a [torch.dataset](https://pytorch.org/docs/stable/data.html) and implement a mechanism to apply transformations to the data on the fly. We will use these transformations to bring the data into a format that pytorch can ingest and later also use them for other purposes such as data augmentation. """ # datasets have to be sub-classes from torch.util.data.Dataset # what transofrmations do we need to feed this data to pytorch?
def smooth_hsv(image, sigma): return filters.gaussian(image, sigma)
from skimage import data, color, filters import numpy as np import matplotlib.pyplot as plt import cv2 gray_img = cv2.imread('Pyramid.jpg', 0) # 先对图像进行两次高斯滤波运算 gimg1 = filters.gaussian(gray_img, sigma=2) gimg2 = filters.gaussian(gray_img, sigma=1.6 * 2) # 两个高斯运算的差分 dimg = gimg2 - gimg1 # 将差归一化 dimg /= 2 # cv2.imshow('', dimg) # cv2.waitKey(0) figure = plt.figure() plt.subplot(141).set_title('original_img1') plt.imshow(gray_img) # cv2.imshow('gray_img',gray_img) plt.subplot(142).set_title('LoG_img1') plt.imshow(gimg1) # cv2.imshow('gimg1',gimg1) plt.subplot(143).set_title('LoG_img2') plt.imshow(gimg2) # cv2.imshow('gimg2',gimg2) plt.subplot(144).set_title('DoG_edge') plt.imshow(dimg, cmap='gray')
def blur_boundary(boundary, sigma): boundary = gaussian(boundary, sigma=sigma) boundary[boundary >= 0.5] = 1 boundary[boundary < 0.5] = 0 return boundary
def gaussian(data): gaussian_filter = filters.gaussian(data.astype(float), sigma=[0.25, 0.5]) new_hist = get_histogram(gaussian_filter) return gaussian_filter, new_hist
young = 1 mask = np.ones((100, 100)) fx_b, fy_b = np.zeros((100, 100)), np.zeros((100, 100)) l1 = np.array([np.arange(35, 45), np.arange(35, 45)]) l2 = np.array([np.arange(55, 65), np.arange(55, 65)]) #l1 = np.array([40,40]) #l2 = np.array([60,60]) fx_b[l1[0], l1[1]] = -1 fy_b[l1[0], l1[1]] = -1 fx_b[l2[0], l2[1]] = 1 fy_b[l2[0], l2[1]] = 1 #fx_b = gaussian(fx_b,sigma=4) #fy_b = gaussian(fy_b,sigma=4) fx_bf = gaussian(fx_b, sigma=1) fy_bf = gaussian(fy_b, sigma=1) #show_quiver(fx_b, fy_b) u_b, v_b = finite_thickness_convolution(fx_b, fy_b, pixelsize, h, young, sigma=0.5, kernel_size=(20, 20)) fx_f_nf, fy_f_nf = traction_wrapper(u_b, v_b, pixelsize, h,
def find_centers_and_crop(imagepath, foldername, imagename, savepath, outfile, scale=4, cropsize=128): # Get the image and resize color_image = np.array(Image.open(imagepath)) print("Working on", imagename) image_shape = color_image.shape[:2] image_shape = tuple(ti // scale for ti in image_shape) color_image = resize(color_image, image_shape) # Split the image into channels microtubules = color_image[:, :, 0] antibody = color_image[:, :, 1] nuclei = color_image[:, :, 2] # Segment the nuclear channel and get the nuclei min_nuc_size = 100.0 val = threshold_otsu(nuclei) smoothed_nuclei = gaussian(nuclei, sigma=5.0) binary_nuclei = smoothed_nuclei > val binary_nuclei = remove_small_holes(binary_nuclei, min_size=300) labeled_nuclei = label(binary_nuclei) labeled_nuclei = clear_border(labeled_nuclei) labeled_nuclei = remove_small_objects(labeled_nuclei, min_size=min_nuc_size) # Iterate through each nuclei and get their centers (if the object is valid), and save to directory for i in range(1, np.max(labeled_nuclei)): current_nuc = labeled_nuclei == i if np.sum(current_nuc) > min_nuc_size: y, x = center_of_mass(current_nuc) x = np.int(x) y = np.int(y) c1 = y - cropsize // 2 c2 = y + cropsize // 2 c3 = x - cropsize // 2 c4 = x + cropsize // 2 if c1 < 0 or c3 < 0 or c2 > image_shape[0] or c4 > image_shape[1]: pass else: nuclei_crop = nuclei[c1:c2, c3:c4] antibody_crop = antibody[c1:c2, c3:c4] microtubule_crop = microtubules[c1:c2, c3:c4] folder_suffix = imagename.rsplit("_", 4)[0] outfolder = savepath + foldername + "_" + folder_suffix outimagename = imagename.rsplit("_", 3)[0] + "_" + str(i) if not os.path.exists(outfolder): os.mkdir(outfolder) Image.fromarray(nuclei_crop).save(outfolder + "//" + outimagename + "_blue.tif") Image.fromarray(antibody_crop).save(outfolder + "//" + outimagename + "_green.tif") Image.fromarray(microtubule_crop).save(outfolder + "//" + outimagename + "_red.tif") output = open(outfile, "a") output.write(foldername + "_" + folder_suffix + "/" + outimagename) output.write("\t") output.write(str(x)) output.write("\t") output.write(str(y)) output.write("\n") output.close()
import random import os import numpy as np from skimage import io from skimage.filters import gaussian from PIL import Image MAIN_DIR = './raw/' LABELS_SUBDIRS = ['amusement','awe','contentment','excitement',\ 'anger','disgust','fear','sadness'] transformations = ['BLURRED', 'FLIPPED'] for label_subdir in LABELS_SUBDIRS: if not (label_subdir == 'fear'): continue dir_path = MAIN_DIR + label_subdir + "/" for image_name in os.listdir(dir_path): if image_name.startswith('BLURRED') or image_name.startswith( 'FLIPPED'): continue print(image_name) original = io.imread(dir_path + image_name) transformation = random.choice(transformations) if transformation == 'BLURRED': new_image = gaussian(original, sigma=5, multichannel=True) else: new_image = np.fliplr(original) img = Image.fromarray(new_image, 'RGB') new_filepath = dir_path + transformation + "_" + image_name img.save(new_filepath)
from skimage import io, filters original_image = skimage.img_as_float(io.imread("skyline.jpg")) plt.imshow(original_image) plt.show() def channel_adjust(channel, values): orig_size = channel.shape flat_channel = channel.flatten() adjusted = np.interp(flat_channel, np.linspace(0, 1, len(values)), values) return adjusted.reshape(orig_size) original_image = skimage.img_as_float(io.imread("skyline.jpg")) r = original_image[:, :, 0] b = original_image[:, :, 2] r_boost_lower = channel_adjust( r, [0, 0.05, 0.1, 0.2, 0.3, 0.5, 0.7, 0.8, 0.9, 0.95, 1.0]) b_more = np.clip(b + 0.03, 0, 1.0) merged = np.stack([r_boost_lower, original_image[:, :, 1], b_more], axis=2) blurred = filters.gaussian(merged, sigma=10, multichannel=True) final = np.clip(merged * 1.3 - blurred * 0.3, 0, 1.0) b = final[:, :, 2] b_adjusted = channel_adjust(b, [ 0, 0.047, 0.118, 0.251, 0.318, 0.392, 0.42, 0.439, 0.475, 0.561, 0.58, 0.627, 0.671, 0.733, 0.847, 0.925, 1 ]) final[:, :, 2] = b_adjusted
if 0: plt.imshow(img, cmap='gray') plt.show() # try active contours init = circle( np.array(img.shape) / 2, img.shape[1] / 2.01, img.shape[0] / 2.01, 360) # alpha: Snake length shape parameter. Higher values makes snake contract faster. # beta: Snake smoothness shape parameter. Higher values makes snake smoother. # gamma: Explicit time stepping parameter. # w_line: Controls attraction to brightness. Use negative values to attract to dark regions. # w_edge: Controls attraction to edges. Use negative values to repel snake from edges. snake = active_contour(gaussian(img, 3), init, alpha=0.015, beta=0.01, gamma=0.001, w_line=0, w_edge=1) # fit circle center, radius = circleFit(snake.astype(np.float32)) fitted_circle = circle(center, radius, radius, 360) if 0: plot1(img, init, snake, fitted_circle) snake_error = np.abs(
def gaussian_filter(image): a = filters.gaussian(image, sigma=1.5) return a
def watershed_scikit(self, input_img, cell_size=None, first_threshold=None, second_threshold=None): img_uint8 = cv2.copyMakeBorder(input_img, 5, 5, 5, 5, cv2.BORDER_CONSTANT, value=0) med_scikit = median(img_uint8, disk(1)) thresh = threshold_li(med_scikit) binary = med_scikit > thresh filled = ndimage.binary_fill_holes(binary) filled_blurred = gaussian(filled, 1) filled_int = (filled_blurred * 255).astype('uint8') # # edge_sobel = sobel(img_uint8) # # enhanced = 50*edge_sobel/edge_sobel.max() + img_uint8 # # enhanced.astype('uint8') # # med_scikit = median(img_uint8, disk(5)) thresh = threshold_li(filled_int) binary = filled_int > thresh filled = ndimage.binary_fill_holes(binary) filled_int = binary_opening(filled, disk(5)) filled_int = ndimage.binary_fill_holes(filled_int) # filled_blurred = gaussian(openeed, 3) # thresh = threshold_li(filled_int) # binary = filled_int > thresh #binary = binary_erosion(filled_int, disk(5)) distance = ndimage.distance_transform_edt(filled_int) binary1 = distance > first_threshold distance1 = ndimage.distance_transform_edt(binary1) binary2 = distance1 > second_threshold labeled_spots, num_features = label(binary2) spot_labels = np.unique(labeled_spots) spot_locations = ndimage.measurements.center_of_mass( binary2, labeled_spots, spot_labels[spot_labels > 0]) mask = np.zeros(distance.shape, dtype=bool) if spot_locations: mask[np.ceil(np.array(spot_locations)[:, 0]).astype(int), np.ceil(np.array(spot_locations)[:, 1]).astype(int)] = True markers, _ = ndimage.label(mask) labels = watershed(-distance, markers, mask=binary, compactness=0.5, watershed_line=True) boundary = find_boundaries(labels, connectivity=1, mode='thick', background=0) boundary_img = (255 * boundary[3:boundary.shape[0] - 3, 3:boundary.shape[1] - 3]).astype('uint8') resized_bound = cv2.resize(boundary_img, (input_img.shape[1], input_img.shape[0])) filled1 = ndimage.binary_fill_holes(resized_bound) mask = (255 * filled1).astype('uint8') - resized_bound boundary = resized_bound.astype('uint8') return boundary, mask
def process(self, save_to_disk=False, abs_path='', file_prefix=''): """Process roi and extract features Args: save_to_disk (bool, optional): Save the results to disk. Defaults to False. abs_path (str, optional): absolute path to location to save to. Defaults to ''. file_prefix (str, optional): roi file prefix. Defaults to ''. cfg (configparser, optional): loaded configparser object. Defaults to None. Returns: [dict]: all features and images """ if not self.loaded: return None # set the file path if file_prefix == '': file_prefix = self.filename[:-4] # set the output path if abs_path == '' and self.output_path is not None: abs_path = self.output_path # Store the paths so they can be used later for saving images self.file_prefix = file_prefix self.abs_path = abs_path # store the url subdir = os.path.basename(os.path.normpath(abs_path)) basedir = os.path.basename(os.path.normpath(os.path.dirname(os.path.normpath(abs_path)))) self.url = basedir + '/' + subdir + '/' + file_prefix # get a reference to 8bit version of the image img = self.img_8bit # Pull out some settings from cfg if available if self.cfg: min_obj_area = self.cfg['rois'].getint("min_obj_area",100) objs_per_roi = self.cfg['rois'].getint("objs_per_roi",1) deconv = self.cfg['rois'].get("deconv",'none') deconv_mask_weight = self.cfg['rois'].getfloat('deconv_mask_weight',0.6) deconv_iter = self.cfg['rois'].getint('deconv_iter',7) edge_thresh = self.cfg['rois'].getfloat("edge_threshold",2.5) use_jpeg = self.cfg['rois'].getboolean("use_jpeg",False) raw_color = self.cfg['rois'].getboolean("raw_color",False) int_features = self.cfg['rois'].getboolean("intensity_features",False) else: min_obj_area = 100 objs_per_roi = 1 deconv = 'none' deconv_mask_weight = 0.6 deconv_iter = 7 int_features = False use_jpeg = False raw_color = True edge_thresh = 2.5 # Define an empty dictionary to hold all features features = {} features['rawcolor'] = np.copy(img) gray = np.uint8(np.mean(img,2)) # edge-based segmentation edges_mag = scharr(gray) edges_med = np.median(edges_mag) edges_thresh = edge_thresh*edges_med edges = edges_mag >= edges_thresh edges = morphology.closing(edges,morphology.square(3)) filled_edges = ndimage.binary_fill_holes(edges) edges = morphology.erosion(filled_edges,morphology.square(3)) # Store the edge image bw_img = edges # Compute morphological descriptors label_img = morphology.label(bw_img,connectivity=2,background=0) props = measure.regionprops(label_img,gray) # clear bw_img bw_img = 0*bw_img # sort the region props props = sorted(props, reverse=True, key=lambda k: k.area) if len(props) > 0: # Init mask with the largest area object in the roi bw_img = (label_img)== props[0].label bw_img_all = bw_img.copy() base_area = props[0].area # use only the features from the object with the largest area max_area = 0 max_area_ind = 0 avg_area = 0.0 avg_maj = 0.0 avg_min = 0.0 avg_or = 0.0 avg_count = 0 if len(props) > objs_per_roi: n_objs = objs_per_roi else: n_objs = len(props) for f in range(0,n_objs): if props[f].area > min_obj_area: bw_img_all = bw_img_all + ((label_img)== props[f].label) avg_count = avg_count + 1 if f >= objs_per_roi: break # Take the largest object area as the roi area # no average avg_area = props[0].area avg_maj = props[0].major_axis_length avg_min = props[0].minor_axis_length avg_or = props[0].orientation avg_eccentricity = props[0].eccentricity avg_solidity = props[0].solidity # Calculate intensity features only for largest if int_features: features_intensity = intensity_features(gray, bw_img) features['intensity_gray'] = features_intensity features_intensity = intensity_features(img[::, ::, 0], bw_img) features['intensity_red'] = features_intensity features_intensity = intensity_features(img[::, ::, 1], bw_img) features['intensity_green'] = features_intensity features_intensity = intensity_features(img[::, ::, 2], bw_img) features['intensity_blue'] = features_intensity # Check for clipped image if np.max(bw_img_all) == 0: bw = bw_img_all else: bw = bw_img_all/np.max(bw_img_all) clip_frac = float(np.sum(bw[:,1]) + np.sum(bw[:,-2]) + np.sum(bw[1,:]) + np.sum(bw[-2,:]))/(2*bw.shape[0]+2*bw.shape[1]) features['clipped_fraction'] = clip_frac # Save simple features of the object features['area'] = avg_area features['minor_axis_length'] = avg_min features['major_axis_length'] = avg_maj if avg_maj == 0: features['aspect_ratio'] = 1 else: features['aspect_ratio'] = avg_min/avg_maj features['orientation'] = avg_or features['eccentricity'] = avg_eccentricity features['solidity'] = avg_solidity features['estimated_volume'] = 4.0 / 3 * pi * avg_maj * avg_min * avg_min else: features['clipped_fraction'] = 0.0 # Save simple features of the object features['area'] = 0.0 features['minor_axis_length'] = 0.0 features['major_axis_length'] = 0.0 features['aspect_ratio'] = 1 features['orientation'] = 0.0 features['eccentricity'] = 0 features['solidity'] = 0 features['estimated_volume'] = 0 self.features = features return features stats_only_features = features # Masked background with Gaussian smoothing, image sharpening, and # reduction of chromatic aberration # mask the raw image with smoothed foreground mask blurd_bw_img = gaussian(bw_img_all,3) img[:,:,0] = img[:,:,0]*blurd_bw_img img[:,:,1] = img[:,:,1]*blurd_bw_img img[:,:,2] = img[:,:,2]*blurd_bw_img # renormalize if np.max(img) == 0: img = np.float32(img) else: img = np.float32(img)/np.max(img) if deconv.lower() == 'um' or deconv.lower() == 'lr': # Get the intesity image in HSV space hsv_img = color.rgb2hsv(img) v_img = hsv_img[:,:,2] # mask the raw image with smoothed foreground mask blurd_bw_img = gaussian(bw_img_all,3) v_img = v_img*blurd_bw_img if deconv.lower() == 'um': old_mean = np.mean(v_img) blurd = gaussian(v_img,1.0) hpfilt = v_img - blurd*deconv_mask_weight v_img = hpfilt/(1-deconv_mask_weight) new_mean = np.mean(v_img) if (new_mean) != 0: v_img = v_img*old_mean/new_mean v_img[v_img > 1] = 1 v_img = np.uint8(255*v_img) if deconv.lower() == 'lr': # Make a guess of the PSF for sharpening psf = make_gaussian(5, 3, center=None) v_img[v_img == 0] = 0.0001 v_img = restoration.richardson_lucy(v_img, psf, deconv_iter) v_img[v_img < 0] = 0 if np.max(v_img) == 0: v_img = np.uint8(255*v_img) else: v_img = np.uint8(255*v_img/np.max(v_img)) # restore the rbg image from hsv hsv_img[:,:,2] = v_img img = color.hsv2rgb(hsv_img) # Rescale image to uint8 0-255 img[img < 0] = 0 if np.max(img) == 0: img = np.uint8(255*img) else: img = np.uint8(255*img/np.max(img)) features['image'] = img features['binary'] = 255*bw_img_all self.features = features # Save the binary image and also color image if requested if save_to_disk: self.save_to_disk() return features
import numpy as np import pylab import sys from skimage import io from skimage import color from scipy import ndimage from matplotlib import pyplot import project_helpers as ph from sklearn.multiclass import OneVsRestClassifier from sklearn.svm import SVC from sklearn import svm from sklearn.preprocessing import LabelBinarizer from sklearn.ensemble import RandomForestClassifier ############# # This tutorial walks through various functions used for the project # and some of the concepts covered in lecture. ############## # Flag that indicates whether to show images. VIEW = False # Set the path to the data files. data_path = "crchistophenotypes_2016_04_28/CRCHistoPhenotypes_2016_04_28/" ############# #Basic image I/O ############## #Load the ground truth labels #Detections are stored as [x,y] and labels are positive for detected nuclei centres img, detections, labels = ph.load_data_set(data_path,"img7") pylab.imshow(img) if(VIEW): pyplot.show()
for im_name in im_names: im = io.imread('/media/data_cifs/andreas/pathology/2018-04-26/mar2019/LMD/annotation_imgs/%s' % im_name)[:2000, :2000, :] feed_dict = { val_dict[dict_image_key]: im[None, ...], val_dict[dict_label_key]: it_labs} it_val_dict = sess.run(val_dict, feed_dict=feed_dict) if 'KRAS' in im_name: pos, neg = 0, 2 elif 'EGFR' in im_name: pos, neg = 1, 3 else: raise NotImplementedError f = plt.figure(figsize=(10, 10)) trans_im = sigmoid_fun(transform.resize(filters.gaussian((it_val_dict['activity'].squeeze()[..., pos]), 3), [2000, 2000])) marked = segmentation.mark_boundaries(im, (trans_im > .5).astype(int).squeeze(), color=None, outline_color=[1, 0, 0], mode='outer') plt.imshow(marked[100:-100, 100:-100]) # plt.imshow(np.concatenate((im.astype(np.float32) / 255., trans_im[..., None]), axis=-1)) plt.savefig('pos_%s.png' % im_name.split('.')[0]) # plt.show() plt.close(f) f = plt.figure(figsize=(10, 10)) trans_im = 1 - sigmoid_fun(transform.resize(filters.gaussian((it_val_dict['activity'].squeeze()[..., neg]), 3), [2000, 2000])) marked = segmentation.mark_boundaries(im, (trans_im > .5).astype(int).squeeze(), color=None, outline_color=[0, 0, 1], mode='outer') plt.imshow(marked[100:-100, 100:-100]) # plt.imshow(np.concatenate((im, trans_im[..., None]), axis=-1)) plt.savefig('neg_%s.png' % im_name.split('.')[0]) # plt.show() plt.close(f)
def Hough_center(data_dcm_directory, Image_file, Image, Is_a_file, sig, pui, thr, resol_r=1, resol_x=1, resol_y=1): if Is_a_file: #Lecture des données pixel du fichier DICOM Lec = sitk.ReadImage(os.path.join(data_dcm_directory, Image_file)) #Conversion en array Image = sitk.GetArrayFromImage(Lec[:, :, 0]) space_y = Lec.GetSpacing()[0] space_x = Lec.GetSpacing()[1] #Conversion de l'image de non signé 16bits vers le type float Image = Image.astype(float) #Lissage de l'image par un filtre gaussien IG8 = filters.gaussian(Image, sigma=sig) # IG8 = filters.median(Image,selem=morphology.square(5)) #Augmentation du contraste de l'image filtrée IG8 = IG8 / (0.7 * np.max(IG8)) IG8 = IG8**pui # Obtention de l'image de gradient par filtre de Scharr Scharr = filters.scharr(IG8) #Définition du seuil pour l'image de gradient B = thr * np.max(Scharr) Thresh = Scharr * (Scharr > B) #Par soucis d'economie en temps de calcul et gestion de mémoire, on va rogner notre espace de travail. En effet, si on travaille avec une forme circulaire, travailler sur l'ensemble de l'image n'a pas d'intérêt : l'information est localisée. #On cherche donc les indices minimums et maximums selon les deux axes de l'image, et on travaille donc sur un espace de Hough réduit, et un remplissage depuis une image tronquée. y_min = np.min(np.nonzero(Thresh)[0]) y_max = np.max(np.nonzero(Thresh)[0]) x_min = np.min(np.nonzero(Thresh)[1]) x_max = np.max(np.nonzero(Thresh)[1]) Dx = x_max - x_min Dy = y_max - y_min Dxt = int(round(0.3 * (Dx))) # Marges en x et y au delà de xmin et xmax Dyt = int(round(0.3 * (Dy))) #Figure de contrôle : Image de gradient seuillé # plt.figure() # plt.title("Image de seuil de " + str(Image_file)) # plt.imshow(Thresh[y_min-Dyt:y_max+Dyt+1,x_min-Dxt:x_max+Dxt+1]) # #Figure de contrôle : Image de base après pré-traitement # plt.figure() # plt.title("Image filtrée de " + str(Image_file)) # plt.imshow(IG8[y_min-Dyt:y_max+Dyt+1,x_min-Dxt:x_max+Dxt+1]) D = max(Dx, Dy) Dt = max(Dxt, Dyt) #Valeurs de contrôles affichées. # print(y_min,y_max,x_min,x_max,Dx,Dy,Dxt,Dyt,Nrt) #Création de l'espace de Hough Hough_space = np.zeros((int(Dt / resol_r), int( (2 * D) / resol_y) + 1, int((2 * D) / resol_x) + 1), dtype=float) # Création de l'espace de Hough beta_x = (1 / 2) * (1 - resol_x) beta_y = (1 / 2) * (1 - resol_y) # n=0 for yi in range(Dy + 1): # Les xi et yi (image) ne sont non nuls for xi in range(Dx + 1): # que sur les True de l'image de seuil. T = Thresh[yi + y_min, xi + x_min] if (T != 0): for rh in range(0, int((Dt / resol_r))): xh = np.arange(int((2 * D) / resol_y) + 1) #On crée un tableau vide de même yh = np.arange(int( (2 * D) / resol_x) + 1) #taille que les dim x et y de #l'espace de Hough #Masque circulaire de centre (xi, yi), de rayon (D/2-Dt) + rh # Calcul du coefficient epsilon de l'équation (x-xc)**2 + ( y-yc)**2 - (r-r0)**2 < epsilon**2 alpha = np.sqrt((resol_x / 2)**2 + (resol_y / 2)**2) epsilon2 = abs(2 * alpha * (rh * resol_r + (D / 2) - Dt) + alpha**2) # Matrice conditionnelle sur epsilon : on calcule tous les membres de gauche de l'eq au dessus cond = abs((xh[np.newaxis, :] * resol_x - beta_x - (xi + D / 2))**2 + (yh[:, np.newaxis] * resol_y - beta_y - (yi + D / 2))**2 - (rh * resol_r + D / 2 - Dt)**2) # Test : Soit on est inférieur à epsilon2 et on garde la valeur, sachant que plus on est proche de epsilon moins on a de poids # Soit on est supérieur et on prend la valeur 0. np clip assure que toutes les valeurs sont comprises entre 0 et 1. # La condition <1 est inutile ici, mais un argument doit être passé à np clip. On met 1.1 pour ne pas exclure les cas # parfaits ou cond = 0. w = np.clip((epsilon2 - cond) / epsilon2, 0, 1.1) # Matrice des poids # if n==30: # plt.figure() # plt.subplot(121) # plt.imshow(w) # plt.subplot(122) # plt.imshow(cond) # Boucle à activer pour démonstration. Penser à décommenter au dessus n=0 et en # print(epsilon2) # dessous n+=1. Hough_space[int(rh)] += w * T # n+=1 #Augmentation du contraste de l'espace de Hough Hough_space = Hough_space / np.percentile(Hough_space, 95) Hough_space_10 = Hough_space**5 #Valeur max de l'espace de Hough : le cdm n'a de sens qu'a proximité Max_Hough = np.max(Hough_space_10) #On retire donc toutes les valeurs trop éloignées du max Hough_trunc = np.where(Hough_space_10 > 0.01 * Max_Hough, Hough_space_10, 0) #Centre de masse de l'espace de Hough cdm_10 = ndimage.measurements.center_of_mass(Hough_trunc) #Figures de contrôle # print(cdm_10[0],D,Dt) # plt.figure() # plt.title("Espace de Hough proche du max de "+str(Image_file)) # plt.subplot(121) # plt.imshow(Hough_space_10[int(np.ceil(cdm_10[0])),:,:]) # plt.subplot(122) # plt.imshow(Hough_space_10[int(np.floor(cdm_10[0])),:,:]) r_est_10 = cdm_10[0] * resol_r + D / 2 - Dt y_est_10 = (cdm_10[1]) * resol_y + y_min - (D / 2) - beta_y x_est_10 = (cdm_10[2]) * resol_x + x_min - (D / 2) - beta_x if not Is_a_file: space_y = 0.336 space_x = 0.336 print("x_est = ", round(x_est_10, 3), ", y_est = ", round(y_est_10, 3), ", r_est = ", round(r_est_10, 3)) # Coordonnées en pixel dans l'image return (x_est_10, y_est_10, r_est_10, space_x, space_y)
def get_grad_plots(nne, nni, e, i, nims, ims, logits, nlogits, out_dir, ran=None, save_figs=False, dpi=300, sig=1): """Gradients from the hGRU on pathfinder.""" font = { 'family': 'normal', # 'weight' : 'bold', 'size': 8 } matplotlib.rc('font', **font) gmin = -8 # np.min([nze, nzi, ze, zi]) gmax = 8 # np.max([nze, nzi, ze, zi]) if ran is None: ran = range(len(nne)) count = 0 for im in ran: f = plt.figure() nze = zscore(nne[im], munne, sdnne) # zscore(nni[im], munni, sdnni) nzi = zscore(nni[im], munni, sdnni) # zscore(nne[im], munne, sdnne) ze = zscore(e[im], mue, sde) zi = zscore(i[im], mui, sdi) # - ax1 = plt.subplot(2, 4, 2) plt.title('$H^{(1)}$') im1 = ax1.imshow(gaussian(nze, sig, preserve_range=True), cmap='PuOr_r', vmin=-4, vmax=4) prep_subplot(f=f, im=im1, ax=ax1) ax2 = plt.subplot(2, 4, 3) plt.title('$H^{(2)}$') im2 = ax2.imshow(gaussian(nzi, sig, preserve_range=True), cmap='PuOr_r', vmin=-4, vmax=4) prep_subplot(f=f, im=im2, ax=ax2) ax3 = plt.subplot(2, 4, 4) ndif = gaussian(nze, sig, preserve_range=True) + gaussian( nzi, sig, preserve_range=True) plt.title('$H^{(2)} + H^{(1)}, max=%s$' % np.around(ndif.max(), 2)) im3 = ax3.imshow((ndif), cmap='RdBu_r', vmin=gmin, vmax=gmax) prep_subplot(f=f, im=im3, ax=ax3, ticks=[-8, 8]) plt.subplot(2, 4, 1) plt.title('Decision: %s' % nlogits[im]) plt.imshow(nims[im], cmap='Greys_r') plt.axis('off') # + ax4 = plt.subplot(2, 4, 6) plt.title('$H^{(1)}$') im4 = ax4.imshow(gaussian(ze, sig, preserve_range=True), cmap='PuOr_r', vmin=-4, vmax=4) prep_subplot(f=f, im=im4, ax=ax4) ax5 = plt.subplot(2, 4, 7) plt.title('$H^{(2)}$') im5 = ax5.imshow(gaussian(zi, sig, preserve_range=True), cmap='PuOr_r', vmin=-4, vmax=4) prep_subplot(f=f, im=im5, ax=ax5) ax6 = plt.subplot(2, 4, 8) dif = gaussian(ze, sig, preserve_range=True) + gaussian( zi, sig, preserve_range=True) plt.title('$H^{(2)} + H^{(1)}, max=%s$' % np.around(dif.max(), 2)) im6 = ax6.imshow((dif), cmap='RdBu_r', vmin=gmin, vmax=gmax) prep_subplot(f=f, im=im6, ax=ax6, ticks=[-8, 8]) plt.subplot(2, 4, 5) plt.title('Decision: %s' % logits[im]) plt.imshow(ims[im], cmap='Greys_r') plt.axis('off') if save_figs: out_path = os.path.join(out_dir, '%s.png' % count) count += 1 plt.savefig(out_path, dpi=dpi) else: plt.show() plt.close(f)
def spatter(x, severity=1): iscolor = len(x.shape) > 2 and x.shape[2] > 1 c = [(0.65, 0.3, 4, 0.69, 0.6, 0), (0.65, 0.3, 3, 0.68, 0.6, 0), (0.65, 0.3, 2, 0.68, 0.5, 0), (0.65, 0.3, 1, 0.65, 1.5, 1), (0.67, 0.4, 1, 0.65, 1.5, 1)][severity - 1] x = np.array(x, dtype=np.float32) / 255. liquid_layer = np.random.normal(size=x.shape[:2], loc=c[0], scale=c[1]) liquid_layer = gaussian(liquid_layer, sigma=c[2]) liquid_layer[liquid_layer < c[3]] = 0 if c[5] == 0: liquid_layer = (liquid_layer * 255).astype(np.uint8) dist = 255 - cv2.Canny(liquid_layer, 50, 150) dist = cv2.distanceTransform(dist, cv2.DIST_L2, 5) _, dist = cv2.threshold(dist, 20, 20, cv2.THRESH_TRUNC) dist = cv2.blur(dist, (3, 3)).astype(np.uint8) dist = cv2.equalizeHist(dist) ker = np.array([[-2, -1, 0], [-1, 1, 1], [0, 1, 2]]) dist = cv2.filter2D(dist, cv2.CV_8U, ker) dist = cv2.blur(dist, (3, 3)).astype(np.float32) m = cv2.cvtColor(liquid_layer * dist, cv2.COLOR_GRAY2BGRA) m /= np.max(m, axis=(0, 1)) m *= c[4] # water is pale turqouise color = np.concatenate( (175 / 255. * np.ones_like(m[..., :1]), 238 / 255. * np.ones_like(m[..., :1]), 238 / 255. * np.ones_like(m[..., :1])), axis=2) color = cv2.cvtColor(color, cv2.COLOR_BGR2BGRA) if len(x.shape) > 2 and x.shape[2] > 1: x = cv2.cvtColor(x, cv2.COLOR_BGR2BGRA) x = np.clip(x + m * color, 0, 1) * 255 if iscolor: return cv2.cvtColor(x, cv2.COLOR_BGRA2BGR) else: return cv2.cvtColor(x, cv2.COLOR_BGRA2GRAY) else: m = np.where(liquid_layer > c[3], 1, 0) m = gaussian(m.astype(np.float32), sigma=c[4]) m[m < 0.8] = 0 # mud brown color = np.concatenate( (63 / 255. * np.ones_like(x[..., :1]), 42 / 255. * np.ones_like(x[..., :1]), 20 / 255. * np.ones_like(x[..., :1])), axis=2) color *= m[..., np.newaxis] x *= (1 - m[..., np.newaxis]) x = np.clip(x + color, 0, 1) * 255 if iscolor: return x else: return cv2.cvtColor(x, cv2.COLOR_BGR2GRAY)