def detectOpticDisc(image): kernel = octagon(10, 10) thresh = threshold_otsu(image[:,:,1]) binary = image > thresh print binary.dtype luminance = convertToHLS(image)[:,:,2] t = threshold_otsu(luminance) t = erosion(luminance, kernel) labels = segmentation.slic(image[:,:,1], n_segments = 3) out = color.label2rgb(labels, image[:,:,1], kind='avg') skio.imshow(out) x, y = computeCentroid(t) print x, y rows, cols, _ = image.shape p1 = closing(image[:,:,1],kernel) p2 = opening(p1, kernel) p3 = reconstruction(p2, p1, 'dilation') p3 = p3.astype(np.uint8) #g = dilation(p3, kernel)-erosion(p3, kernel) #g = rank.gradient(p3, disk(5)) g = cv2.morphologyEx(p3, cv2.MORPH_GRADIENT, kernel) #markers = rank.gradient(p3, disk(5)) < 10 markers = drawCircle(rows, cols, x, y, 85) #markers = ndimage.label(markers)[0] #skio.imshow(markers) g = g.astype(np.uint8) #g = cv2.cvtColor(g, cv2.COLOR_GRAY2RGB) w = watershed(g, markers) print np.max(w), np.min(w) w = w.astype(np.uint8) #skio.imshow(w) return w
def run(args): logging.basicConfig(level=logging.INFO) slide = openslide.OpenSlide(args.wsi_path) # note the shape of img_RGB is the transpose of slide.level_dimensions img_RGB = np.transpose(np.array(slide.read_region((0, 0), args.level, slide.level_dimensions[args.level]).convert('RGB')), axes=[1, 0, 2]) img_HSV = rgb2hsv(img_RGB) background_R = img_RGB[:, :, 0] > threshold_otsu(img_RGB[:, :, 0]) background_G = img_RGB[:, :, 1] > threshold_otsu(img_RGB[:, :, 1]) background_B = img_RGB[:, :, 2] > threshold_otsu(img_RGB[:, :, 2]) tissue_RGB = np.logical_not(background_R & background_G & background_B) tissue_S = img_HSV[:, :, 1] > threshold_otsu(img_HSV[:, :, 1]) min_R = img_RGB[:, :, 0] > args.RGB_min min_G = img_RGB[:, :, 1] > args.RGB_min min_B = img_RGB[:, :, 2] > args.RGB_min tissue_mask = tissue_S & tissue_RGB & min_R & min_G & min_B np.save(args.npy_path, tissue_mask)
def evaluate(self,img): img = cv2.medianBlur(img,5) if self.auto_thresh: self.thresh = threshold_otsu(img) if self.white_spots: bw = (img > self.thresh).astype(np.uint8) else: bw = (img <= self.thresh).astype(np.uint8) #cv2.imshow(self.name,bw*255) #cv2.waitKey(5) if not .1* img.size < np.count_nonzero(bw) < .8*img.size: print("reevaluating threshold!!") print("Ratio:",np.count_nonzero(bw)/img.size) print("old:",self.thresh) self.thresh = threshold_otsu(img) print("new:",self.thresh) m = cv2.moments(bw) r = {} try: r['x'] = m['m10']/m['m00'] r['y'] = m['m01']/m['m00'] except ZeroDivisionError: return -1 x,y,w,h = cv2.boundingRect(bw) #if (h,w) == img.shape: # return -1 r['bbox'] = y,x,y+h,x+w return r
def duel(frame): threshold = threshold_otsu(frame) binary1 = frame > threshold frame = frame - binary1 * frame threshold = threshold_otsu(frame) binary2 = frame > threshold binary = binary1 & binary2 return binary
def CleanImgage(self, thresh = 30): # Image processing footprint = np.array([[-1,-1,-1],[-1,8,-1], [-1,-1,-1]]) self.clean_image = cv2.medianBlur(self.image, 5) self.clean_image = cv2.filter2D(self.clean_image,-1,footprint) self.clean_image = cv2.medianBlur(self.clean_image, 5) self.markers = np.zeros_like(self.image) self.markers[self.clean_image < threshold_otsu(self.image)] = 1 self.markers[self.clean_image >= ((threshold_otsu(self.image)*thresh)/100)] = 2 self.markers[self.clean_image >= ((threshold_otsu(self.image)*50)/100)] = 3
def watershed_counter(path_image): # load the image and convert it to a floating point data type rgb_image = img_as_float(io.imread(path_image)) image = rgb2grey(rgb_image) bin_image = image > threshold_otsu(image) fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(8, 2.5)) ax1.imshow(image, cmap=plt.cm.gray) ax1.set_title('Original Image') ax1.axis('off') ax2.hist(image) ax2.set_title('Otsu Thresholded Histogram') ax2.axvline(threshold_otsu(image), color='r') ax3.imshow(bin_image, cmap=plt.cm.gray) ax3.set_title('Thresholded Image') ax3.axis('off') # Now we want to separate the two objects in image # Generate the markers as local maxima of the distance to the background distance = ndi.distance_transform_edt(bin_image) local_maxi = peak_local_max(distance, indices=False, footprint=np.ones((3, 3)), labels=bin_image) markers = ndi.label(local_maxi)[0] labels = watershed(distance, markers, mask=bin_image) regions = regionprops(labels) regions = [r for r in regions if r.area > 50] num = len(regions) fig, axes = plt.subplots(ncols=4, figsize=(8, 2.7)) ax0, ax1, ax2, ax3 = axes ax0.imshow(image, cmap=plt.cm.gray, interpolation='nearest') ax0.set_title('Overlapping objects') ax1.imshow(-distance, cmap=plt.cm.jet, interpolation='nearest') ax1.set_title('Distances') ax2.imshow(labels, cmap=plt.cm.spectral, interpolation='nearest') ax2.set_title(str(num) + ' Total Objects') ax3.imshow(rgb_image, cmap=plt.cm.gray, interpolation='nearest') ax3.contour(labels, [0.5], linewidths=1.2, colors='y') ax3.axis('off') for ax in axes: ax.axis('off') fig.subplots_adjust(hspace=0.01, wspace=0.01, top=1, bottom=0, left=0, right=1) print num plt.show()
def initial_segmentation(self, emphasized): binary_img = np.zeros(emphasized.shape) for z in range(0, emphasized.shape[0]): th = threshold_otsu(emphasized[z, :, :]) binary_img[z, :, :] = emphasized[z, :, :] > th tmp = np.multiply(emphasized[z, :, :], binary_img[z, :, :]) tmp[tmp < 0] = 0 th = threshold_otsu(tmp) binary_img[z, :, :] = tmp > th binary_img[z, :, :] = remove_small_objects(binary_img[z, :, :].astype(bool), 5) binary_img = binary_img.astype('i4') return binary_img
def load_data(size): """ Loads data from /chars and /notChars to train binary Classifier """ char_files = glob.glob("/users/derekjanni/pyocr/chars/*") non_files = glob.glob("/users/derekjanni/pyocr/notChars/*") # pre-process character data X_char = [] for i in char_files: img = Image.open(i) img = img.convert("L") img = img.resize((size, size)) image = np.asarray(img).astype("int32") image.setflags(write=True) thresh = threshold_otsu(image) binary = image > thresh X_char.append(binary) Y_char = [1 for i in range(len(X_char))] # pre-process non-char data X_non = [] for i in char_files: img = Image.open(i) img = img.convert("L") img = img.resize((size, size)) image = np.asarray(img).astype("int32") image.setflags(write=True) thresh = threshold_otsu(image) binary = image > thresh X_non.append(binary) # add 1000 circles of varying radius and position for i in range(1000): radius = np.random.randint(10, 15) + np.random.randint(5) offset = np.random.randint(-5, 5) img = np.ones((size, size), dtype=np.int32) cv2.circle(img, ((offset + size) / 2, (offset + size) / 2), radius, (0, 0, 0), -1) X_non.append(img) Y_non = [0 for i in range(len(X_non))] X = np.asarray(X_char + X_non).reshape(-1, 1, 50, 50).astype(np.float32) Y = np.asarray(Y_char + Y_non).astype(np.int32) # some trickery to emulate a train-test split indices = list(zip(X, Y)) # name to indicate what's going on here, not the actual data struct random.shuffle(indices) X, Y = zip(*indices) X = np.asarray(X) Y = np.asarray(Y) return X, Y
def read_input_otsu(): print("Otsu") alphabet=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"] #alphabet=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,17,19,20,21,2] n=70 train_data=[] train_solutions=[] for i in range(n): for letter in alphabet: image = imread("chars74k-lite/chars74k-lite/"+letter+"/"+letter+"_"+str(i)+".jpg") img = np.asarray(image) img.setflags(write=True) thresh = threshold_otsu(img) binary = image > thresh binary = np.reshape(binary,(1,400)) train_data.append(np.asarray(binary[0]).astype(float)) # t=[0]*26#init all vectors to zero t[ord(letter)-97] = 1 t = np.asarray(t) train_solutions.append(t) #print(train_data[0]) #train_data = threshold_otsu(train_data) #print(train_data[0]) # m=88 test_data=[] test_solutions=[] for j in range(n,m): for letter in alphabet: image = imread("chars74k-lite/chars74k-lite/"+letter+"/"+letter+"_"+str(j)+".jpg") img = np.asarray(image) img.setflags(write=True) thresh = threshold_otsu(img) binary = image > thresh binary = np.reshape(binary,(1,400)) test_data.append(np.asarray(binary[0]).astype(float)) # t=[0]*26#init all vectors to zero t[ord(letter)-97] = 1 t = np.asarray(t) test_solutions.append(t) # '''print(len(train_data)) print(len(train_solutions)) print(len(test_data)) print(len(test_solutions))''' return train_data, train_solutions, test_data, test_solutions
def nln(src_image): orig_image = io.imread(src_image, 0) if len(orig_image.shape) == 3: image = (orig_image.sum(axis=2) / 3).astype('ubyte') else: image = orig_image h, w = image.shape thresh = filters.threshold_otsu(image) binary = (image < thresh).astype('ubyte') # 二值化,黑色为1 sum_of_black_pixels = np.sum(binary) if sum_of_black_pixels == 0: raise Exception("white or black image.") binary_line = binary.sum(axis=0) # 在X轴上投影 hx = binary_line / (sum_of_black_pixels * 1.0) binary_line_v = binary.sum(axis=1) # 在Y轴上投影 hy = binary_line_v / (sum_of_black_pixels * 1.0) H = 32 W = 32 binary_new = np.zeros( (H, W) ) for y in range(h): for x in range(w): if binary[y,x] == 1: x2 = int((W-1) * np.sum(hx[0:x+1])) y2 = int((H-1) * np.sum(hy[0:y+1])) x2_end = int((W - 1) * np.sum(hx[0:x + 2])) y2_end = int((H - 1) * np.sum(hy[0:y + 2])) if y == h - 1: y2 = H - 1 y2_end = H if x == w - 1: x2 = W - 1 x2_end = W binary_new[y2:y2_end, x2:x2_end] = binary[y,x] return binary_new.astype('ubyte')
def skeletonize_mitochondria(mch_channel): mch_collector = np.max(mch_channel, axis=0) # TODO: check max projection v.s. sum skeleton_labels = np.zeros(mch_collector.shape, dtype=np.uint8) # thresh = np.max(mch_collector)/2. thresh = threshold_otsu(mch_collector) # use adaptative threshold? => otsu seems to be sufficient in this case skeleton_labels[mch_collector > thresh] = 1 skeleton2 = skeletonize(skeleton_labels) skeleton, distance = medial_axis(skeleton_labels, return_distance=True) active_threshold = np.mean(mch_collector[skeleton_labels]) * 5 # print active_threshold transform_filter = np.zeros(mch_collector.shape, dtype=np.uint8) transform_filter[np.logical_and(skeleton > 0, mch_collector > active_threshold)] = 1 skeleton = transform_filter * distance skeleton_ma = np.ma.masked_array(skeleton, skeleton > 0) skeleton_convolve = ndi.convolve(skeleton_ma, np.ones((3, 3)), mode='constant', cval=0.0) divider_convolve = ndi.convolve(transform_filter, np.ones((3, 3)), mode='constant', cval=0.0) skeleton_convolve[divider_convolve > 0] = skeleton_convolve[divider_convolve > 0] / \ divider_convolve[divider_convolve > 0] new_skeleton = np.zeros_like(skeleton) new_skeleton[skeleton2] = skeleton_convolve[skeleton2] skeleton = new_skeleton return skeleton_labels, mch_collector, skeleton, transform_filter
def get_monograms_otsu(img, cropped): otsu_img = img_as_ubyte(np.copy(np.asarray(cropped.convert('L')))) try: threshold_global_otsu = threshold_otsu(otsu_img) except TypeError: print 'Something weird happened' return None, None, None global_otsu = np.array(otsu_img >= threshold_global_otsu).astype(np.int64) hist = np.zeros(global_otsu.shape[1]) for col in range(global_otsu.shape[1]): max_white = 0 white = 0 for row in range(global_otsu.shape[0]): white += 1 if global_otsu[row, col] == 1 else 0 max_white = max(white, max_white) hist[col] = max_white hist = np.convolve(hist, 5 * [1 / 5.], 'same') / 25 for idx in range(1, len(hist) - 1): if hist[idx] == hist[idx + 1] and hist[idx - 1] < hist[idx]: hist[idx] = (hist[idx + 1] + hist[idx - 1]) / 2 maxes = argrelextrema(hist, np.greater) cuts = maxes[0] monograms = [] for idx, cut in enumerate(cuts): for next in cuts[idx + 1:]: if 10 < next - cut < 60: monograms.append(N_gram(img[:, cut:next], cut, next)) return monograms, cuts[0], cuts[-1]
def find_single_seed(image_filename, output_filename): image = Image.from_file(image_filename) w, h = 500, 500 tube_section = image[1024-w:1024+w,1024-h:1024+h] threshold = threshold_otsu(tube_section) thresholded = tube_section > threshold x, y, r = find_inner_circle_parameters(thresholded, 400, 500) # FIXME - think routine is finding outer circle stripped = strip_outside_circle(thresholded, (x, y), 300) eroded = binary_erosion(stripped, structure=np.ones((10, 10))) float_coords = map(np.mean, np.where(eroded > 0)) ix, iy = map(int, float_coords) w, h = 100, 100 selected = tube_section[ix-w:ix+w,iy-h:iy+h] with open(output_filename, 'wb') as f: f.write(selected.view(Image).png())
def im_proc(im): """Apply series of morphological procedures on image.""" th = threshold_otsu(im) im_bin = im > th return(ndi.binary_fill_holes( morphology.closing( im_bin,np.ones((3,3)))))
def thresholding(image, threshold): if isinstance(threshold,int) or isinstance(threshold,float): thresh = threshold elif isinstance(threshold,str): # Assume its Ok to use the same threshold for each layer. parsestr = threshold.split(" ") parsestr = [i.lower() for i in parsestr] parsestr = set(parsestr) if "otsu" in parsestr: if "global" in parsestr: ind = np.argmax([np.mean(i) for i in image]) thresh = filters.threshold_otsu(image[ind]) print thresh, ind elif "local" in parsestr: radius = int(parsestr[2]) mask = morphology.disk(radius) thresh = filters.rank.otsu(image,mask) if "li" in parsestr: thresh = filters.threshold_li(image) if "yen" in parsestr: thresh = filters.threshold_yen(image) threshinds = image<thresh binimg = np.ones(image.shape) binimg[threshinds] = 0 return binimg, thresh
def segment_Image(im,depth): ## Setting lower threshold using Otsu method. thresh_l = 0.8*threshold_otsu(im) print thresh_l img = sitk.GetImageFromArray(im) ## Smoothing image. imgSmooth = sitk.CurvatureFlow(image1=img, timeStep=0.125, numberOfIterations=10) lstSeeds=[] for i in range(0, im.shape[1]-1, 10 ): for j in range (0, im.shape[0]-1, 10 ): if im[j][i]>0: seed = [int(i), int(j)] lstSeeds.append( seed ) ## Segmenting image. imgWhiteMatter = sitk.ConnectedThreshold(image1=imgSmooth, seedList=lstSeeds, lower=thresh_l, upper=255, replaceValue=255) nda = sitk.GetArrayFromImage(imgWhiteMatter) nda.astype(bool) nda1=remove_small_objects(nda,150,connectivity=3) nda1.astype(int) newdepth = depth return nda1,newdepth
def cont(imagen, depth=2**16, gaussian=3, screenpercent=0.7,t=0): imagen = gaussian_filter(imagen, gaussian) if t==0: otsu = threshold_otsu(imagen, depth) elif t==1: otsu = filters.threshold_isodata(imagen, depth) else: otsu = filters.threshold_li(imagen) imagen = binarizar(imagen, otsu) imagen = gaussian_filter(imagen, gaussian) contours = measure.find_contours(imagen, 1) centro = np.asanyarray([1280*0.5, 960*0.5]) while len(contours) > 1: if sum(np.abs(centro - contours[1].mean(axis=0)) < [1280*screenpercent*0.5, 960*screenpercent*0.5]) != 2: del contours[1] elif sum(np.abs(centro - contours[0].mean(axis=0)) < [1280*screenpercent*0.5, 960*screenpercent*0.5]) != 2: del contours[0] else: if contours[1].size < contours[0].size: del contours[1] else: del contours[0] return imagen, contours[0]
def locate(i): """ Median subtract each hologram, convolve with Mexican hat kernel, then smooth the absolute value of the convolution, and use Otsu's thresholding to segment the image into specimens. Record the time, x and y centroids, and some intensity features within each segment. """ median_sub_holo = hologram_cube[i, ...] - median_holo conv_holo = convolve_fft(median_sub_holo, MexicanHat2DKernel(convolution_kernel_radius), fftn=fft2, ifftn=ifft2) smooth_abs_conv = gaussian_filter(np.abs(conv_holo), gaussian_kernel_radius) thresh = threshold_otsu(smooth_abs_conv - np.median(smooth_abs_conv)) # thresh = threshold_yen(smooth_abs_conv - np.median(smooth_abs_conv)) masked = np.ones_like(smooth_abs_conv) masked[smooth_abs_conv <= thresh] = 0 label_image = label(masked) regions = regionprops(label_image, smooth_abs_conv) for region in regions: centroid = region.weighted_centroid pos = (i, centroid[0], centroid[1], region.max_intensity, region.mean_intensity) positions.append(pos)
def mouse_centroid(im, previous_centroid): """ Find mouse's centroid in a single image Parameters: im = image of analyze (numpy array) previous_centroid = coordinates of the mouse in the previous frame Returns: Coordinates of the mouse's centroid """ original = copy(im) im = im < filters.threshold_otsu(im) * 0.2 distance = ndimage.distance_transform_edt(im) centers = (distance > 0.8 * distance.max()) if len(centers) == 0: return previous_centroid labels = label(centers) centroids = [r.weighted_centroid for r in regionprops(labels, original)] if len(centroids) == 1: return list(centroids[0]) elif len(centroids) > 1: d = lambda a, b: ((a[0] - b[0])**2 + (a[1] - b[1])**2)**0.5 dists = [d(c, previous_centroid) for c in centroids] d_idx = np.array(dists == np.min(dists)) return list(np.array(centroids)[d_idx][0])
def read_img_bw(self, image_file, array_type="one_zero"): ''' Read an image from a file and return its matrix inputs: image_file: full path to the image file including extension array_type: String with two values: "one_zero": matrix is uint8 with only 1s and 0s "bool": matrix is bool with True and False ''' #read image and convert to matrix image = Image.open(image_file).convert('L') # image = image.resize((100,100), Image.LANCZOS) image_array = image.getdata() image_array = numpy.array(image_array).astype(numpy.uint8).reshape((image.size[0],image.size[1])) # print image_array.shape #Threshold the image according to array_type parameter if array_type == "bool": thresh = threshold_otsu(image_array) image_array = image > thresh image_array = ~image_array else: image_array[image_array < 128] = 1 image_array[image_array >= 128] = 0 image_array[image_array == 1] = 255 # plt.imshow(image_array) # plt.show() return image_array
def plot_preprocessed_image(self): """ plots pre-processed image. The plotted image is the same as obtained at the end of the get_text_candidates method. """ image = restoration.denoise_tv_chambolle(self.image, weight=0.1) thresh = threshold_otsu(image) bw = closing(image > thresh, square(2)) cleared = bw.copy() label_image = measure.label(cleared) borders = np.logical_xor(bw, cleared) label_image[borders] = -1 image_label_overlay = label2rgb(label_image, image=image) fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(12, 12)) ax.imshow(image_label_overlay) for region in regionprops(label_image): if region.area < 10: continue minr, minc, maxr, maxc = region.bbox rect = mpatches.Rectangle((minc, minr), maxc - minc, maxr - minr, fill=False, edgecolor='red', linewidth=2) ax.add_patch(rect) plt.show()
def label_stack(z_stack, parameters): ''' Apply an automated threshold and a watershed algorithm to label cells in each planes of the stack Parameters: ----------- z_stack: 3d array of shape (nz, nx, ny) Returns: -------- labeled_stack: 3d array of the same shape as z_stack, with each detected region labeled ''' segment_method = parameters['segment_method'] correction = parameters['correction'] labeled_stack = np.zeros(z_stack.shape, dtype=np.uint8) max_int_proj = z_stack.max(axis=0) thresh = None if segment_method == 'otsu': thresh = threshold_otsu(max_int_proj) * correction elif segment_method == 'naive': thresh = max_int_proj.max() * correction else: err_string = ('Segmentation method {}' 'is not implemented'.format(segment_method)) raise NotImplementedError(err_string) while thresh > z_stack.max(): log.warning('''Reducing threshold''') thresh *= 0.9 for n, frame in enumerate(z_stack): labeled_stack[n] = label_from_thresh(frame, thresh, parameters) return labeled_stack
def isolate_single_seed(stack_path, output_path): """Load stack then isolate seed.""" sigma = 10 iterations = 1 raw_stack = Image3D.from_path(stack_path) print raw_stack.shape smoothed = gaussian_filter(raw_stack, sigma).view(Image3D) edges = sobel_magnitude_nd(smoothed).view(Image3D) #edges.save('edges') labels = np.zeros(raw_stack.shape) cx, cy, cz = map(lambda x: x/2, raw_stack.shape) labels[cx, cy, cz] = 1 threshold = threshold_otsu(smoothed) thresholded = smoothed > threshold #thresholded.view(Image3D).save('thresh') segmentation = watershed(edges, markers=labels, mask=thresholded) #segmentation.view(Image3D).save('seg') dilated = binary_dilation(segmentation, iterations=iterations) isolate = np.multiply(raw_stack, dilated) isolate.view(Image3D).save(output_path)
def image_prep(img): # apply filters for better contrast and noise removal img_gamma = adjust_gamma(img, 0.7) img_median = median(img_gamma, disk(1)) # apply threshold val = threshold_otsu(img_median) img_otsu = img_median > val # label image regions label_image = label(img_otsu) candidates = [] for region in regionprops(label_image): minr, minc, maxr, maxc = region.bbox if (maxr - minr > maxc - minc): candidates.append(region) # find numbers areas = [] for candidate in candidates: areas.append(candidate.area) areas.sort() areas.reverse() n = 1 v = [] for candidate in candidates: if (candidate.area == areas[0] or candidate.area == areas[1] or candidate.area == areas[2]): v.append(candidate.image) imsave('num%d.png' % n, candidate.image) n += 1 return v
def save_img(img, filename, shape): img = deprocess_img(img) img = img.numpy() img *= 255 img = img.clip(0, 255) img = np.transpose(img, (1, 2, 0)) img = imresize(img, (shape[0], shape[1], 1)) img=rgb2gray(img) thresh = threshold_otsu(img) img= img > thresh img = img.astype(np.uint8) img=img*255 kernel = np.ones((3,3),np.uint8) img = cv2.erode(img,kernel,iterations = 1) # png.from_array(img[:], 'L').save(filename) # filename=filename.split('.jpg')[0] # filename = filename + '.png' imsave(filename, img) # imshow(img) print ("Image saved as {}".format(filename))
def max_mask_iter(fns, offset=0, close_radius=0, erode_radius=0): """Find masks for a set of images having brightness artifacts. Parameters ---------- fns : list of string The images being examined. offset : int, optional Offset the threshold automatically found. close_radius : int, optional Perform a morphological closing of the mask of this radius. erode_radius : int, optional Perform a morphological erosion of the mask, after any closing, of this radius. Returns ------- maxes : iterator of bool array The max mask image corresponding to each input image. """ ms = maxes(fns) t = imfilter.threshold_otsu(ms) ims = it.imap(io.imread, fns) masks = ((im < t + offset) for im in ims) if close_radius > 0: masks = (morphop(mask, 'close', close_radius) for mask in masks) if erode_radius > 0: masks = (morphop(mask, 'erode', erode_radius) for mask in masks) return masks
def create_binary(img): """ Create global binary """ global_thresh = threshold_otsu(img) binary_global = img > global_thresh return(binary_global)
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 gen(nb): X_out = np.empty((nb, c, w, h)) for i in range(nb): while True: img = np.random.choice(collection) ch = min(img.shape[0], h_crop) cw = min(img.shape[1], w_crop) crop_pos_y = np.random.randint(0, img.shape[0] - ch + 1) crop_pos_x = np.random.randint(0, img.shape[1] - cw + 1) x = crop_pos_x y = crop_pos_y im = img[y:y+ch, x:x+cw] im = im / 255. im = 1 - im #im = pad(im, 15, 'constant', constant_values=(0, 0)) im = resize(im, (w, h)) thresh = threshold_otsu(im) im = im > thresh im = im.astype(np.float32) if im.sum() > 500 and im.sum() < 2000: break X_out[i, 0] = im X_out = X_out.reshape((X_out.shape[0], -1)) X_out = X_out.astype(np.float32) return X_out
def testSkimage2(): img = Image.open('../img/1.png') # img = Image.open('../img/2.png') img = np.array(img) imggray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # (thresh, imgbw) = cv2.threshold(imggray, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU) camera = imggray # camera = data.camera() val = filters.threshold_otsu(camera) hist, bins_center = exposure.histogram(camera) plt.figure(figsize=(9, 4)) plt.subplot(131) plt.imshow(camera, cmap='gray', interpolation='nearest') plt.axis('off') plt.subplot(132) plt.imshow(camera < val, cmap='gray', interpolation='nearest') plt.axis('off') plt.subplot(133) plt.plot(bins_center, hist, lw=2) plt.axvline(val, color='k', ls='--') plt.tight_layout() plt.show() return
def basicProcessing(volume, sigma, order, output, mode, truncate): """This function shows an example of processing a volume. Parameters ---------- volume : array Array in which different processing will be applied. sigma : int or sequence of int Standard deviation for Gaussian kernel. order : int or sequence of int An order of 0 corresponds to convolution with a Gaussian kernel. An order of 1, 2, or 3 corresponds to convolution with the first, second or third derivatives of a Gaussian. Higher order derivatives are not implemented. output : array or dtype The array in which to place the output, or the dtype of the returned array. By default an array of the same dtype as input will be created. mode : str The mode parameter determines how the input array is extended when the filter overlaps a border. truncate : float Truncate the filter at this many standard deviations. Default is 4.0. """ #### Filters ### result = gaussian_filter(input=volume, sigma=sigma, order=order, output=output, mode=mode, truncate=truncate) val = threshold_otsu(result) print("val : {}".format(val)) mask = np.zeros(volume.shape, dtype=np.int8) mask[volume > val] = 1 #mask = mask.astype(int) print("mask shape: {}".format(mask.shape)) print(mask) #### Morphological Operation ### # Opening removes small objects r1 = binary_opening(mask, structure=np.ones((3, 3, 3))).astype(np.int8) # Closing removes small holes r2 = binary_closing(r1, structure=np.ones((3, 3, 3))).astype(np.int8) # 3x3x3 structuring element with connectivity 4 or 8 struct1 = generate_binary_structure(3, 1) # no diagonal elements #struct1 = generate_binary_structure(3, 2) # with diagonal elements ############struct1 = struct1.astype(int) print (struct1) #r3 = binary_dilation(r2).astype(int) r3 = binary_dilation(r2, structure=struct1).astype(int) # using a structure element # Erosion removes objects smaller than the structure r4 = binary_erosion(r3, structure=np.ones((3, 3, 3))).astype(np.int8) #### Measurements ### struct2 = np.ones((3, 3, 3), dtype=np.int8) labeled_array, num_features = label(r4, structure=struct2) #print(labeled_array) print(num_features) return labeled_array, num_features
def otsu(image_array_loss1,image_array_loss2, H, W,date1,date2,parameters,paths,loss_arrays,loss_arrays_otsu,changes=None): patch_size = parameters.get("patch_size") epoch_nb = parameters.get("epoch_nb") batch_size = parameters.get("batch_size") learning_rate = parameters.get("learning_rate") weighted = parameters.get("weighted") sigma = parameters.get("sigma") shuffle = parameters.get("shuffle") sampleTrue = parameters.get("sampleTrue") threshold = parameters.get("threshold") patient = paths.get("patient") path_results = paths.get("path_results") path_models = paths.get("path_models") path_models_finetuned = paths.get("path_models_finetuned") path_diff = paths.get("path_diff") path_bcm = paths.get("path_bcm") path_loss = paths.get("path_loss") path_reconstructed = paths.get("path_reconstructed") # We calculate the average reconstruction error image image_array_loss = np.divide((image_array_loss1+image_array_loss2), 2) #normalize loss for visual image_array_loss = normalize(image_array_loss)*255 # for loss and diff comparaison loss_arrays.append(image_array_loss) # We rescale the image values to 8 bits so it works with the functions from skimage # max_ = np.nanmax(image_array_loss) # coef = max_/256 # image_array_loss = image_array_loss/coef # image_array_loss = np.asarray(image_array_loss, dtype=int) # THIS IS VERY IMPORTANT VALUE # Otsu threshold is automatic, however before applying it, we exclude 0.5% of the highest reconstruction error values as they ae considered to be outliers # This parameter can be modified if needed # threshold = 1 # threshold = 0.995 # image_array_loss[~np.isnan(image_array_loss)].flatten() car on fait le threshold sans les nan a=image_array_loss[~np.isnan(image_array_loss)].flatten() val = filters.threshold_otsu(np.sort(a)[0:int(a.shape[0]*threshold)]) # Obtained threshold value del a # We get binary change map (1 - changes, 0 - no changes) using the threshold and write it to tiff and shp image_array_outliers = np.zeros(H*W) image_array_outliers[image_array_loss.flatten() > val] = 1 outliers_image_mean = path_bcm+"BCM_"+date1+'_to_'+date2+"_threshold_" +str(threshold)+"_otsu_"+str(val)+'.png' image_array_outliers = np.reshape(image_array_outliers, (H, W)) image_array_outliers=image_array_outliers.astype('uint8') image_array_outliers=image_array_outliers*255 # image_gray = cv2.cvtColor(image_array_outliers, cv2.COLOR_BGR2GRAY) cv2.imwrite(outliers_image_mean,image_array_outliers) loss_arrays_otsu.append([image_array_outliers,val]) # We calculate the stats if the ground truth is available for this couple of images # if changes is not None: # # path of ground truth image, I have only 2 GT # path_cm = '/home/user/Dropbox/IJCNN/images/' + changes # cm_truth_name = "mask_changes_small1" # if changes=="changes_2004_2005": # cm_predicted = (np.reshape(image_array_outliers, (H, W))[0:600, 600:1400]).flatten() # if changes == "changes_2006_2008": # cm_predicted = (np.reshape(image_array_outliers, (H, W))[100:370, 1000:1320]).flatten() # cm_truth, _, _, _, _, _ = open_tiff(path_cm, cm_truth_name) # cm_truth = cm_truth.flatten() # cm_truth[cm_truth==255]=0 # #Different stats takenthreshold = 0.995 from scikit # print(classification_report(cm_truth, cm_predicted, target_names=["no changes", "changes"])) # print(accuracy_score(cm_truth, cm_predicted)) # print(cohen_kappa_score(cm_truth, cm_predicted)) # conf = confusion_matrix(cm_truth, cm_predicted) # print(confusion_matrix(cm_truth, cm_predicted)) # omission = conf[1][0]/sum(conf[1]) # print (omission) return image_array_outliers
from skimage.transform import resize from PIL import Image car_image = imread("car6.jpg", as_grey=True) # it should be a 2 dimensional array print(car_image.shape) # the next line is not compulsory however, a grey scale pixel # in skimage ranges between 0 & 1. multiplying it with 255 # will make it range between 0 & 255 (something we can relate better with gray_car_image = car_image * 255 fig, (ax1, ax2) = plt.subplots(1, 2) ax1.imshow(gray_car_image, cmap="gray") threshold_value = threshold_otsu(gray_car_image) binary_car_image = gray_car_image > threshold_value ax2.imshow(binary_car_image, cmap="gray") plt.show() label_image = measure.label(binary_car_image) # getting the maximum width, height and minimum width and height that a license plate can be plate_dimensions = (0.06*label_image.shape[0], 0.2*label_image.shape[0], 0.15*label_image.shape[1], 0.4*label_image.shape[1]) min_height, max_height, min_width, max_width = plate_dimensions plate_objects_cordinates = [] plate_like_objects = [] fig, (ax1) = plt.subplots(1) ax1.imshow(gray_car_image, cmap="gray"); # regionprops creates a list of properties of all the labelled regions
from skimage.filters import threshold_otsu from skimage.color import rgb2gray from matplotlib import pyplot as plt chess_pieces = plt.imread('images/bw.jpg') chess_pieces_gray = rgb2gray(chess_pieces) thresh = threshold_otsu(chess_pieces_gray) binary = chess_pieces_gray > thresh plt.imshow(chess_pieces, cmap="gray") plt.show() plt.imshow(chess_pieces_gray, cmap="gray") plt.show() plt.imshow(binary, cmap="gray") plt.show()
path_list=['plantA_39.jpg','plantB_26.jpg','plantC_15.jpg',\ 'plantA_23.jpg','plantB_51.jpg','plantC_44.jpg'] for i in path_list: im = imageio.imread(i) img = color.rgb2gray(im) #grayscale plt.imshow(img, cmap=plt.cm.gray, interpolation='nearest') plt.axis('off') plt.savefig('(a)%s_grayscale.png'%i) #B&W threshold = filters.threshold_otsu(img) binary_img = img > threshold plt.imshow(binary_img, cmap=plt.cm.gray, interpolation='nearest') plt.axis('off') plt.savefig('(b)%s_b&w.png'%i) #edges edges = feature.canny(img, sigma=1) plt.imshow(edges, cmap=plt.cm.gray, interpolation='nearest') plt.axis('off') plt.savefig('(c)%s_edges.png'%i) #contours threshold = filters.threshold_otsu(img)
def find_somas(volume: np.ndarray, res: list) -> Tuple[int, np.ndarray, np.ndarray]: r"""Find bright neuron somas in an input volume. This simple soma detector assumes that somas are brighter than the rest of the objects contained in the input volume. To detect somas, these steps are performed: #. **Check input volume shape.** This detector requires the `x` and `y` dimensions of the input volumes to be larger than `20` pixels. #. **Zoom volume.** We found that this simple soma detector works best when then input volume has size `160 x 160 x 50`. We use `ndimage.zoom <https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.zoom.html>`_ to scale the input volume size to the desired shape. #. **Binarize volume.** We use `Otsu thresholding <https://scikit-image.org/docs/dev/api/skimage.filters.html#skimage.filters.threshold_otsu>`_ to binarize the image. #. **Erode the binarized image.** We erode the binarized image with a structuring element which size is directly proportional to the maximum zoom factor applied to the input volume. #. **Remove unreasonable connected components.** After erosion, we compute the equivalent diameter `d` of each connected component, and only keep those ones such that `5\mu m \leq d < 21 \mu m` #. **Find relative centroids.** Finally, we compute the centroids of the remaining connected components. The centroids are in voxel units, relative to the input volume. Parameters ---------- volume : numpy.ndarray The 3D image array to run the detector on. res : list A `1 x 3` list containing the resolution of each voxel in `nm`. Returns ------- label : bool A boolean value indicating whether the detector found any somas in the input volume. rel_centroids : numpy.ndarray A `N x 3` array containing the relative voxel positions of the detected somas. out : numpy.ndarray A `160 x 160 x 50` array containing the detection mask. Examples -------- >>> # download a volume >>> dir = "s3://open-neurodata/brainlit/brain1" >>> dir_segments = "s3://open-neurodata/brainlit/brain1_segments" >>> volume_keys = "4807349.0_3827990.0_2922565.75_4907349.0_3927990.0_3022565.75" >>> mip = 1 >>> ngl_sess = NeuroglancerSession( >>> mip=mip, url=dir, url_segments=dir_segments, use_https=False >>> ) >>> res = ngl_sess.cv_segments.scales[ngl_sess.mip]["resolution"] >>> volume_coords = np.array(os.path.basename(volume_keys).split("_")).astype(float) >>> volume_vox_min = np.round(np.divide(volume_coords[:3], res)).astype(int) >>> volume_vox_max = np.round(np.divide(volume_coords[3:], res)).astype(int) >>> bbox = Bbox(volume_vox_min, volume_vox_max) >>> img = ngl_sess.pull_bounds_img(bbox) >>> # apply soma detector >>> label, rel_centroids, out = find_somas(img, res) """ check_type(volume, np.ndarray) check_iterable_type(volume.flatten(), np.uint16) volume_dim = volume.ndim if volume_dim != 3: raise ValueError("Input volume must be three-dimensional") if volume.shape[0] < 20 or volume.shape[1] < 20: raise ValueError("Input volume is too small") check_type(res, list) check_iterable_type(res, (int, float)) if len(res) != 3: raise ValueError("Resolution must be three-dimensional") if np.any([el == 0 for el in res]): raise ValueError("Resolution must be non-zero at every position") desired_size = np.array([160, 160, 50]) zoom_factors = np.divide(desired_size, volume.shape) res = np.divide(res, zoom_factors) out = ndimage.zoom(volume, zoom=zoom_factors) out = out / np.max(out.flatten()) # 1) binarize volume using Otsu's method t = filters.threshold_otsu(out) out = out > t # 2) erode with structuring element proportional to zoom factors selem_size = np.amax(np.ceil(zoom_factors)).astype(int) clean_selem = morphology.octahedron(selem_size) out = morphology.erosion(out, clean_selem) # 3) identify connected components out, num_labels = morphology.label(out, background=0, return_num=True) # 4) remove connected components with diameter not in reasonable range, find centroids of candidate regions properties = ["label", "equivalent_diameter"] props = measure.regionprops_table(out, properties=properties) df_props = pd.DataFrame(props) rel_centroids = [] for _, row in df_props.iterrows(): l = row["label"] d = row["equivalent_diameter"] dmu = d * np.mean(res[:1]) / 1000 if dmu < 5 or dmu >= 21: out[out == l] = 0 num_labels -= 1 else: ids = np.where(out == l) centroid = np.round([np.median(u) for u in ids]).astype(int) centroid = np.divide(centroid, zoom_factors) rel_centroids.append(centroid) return num_labels > 0, np.array(rel_centroids), out
plt.hist(red_channel.ravel(), bins= 256) # Set title and show plt.title('Red Histogram') plt.show() -------------------------------------------------- # Exercise_4 # Import the otsu threshold function from skimage.filters import threshold_otsu # Make the image grayscale using rgb2gray chess_pieces_image_gray = rgb2gray(chess_pieces_image) # Obtain the optimal threshold value with otsu thresh = threshold_otsu(chess_pieces_image_gray) # Apply thresholding to the image binary = chess_pieces_image_gray > thresh # Show the image show_image(binary, 'Binary image') -------------------------------------------------- # Exercise_5 #1 # Import the otsu threshold function from skimage.filters import threshold_otsu # Obtain the optimal otsu global thresh value global_thresh = threshold_otsu(page_image)
import matplotlib.pyplot as plt from skimage import filters from skimage import exposure import cv2 import numpy as np from skimage.exposure import histogram img = cv2.imread('dew on roses.tif', 0) val = filters.threshold_otsu(img) #mult = skimage.filters.threshold_multiotsu print(val) ''' hist, bins_center = exposure.histogram(img) plt.figure(figsize=(9, 4)) plt.subplot(131) plt.imshow(img, cmap='gray', interpolation='nearest') plt.axis('off') # otsu threshold plt.subplot(132) plt.imshow(img < val, cmap='gray', interpolation='nearest') plt.axis('off') # histogram plt.subplot(133) plt.plot(bins_center, hist, lw=2) plt.axvline(val, color='k', ls='--') plt.tight_layout() plt.show()
def main(_): ground_truth = get_ground_truth(images_dir=FLAGS.images_dir, subdir_name=FLAGS.subdir_name, target_dir=None) contours = overlay_contours(images_dir=FLAGS.images_dir, subdir_name=FLAGS.subdir_name, target_dir=None) masks = overlay_masks(images_dir=FLAGS.images_dir, subdir_name=FLAGS.subdir_name, target_dir=None) ############################ # Problem 1 -> dirty masks ############################ idx = 5 dirty = masks[idx], contours[idx], ground_truth[idx] plot_list(images=[dirty[0], dirty[1]], labels=[dirty[2]]) ################################# # Problem 2 -> dirty at border ################################# idx = 44 dirty_at_border = masks[idx], contours[idx], ground_truth[idx] plot_list(images=[dirty_at_border[0], dirty_at_border[1]], labels=[dirty_at_border[2]]) ################ # Approach V1 ################ m, c, t = dirty ######################################################################## # Let's put it all together in a function - def clean_mask_v1(m,c) # # m, c, t = dirty # m_ = clean_mask_v1(m,c) # plot_list(images = [m,c,m_], labels = [t]) # ######################################################################## # let's proceed to cleaning. # First we binarize both the mask and contours using global, otsu thresholding method: m_b = m > threshold_otsu(m) c_b = c > threshold_otsu(c) plot_list(images=[m_b, c_b], labels=[]) # combine binarized masks and contours m_ = np.where(m_b | c_b, 1, 0) plot_list(images=[m_], labels=[]) # fill the holes that remained m_ = ndi.binary_fill_holes(m_) plot_list(images=[m_], labels=[]) # Now that we filled the holes in the cells we can detach them again because we have the contour information m_ = np.where(c_b & (~m_b), 0, m_) plot_list(images=[m_], labels=[]) # We are left with artifacts. Let's use binary_openning to drop them. area, radius = mean_blob_size(m_b) m_ = morph.binary_opening(m_, selem=morph.disk(0.25 * radius)) plot_list(images=[m_], labels=[]) # It works to a certain extend but it removes things that # where not connected and/or things around borders ################ # Approach V2 ################ # Let's start by binarizing and filling the holes again m, c, t = dirty_at_border ######################################################################## # Let's put it all together in one function - def clean_mask_v2(m,c) # # m,c,t = dirty_at_border # m_ = clean_mask_v2(m,c) # # plot_list(images = [m,c,m_], labels = [t]) # ######################################################################## # threshold m_b = m > threshold_otsu(m) c_b = c > threshold_otsu(c) # combine contours and masks and fill the cells m_ = np.where(m_b | c_b, 1, 0) m_ = ndi.binary_fill_holes(m_) plot_list(images=[m_], labels=[]) # Now we will use binary_closing to fill what wasn't closed with fill holes. # We will need two helper functions pad_mask and crop_mask to deal with problems around the edges # close what wasn't closed before area, radius = mean_blob_size(m_b) struct_size = int(1.25 * radius) struct_el = morph.disk(struct_size) m_padded = pad_mask(m_, pad=struct_size) m_padded = morph.binary_closing(m_padded, selem=struct_el) m_ = crop_mask(m_padded, crop=struct_size) plot_list(images=[m_], labels=[]) # we closed everything but it is way more than we wanted. # Let's now cut it with our contours and see what we get m_ = np.where(c_b & (~m_b), 0, m_) plot_list(images=[m_], labels=[]) # we can use binary_openning with a larger structural element. Let's try that area, radius = mean_blob_size(m_b) struct_size = int(0.75 * radius) struct_el = morph.disk(struct_size) m_padded = pad_mask(m_, pad=struct_size) m_padded = morph.binary_opening(m_padded, selem=struct_el) m_ = crop_mask(m_padded, crop=struct_size) plot_list(images=[m_, m], labels=[t]) # join the connected cells with what we had at the beginning m_ = np.where(m_b | m_, 1, 0) m_ = ndi.binary_fill_holes(m_) plot_list(images=[m_, m], labels=[t]) m_ = drop_artifacts(m_, m_b, min_coverage=0.25) plot_list(images=[m_, m, c], labels=[t]) ############################################ # Problem 3 -> not everything gets filled ############################################ ############################################ # Problem 4 -> some cells get connected # # Ideas: # - work more with dilation # - do better around borders # - drop some cells after watershed with drop_artifact function # # TODO: clean_mask_V3 would be dev... # Go ahead and try to improve it. The floor is yours # # def clean_mask_v3(m,c): # return # ############################################ ################### # Good Markers ################### # In this approach we will simply cut the masks with the contours and use that as markers. # Simple but really effective. for idx in range(5): print(idx) mask = masks[idx] contour = contours[idx] cleaned_mask = clean_mask_v2(mask, contour) good_markers = good_markers_v1(mask, contour) gt = ground_truth[idx] plot_list(images=[mask, contour, cleaned_mask, good_markers], labels=[gt]) # Problem 1 -> building markers on initial mask when we have better mask for idx in range(5): print(idx) mask = masks[idx] contour = contours[idx] cleaned_mask = clean_mask_v2(mask, contour) good_markers = good_markers_v2(cleaned_mask, contour) gt = ground_truth[idx] plot_list(images=[mask, contour, cleaned_mask, good_markers], labels=[gt]) # Problem 2 some markers are to large and connected m, c, t = dirty cleaned_mask = clean_mask_v2(m, c) c_b = c > threshold_otsu(c) mk_ = np.where(c_b, 0, cleaned_mask) plot_list(images=[m, c, mk_], labels=[]) # For instance the two markers at the top left are still connected and will be treated # as a single marker by the watershed And nowe lets erode the markers area, radius = mean_blob_size(m_b) struct_size = int(0.25 * radius) struct_el = morph.disk(struct_size) m_padded = pad_mask(mk_, pad=struct_size) m_padded = morph.erosion(m_padded, selem=struct_el) mk_ = crop_mask(m_padded, crop=struct_size) plot_list(images=[m, c, mk_], labels=[]) # we now compare those markers with the labels we get the following mk_, _ = ndi.label(mk_) plot_list(images=[cleaned_mask], labels=[mk_, t]) ######################################################### # So the markers and cleaned mask look really good! ######################################################### for idx in range(5): print(idx) mask = masks[idx] contour = contours[idx] cleaned_mask = clean_mask_v2(mask, contour) good_markers = good_markers_v3(cleaned_mask, contour) gt = ground_truth[idx] plot_list(images=[mask, contour, cleaned_mask], labels=[good_markers, gt]) # Problem 3 -> still some barely connected markers are left¶ for idx in [25, 27]: print(idx) mask = masks[idx] contour = contours[idx] cleaned_mask = clean_mask_v2(mask, contour) good_markers = good_markers_v3(cleaned_mask, contour) gt = ground_truth[idx] plot_list(images=[mask, contour, cleaned_mask], labels=[good_markers, gt]) ######################################################################### # Problem 4 -> we are dropping markers on many images with small cells # # Ideas # - play with binary closing/opening # - involve contours and/or centers in this # - we will asume that lost markers are in facet small cells that don't need to be divided and # we will get back all the cells that were dropped in watershed # - use local maxima on distance transform # # TODO: good_markers_v4 need to be dev... # def good_markers_v4(m_b,c): # return # ######################################################################### ##################### # Good distance ##################### # Here I have no better idea than to use the binary distance from the background. # Feel free to improve on that! # # Idea # - investigate imposing some gradients on original image or good clean mask # for idx in range(5): print(idx) mask = masks[idx] contour = contours[idx] cleaned_mask = clean_mask_v2(mask, contour) good_markers = good_markers_v3(cleaned_mask, contour) good_distance = good_distance_v1(cleaned_mask) gt = ground_truth[idx] plot_list(images=[cleaned_mask, good_distance], labels=[good_markers, gt]) ######################## # Watershed ######################## for idx in range(5): print(idx) mask = masks[idx] contour = contours[idx] cleaned_mask = clean_mask_v2(mask, contour) good_markers = good_markers_v3(cleaned_mask, contour) good_distance = good_distance_v1(cleaned_mask) water = watershed_v1(mask, contour) gt = ground_truth[idx] plot_list(images=[cleaned_mask, good_distance], labels=[good_markers, water, gt]) # Problem 1 -> some cells are dumped # Problem 2 -> some artifacts from mask_cleaning remain # Unfortunatelly some cells are dropped, some cells are oversegmented and # some artifacts from the mask cleaning still remain. # The good thing is we can deal with some of those problems by using ideas we have already tried. for idx in range(5): print(idx) mask = masks[idx] contour = contours[idx] cleaned_mask = clean_mask_v2(mask, contour) good_markers = good_markers_v3(cleaned_mask, contour) good_distance = good_distance_v1(cleaned_mask) water = watershed_v2(mask, contour) gt = ground_truth[idx] plot_list(images=[cleaned_mask, good_distance], labels=[good_markers, water, gt]) # Problem 3 -> some cells are oversemgmented and small cell chunks remain # Now some small pieces of cells may remain at this point or the cells could get oversegmented. # We will deal with that by dropping to small to be a cell blobs. for idx in range(5): print(idx) mask = masks[idx] contour = contours[idx] cleaned_mask = clean_mask_v2(mask, contour) good_markers = good_markers_v3(cleaned_mask, contour) good_distance = good_distance_v1(cleaned_mask) water = watershed_v3(mask, contour) gt = ground_truth[idx] plot_list(images=[cleaned_mask, good_distance], labels=[good_markers, water, gt]) idx = 0 train_dir = os.path.join(FLAGS.images_dir, FLAGS.subdir_name) for filename in tqdm(glob.glob('{}/*'.format(train_dir))): imagename = filename.split("/")[-1] mask = masks[idx] contour = contours[idx] water = watershed_v3(mask, contour) img = Image.fromarray(water.astype('uint8')) water_path = (filename + '/water/') if not os.path.exists(water_path): os.makedirs(water_path) img.save(os.path.join(water_path, imagename + '.png')) ''' cleaned_mask = clean_mask_v2(mask, contour) img = Image.fromarray(cleaned_mask.astype('uint8')) cleaned_mask_path = (filename + '/cleaned_mask/') if not os.path.exists(cleaned_mask_path): os.makedirs(cleaned_mask_path) img.save(os.path.join(cleaned_mask_path, imagename + '.png')) good_markers = good_markers_v3(cleaned_mask, contour) img = Image.fromarray(good_markers) good_markers_path = (filename + '/good_markers/') if not os.path.exists(good_markers_path): os.makedirs(good_markers_path) img.save(os.path.join(good_markers_path, imagename + '.png')) good_distance = good_distance_v1(cleaned_mask) img = Image.fromarray(good_distance.astype('uint8')) good_distance_path = (filename + '/good_distance/') if not os.path.exists(good_distance_path): os.makedirs(good_distance_path) img.save(os.path.join(good_distance_path, imagename + '.png')) ''' idx = idx + 1
# pic = erosion(pic, disk(1)) # thresh = threshold_otsu(pic)*1.0 thresh = 0.5 * pic.max() #0.95 * pic.max() mask = pic h = thresh seed = pic - h dilated = reconstruction(seed, mask, method='dilation') pic = pic - dilated if show_plots: plt.imshow(pic) plt.show() thresh = threshold_otsu(pic) bin = pic > thresh # bin = pic > np.max(pic)/2 if do_erosion[f]: pic = erosion(pic, disk(5)) pic = erosion(pic, disk(3)) pic = erosion(pic, disk(1)) if show_plots: plt.imshow(bin) plt.show() fig = plt.figure() plt.imshow(bin) plt.savefig(savedir + file + '.png', dpi=600)
plt.imshow(img, cmap = 'gray', interpolation = 'none') for c in cts: plt.plot(c[:,0], c[:,1]) plt.xlim(0, 151); plt.ylim(0,151) plt.axis('off') fig.savefig('pipeline_contour.png', facecolor = 'white') #curvature import numpy as np from skimage.filters import threshold_otsu from scipy.interpolate import splev, splprep threshold_factor = 0.95 level = threshold_factor * threshold_otsu(img); pts = wgeo.detect_contour(img, level)[0]; ncontour = 100 smooth = 1.0 cinterp, u = splprep(pts.T, u = None, s = smooth, per = 1) us = np.linspace(u.min(), u.max(), ncontour) x, y = splev(us[:-1], cinterp, der = 0) dx, dy = splev(us[:-1], cinterp, der = 1) d2x, d2y = splev(us[:-1], cinterp, der = 2) k = (dx * d2y - dy * d2x)/np.power(dx**2 + dy**2, 1.5); from signalprocessing.peak_detection import find_peaks nextra = 20; delta = 0.3
def otsu(image): assert image.ndim == 2 return image >= threshold_otsu(image)
def register_bands(image, template_band=1, ECC_criterion=True): """ Fix chromatic abberation in images by calculating and applying an affine transformation. Chromatic abberation is a result of uneven refraction of light of different wavelengths. It shows up as systematic blue and red edges in high-contrast areas. This should be done before other processing to minimize artifacts. Parameters ---------- image : ndarray 3- or 4-channel image, probably RGB. template : int Band to which to align the other bands. Usually G in RGB. ECC_criterion : bool Use ECC criterion to find optimal warp? Improves results, but increases processing time 5x. Returns ------- An ndarray of `image.size` Notes ----- Uses `skimage.filters.scharr` to find edges in each band, then finds and applies an affine transformation to register the images using `cv2.estimateRigidTransform` and `cv2.warpAffine`. If `ECC_criterion=True`, the matrix from `estimateRigidTransform` is updated using `cv2.findTransformECC`. """ #find dimensions height, width, depth = image.shape #define bands to analyze analyze = [] for i in range(depth): if i != template_band: analyze.append(i) # Extract bands, find edges bands = img_split(image) edges = [np.ones_like(i) for i in bands] for i in range(len(bands)): sigma_val = 0.25 edge_val = 1 while edge_val > 0.1 and sigma_val < 10: temp = filters.gaussian(bands[i], sigma=sigma_val) scharr = filters.scharr(temp) temp = scharr > filters.threshold_otsu(scharr) edge_val = np.sum(temp) / np.sum(np.ones_like(temp)) sigma_val = 2 * sigma_val edges[i] = img_as_ubyte(scharr * temp) #make output image out = np.zeros((height, width, depth), dtype=np.uint8) out[:, :, template_band] = bands[template_band] try: for i in analyze: # Estimate transformation warp_matrix = np.array(cv2.estimateRigidTransform( edges[template_band], edges[i], fullAffine=False), dtype=np.float32) if ECC_criterion == True: # Optimize using ECC criterion and default settings warp_matrix = cv2.findTransformECC(edges[template_band], edges[i], warpMatrix=warp_matrix)[1] # transform aligned = cv2.warpAffine( bands[i], warp_matrix, (width, height), flags=cv2.INTER_LINEAR + cv2. WARP_INVERSE_MAP, # otherwise the transformation goes the wrong way borderMode=cv2.BORDER_CONSTANT) # add to color image out[:, :, i] = aligned except: # Probably few objects, so no smoothing and no thresholding to have as much info as possible edges = [img_as_ubyte(filters.scharr(i)) for i in edges] for i in analyze: # Estimate transformation warp_matrix = np.array(cv2.estimateRigidTransform( edges[template_band], edges[i], fullAffine=False), dtype=np.float32) if ECC_criterion == True: # Optimize using ECC criterion and default settings warp_matrix = cv2.findTransformECC(edges[template_band], edges[i], warpMatrix=warp_matrix)[1] # transform aligned = cv2.warpAffine( bands[i], warp_matrix, (width, height), flags=cv2.INTER_LINEAR + cv2. WARP_INVERSE_MAP, # otherwise the transformation goes the wrong way borderMode=cv2.BORDER_CONSTANT) # add to color image out[:, :, i] = aligned return (img_as_ubyte(out))
def Workflow_npm1_SR(struct_img, rescale_ratio, output_type, output_path, fn, output_func=None): ########################################################################## # PARAMETERS: # note that these parameters are supposed to be fixed for the structure # and work well accross different datasets intensity_norm_param = [0.05, 32] gaussian_smoothing_sigma = 2 gaussian_smoothing_truncate_range = 3.0 dot_2d_sigma = 3 dot_2d_sigma_extra = 1 dot_2d_cutoff = 0.025 minArea = 8 low_level_min_size = 3000 ########################################################################## out_img_list = [] out_name_list = [] ################### # PRE_PROCESSING ################### # intenisty normalization (min/max) struct_img = intensity_normalization(struct_img, scaling_param=intensity_norm_param) out_img_list.append(struct_img.copy()) out_name_list.append('im_norm') # rescale if needed if rescale_ratio > 0: struct_img = resize(struct_img, [1, rescale_ratio, rescale_ratio], method="cubic") struct_img = (struct_img - struct_img.min() + 1e-8) / (struct_img.max() - struct_img.min() + 1e-8) gaussian_smoothing_truncate_range = gaussian_smoothing_truncate_range * rescale_ratio # smoothing with gaussian filter structure_img_smooth = image_smoothing_gaussian_3d( struct_img, sigma=gaussian_smoothing_sigma, truncate_range=gaussian_smoothing_truncate_range) out_img_list.append(structure_img_smooth.copy()) out_name_list.append('im_smooth') ################### # core algorithm ################### # step 1: low level thresholding global_tri = threshold_triangle(structure_img_smooth) global_median = np.percentile(structure_img_smooth, 50) th_low_level = (global_tri + global_median) / 2 bw_low_level = structure_img_smooth > th_low_level bw_low_level = remove_small_objects(bw_low_level, min_size=low_level_min_size, connectivity=1, in_place=True) bw_low_level = dilation(bw_low_level, selem=ball(2)) # step 2: high level thresholding local_cutoff = 0.333 * threshold_otsu(structure_img_smooth) bw_high_level = np.zeros_like(bw_low_level) lab_low, num_obj = label(bw_low_level, return_num=True, connectivity=1) for idx in range(num_obj): single_obj = (lab_low == (idx + 1)) local_otsu = threshold_otsu(structure_img_smooth[single_obj]) if local_otsu > local_cutoff: bw_high_level[np.logical_and( structure_img_smooth > 0.98 * local_otsu, single_obj)] = 1 out_img_list.append(bw_high_level.copy()) out_name_list.append('bw_coarse') response_bright = dot_slice_by_slice(structure_img_smooth, log_sigma=dot_2d_sigma) response_dark = dot_slice_by_slice(1 - structure_img_smooth, log_sigma=dot_2d_sigma) response_dark_extra = dot_slice_by_slice(1 - structure_img_smooth, log_sigma=dot_2d_sigma_extra) holes = np.logical_or(response_dark > dot_2d_cutoff, response_dark_extra > dot_2d_cutoff) bw_extra = response_bright > dot_2d_cutoff bw_extra[~bw_low_level] = 0 bw_final = np.logical_or(bw_extra, bw_high_level) bw_final[holes] = 0 ################### # POST-PROCESSING ################### seg = remove_small_objects(bw_final, min_size=minArea, connectivity=1, in_place=True) # output seg = seg > 0 seg = seg.astype(np.uint8) seg[seg > 0] = 255 out_img_list.append(seg.copy()) out_name_list.append('bw_fine') if output_type == 'default': # the default final output save_segmentation(seg, False, output_path, fn) elif output_type == 'AICS_pipeline': # pre-defined output function for pipeline data save_segmentation(seg, True, output_path, fn) elif output_type == 'customize': # the hook for passing in a customized output function output_fun(out_img_list, out_name_list, output_path, fn) elif output_type == 'array': return seg elif output_type == 'array_with_contour': return (seg, generate_segmentation_contour(seg)) else: # the hook for pre-defined RnD output functions (AICS internal) img_list, name_list = NPM1_output(out_img_list, out_name_list, output_type, output_path, fn) if output_type == 'QCB': return img_list, name_list
# reading multiple images for the same wound at different times # calculating the healing rate and other stats from skimage import io from matplotlib import pyplot as plt import numpy as np from skimage.filters.rank import entropy from skimage.morphology import disk from skimage.filters import threshold_otsu import glob from scipy.stats import linregress time = 0 time_list = [] area_list = [] path = "ImageProcessing/Images/Wounds/*" for img_file in glob.glob(path): img = io.imread(img_file, as_gray=True) entropy_img = entropy(img, disk(40)) thresh = threshold_otsu(entropy_img) # auto thresholding method binary = entropy_img <= thresh scratch_area = np.sum(binary == True) print(time, scratch_area) time_list.append(time) area_list.append(scratch_area) time += 1 print(time_list, area_list) plt.plot(time_list, area_list, 'ro') print(linregress(time_list, area_list)) plt.show()
import matplotlib.pyplot as plt os.chdir('/home/sambeet/data/kaggle/carvana/') image_list = os.listdir('train_resized/') x = misc.imread('train_resized/' + image_list[0],flatten = True) hist = np.histogram(x, bins=np.arange(0, 256)) plt.plot(hist[1][:-1], hist[0], lw=2) z = filters.scharr((x/255.)) #z = filters.gaussian(z,1.) z = canny(z,1) z = ndi.binary_closing(z,structure=np.ones((3,3))) z = ndi.binary_fill_holes(z,np.ones((3,3))) io.imshow(z) z = filters.prewitt((x-127.5)/255.) z = filters.scharr((x-127.5)/255.) x = abs(x - 127.5)/127.5 threshold_li = filters.threshold_yen((x)) threshold_li = filters.threshold_otsu(x) threshold_li = filters.threshold_isodata(x) z = x > threshold_li io.imshow(z) x = misc.imread('train/' + image_list[0],flatten = True) x = abs(x - 127.5)/127.5 #x_rgb = misc.imread('train/' + image_list[0]) threshold_li = filters.try_all_threshold(z1) z = x > threshold_li io.imshow((z -.5)) z1 = filters.rank.autolevel((x-127.5)/127.5,disk(20))
def binarize(image): thresh = threshold_otsu(image) binary = image <= thresh return binary
# .. [4] http://en.wikipedia.org/wiki/Otsu's_method from skimage.filters.rank import otsu from skimage.filters import threshold_otsu p8 = data.page() radius = 10 selem = disk(radius) # t_loc_otsu is an image t_loc_otsu = otsu(p8, selem) loc_otsu = p8 >= t_loc_otsu # t_glob_otsu is a scalar t_glob_otsu = threshold_otsu(p8) glob_otsu = p8 >= t_glob_otsu fig, axes = plt.subplots(2, 2, sharex=True, sharey=True) ax = axes.ravel() fig.colorbar(ax1.imshow(p8, cmap=plt.cm.gray), ax=ax1) ax[0].set_title('Original') fig.colorbar(ax[1].imshow(t_loc_otsu, cmap=plt.cm.gray), ax=ax2) ax[1].set_title('Local Otsu ($r=%d$)' % radius) ax[2].imshow(p8 >= t_loc_otsu, cmap=plt.cm.gray) ax[2].set_title('Original >= local Otsu' % t_glob_otsu) ax[3].imshow(glob_otsu, cmap=plt.cm.gray)
mode='constant', preserve_range=True)) # This will be our final dataframe df = pd.DataFrame(columns=["ImageID", "EncodedPixels"]) j = 0 # RLE encode the test data for n, id_ in tqdm(enumerate(test_ids), total=len(test_ids)): image_id = id_ image_label = preds_test_upsampled[j] #CONVERT PROBABILISTIC PREDICTION TO BINARY from skimage.filters import threshold_otsu thresh_val = threshold_otsu(image_label) newmask = np.where(image_label > thresh_val, 1, 0) add_to_df = RLE_mask(newmask, image_id, df) df = df.append(add_to_df, ignore_index=True) j += 1 print(df) df.to_csv('CNNsubmission.csv', index=False) #See stuff image_label = preds_test_upsampled[0] #CONVERT PROBABILISTIC PREDICTION TO BINARY from skimage.filters import threshold_otsu thresh_val = threshold_otsu(image_label)
########################################################### #Entropy #Entropy quantifies disorder. #Since cell region has high variation in pixel values the entropy would be #higher compared to scratch region from skimage.filters.rank import entropy from skimage.morphology import disk entropy_img = entropy(img, disk(3)) plt.imshow(entropy_img) #Scratch Analysis - single image #Now let us use otsu to threshold high vs low entropy regions. plt.hist(entropy_img.flat, bins=100, range=(0, 5)) #.flat returns the flattened numpy array (1D) thresh = threshold_otsu(entropy_img) #Now let us binarize the entropy image binary = entropy_img <= thresh plt.imshow(binary) #Sum all pixels in the scratch region (values =1) scratch_area = np.sum(binary == 1) print("Scratched area is: ", scratch_area, "Square pixels") scale = 0.45 # microns/pixel print("Scratched area in sq. microns is: ", scratch_area * ((scale)**2), "Square pixels")
x=x_train, y=y_train, batch_size=BATCH_SIZE_GLOBAL, epochs=NUMERO_EPOCAS_GLOBAL, callbacks=callbacks, validation_data=(x_val, y_val), ) fim = time.time() tempo_processamento = fim-inicio print("TEMPO DE PROCESSAMENTO: ",tempo_processamento) ###### TESTA predicoes = model.predict(x_test) predicoes= (np.asarray(predicoes)> threshold_otsu(np.asarray(predicoes))) # calcular metricas do teste sensitivity,specificity,accuracy,auc,dice,jaccard = calc_metric(y_test,predicoes[:,:,:,0]) print("############## RESULTADO FINAL ##############") print("sensitivity:",sensitivity) print("specificity:",specificity) print("accuracy:",accuracy) print("auc:",auc) print("dice:",dice) print("jaccard:",jaccard) print(PASTA_DE_TESTES) # SALVAR RESULTADOS
#carrega a base de dados digitsdb = datasets.load_digits() #armazena as imagens e os rótulos em estruturas distintas digits = np.array(digitsdb.images, 'int16') targets = np.array(digitsdb.target) #inicializa vetores que irão armazenar os descritores d0 = [] d1 = [] #para cada imagem, extrai descritores for image in digits: #forma de binarização 1 thresh = threshold_otsu(image) binary = image > thresh #forma de binarização 2 #binary = image != 0 ## << Tire este comentário para fazer o exercício! label_img = label(binary) #se quiser ver as figuras #plt.figure(1, figsize=(3, 3)) #plt.imshow(image, cmap=plt.cm.gray_r, interpolation='nearest') #plt.show() regions = regionprops(label_img, coordinates='xy') for props in regions: d0.append(props.eccentricity)
# scaler = StandardScaler() scaler = MinMaxScaler() scaler.fit(X_train) X_train = scaler.transform(X_train) X_test = scaler.transform(X_test) knn = KNN(X_train, X_test, y_train, y_test) msv = SVM(X_train, X_test, y_train, y_test) for name in glob.glob('placas/*'): car_image = imread(name) gray = cv2.cvtColor(car_image, cv2.COLOR_BGR2GRAY) color = cv2.cvtColor(car_image, cv2.COLOR_BGR2RGB) threshold_value = threshold_otsu(gray) binary_car_image = gray > threshold_value # fig, (ax1, ax2) = plt.subplots(1, 2) # ax1.imshow(gray, cmap="gray") # ax2.imshow(binary_car_image, cmap="gray") # this gets all the connected regions and groups them together label_image = measure.label(binary_car_image) plate_dimensions = (0.06 * label_image.shape[0], 0.1 * label_image.shape[0], 0.06 * label_image.shape[1], 0.09 * label_image.shape[1]) min_height, max_height, min_width, max_width = plate_dimensions plate_objects_cordinates = []
def convertHatchedImageToSTL(imageurl): image = cv2.imread(imageurl) '''' # generate copy of original image im1 = image.copy() # decrease contrast to emboss lines ob = preprocess(im1) # decrease contrast to emboss lines im1 = ob.ChangeContrast(0.5) # blur to emboss edges im1 = ob.Blur() # define kernel for dilate kernel = np.ones((5, 5), np.uint8) # morphology operations for separating outer lines opening = cv2.morphologyEx(im1, cv2.MORPH_OPEN, kernel) # detect edges for dilation edges = cv2.Canny(opening, 100, 150) # img = np.array(image, dtype=np.uint8) gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Perform 'closing' morphological operation on the image kernel = np.ones((1, 1), np.uint8) gray_image = cv2.morphologyEx(gray_image, cv2.MORPH_CLOSE, kernel) # Figure out the scaling parameter according to original size and then scale scale = prop.get_scaling_factor() gray_image = cv2.resize(gray_image, (0, 0), fx=scale, fy=scale) # Normalize all pixels to assume values from 0 to 1 gray_image = gray_image / 255.0 gray_image = np.subtract(1.0, gray_image) # Find the threshold to separate foreground from background using OTSU's thresholding method threshold = threshold_otsu(gray_image) (rows, cols) = gray_image.shape ''' textons = Textons(image, 3, 10, 1) tex = textons.textons() show = np.zeros_like(image) colors = np.unique(tex) show[tex == colors[1]] = [0, 0, 255] show[tex == colors[2]] = [0, 255, 255] show = cv2.medianBlur(show, 5) cv2.imwrite('temp.jpg', show) distinct_colors = color_extractor.get_top_colors_hsv(show, 10) ui.assign_height_to_colors(distinct_colors, 'temp.jpg') hsv_image = color_converter.get_hsv_from_bgr_image(show) gray_image = convert_from_colored_to_gray(hsv_image, distinct_colors) gray_image = cv2.medianBlur(gray_image, 3) # Perform 'closing' morphological operation on the image kernel = np.ones((1, 1), np.uint8) gray_image = cv2.morphologyEx(gray_image, cv2.MORPH_CLOSE, kernel) # Figure out the scaling parameter according to original size and then scale scale = prop.get_scaling_factor() gray_image = cv2.resize(gray_image, (0, 0), fx=scale, fy=scale) # Find the threshold to separate foreground from background using OTSU's thresholding method threshold = threshold_otsu(gray_image) (rows, cols) = gray_image.shape ''' Create a 3D voxel data from the image The top-most (#1) and bottom-most (#13) layer will contain all zeros The middle 10 layers (#3 to #12) contain the same pixel values as the gray scale image There is an additional layer(#2) for the base of the model ''' layers = 13 rows += 2 cols += 2 voxel = np.zeros((rows, cols, layers)) voxel[:, :, 1] = np.ones((rows, cols)).astype('float32') # making the boundary voxel values to be zero, for the marching cubes algorithm to work correctly voxel[0, :, :] = np.zeros((cols, layers)).astype('float32') voxel[(rows - 1), :, :] = np.zeros((cols, layers)).astype('float32') voxel[:, 0, :] = np.zeros((rows, layers)).astype('float32') voxel[:, (cols - 1), :] = np.zeros((rows, layers)).astype('float32') ''' Create the middle 10 layers from the image Based on the pixel values the layers are created to assign different heights to different regions in the image ''' for level in range(1, 10): level_threshold = level * 0.1 for j in range(0, rows - 2): for k in range(0, cols - 2): pixel_value = gray_image[j][k] if pixel_value > level_threshold: voxel[j + 1][k + 1][level + 1] = pixel_value ''' Run the marching cubes algorithm to extract surface mesh from 3D volume. Params: volume : (M, N, P) array of doubles Input data volume to find isosurfaces. Will be cast to `np.float64`. level : float Contour value to search for isosurfaces in `volume`. If not given or None, the average of the min and max of vol is used. spacing : length-3 tuple of floats Voxel spacing in spatial dimensions corresponding to numpy array indexing dimensions (M, N, P) as in `volume`. gradient_direction : string Controls if the mesh was generated from an isosurface with gradient descent toward objects of interest (the default), or the opposite. The two options are: * descent : Object was greater than exterior * ascent : Exterior was greater than object ''' verts, faces, normals, values = measure.marching_cubes_lewiner( volume=voxel, level=threshold, spacing=(1., 1., 1.), gradient_direction='descent') # Export the mesh as stl mymesh = mesh.Mesh(np.zeros(faces.shape[0], dtype=mesh.Mesh.dtype)) for i, f in enumerate(faces): for j in range(3): mymesh.vectors[i][j] = verts[f[j], :] file_utils.save_mesh_to_file(imageurl, mymesh)
except: print("File doesn't exists - Terminating process") exit(0) # Load the car image with license plate car_img = imread("./output/frame%d.jpg" % (count - 1), as_gray=True) # Normalizing the pixles gray_img = car_img * 255 # Defining subplots fig, (ax1, ax2) = plt.subplots(1, 2) fig.suptitle('Processed Images') ax1.imshow(gray_img, cmap='gray') ax1.set_title('Gray Image') # Performing Otsu Thresholding (To separate foreground / background) # Read more : https://en.wikipedia.org/wiki/Otsu%27s_method threshold_val = threshold_otsu(gray_img) # Each pixel of binary image is True (1) if the corresponding pixel in # gray image has intensity value greater than thrrshold_val binary_img = gray_img > threshold_val ax2.imshow(binary_img, cmap='gray') ax2.set_title('Binary Image') plt.show() def predict(): license_plate = binary_img # license_plate = SegmentChars.DetectPlateImage.plate_objects[0] # For image processing plate = pytesseract.image_to_string(license_plate).strip( ' !@#$%^&*()_+{\\}\\[];/><,.-') print("License plate is :{}".format(plate)) print("\n\n\n")
def Base_Datos(): Momentos = [] Etiquetas = [] for name in glob.glob('test_train/*'): car_image = imread(name) gray = cv2.cvtColor(car_image, cv2.COLOR_BGR2GRAY) gray = cv2.GaussianBlur(gray, (1, 1), 0) color = cv2.cvtColor(car_image, cv2.COLOR_BGR2RGB) ret, binaria = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU) threshold_value = threshold_otsu(gray) binary_car_image = gray > threshold_value license_plate = np.invert(binary_car_image) labelled_plate = measure.label(license_plate) character_dimensions = (0.3 * license_plate.shape[0], 0.55 * license_plate.shape[0], 0.1 * license_plate.shape[1], 0.25 * license_plate.shape[1]) min_height, max_height, min_width, max_width = character_dimensions fig, ax1 = plt.subplots(1) ax1.imshow(license_plate, cmap="gray") characters = [] counter = 11 for regions in regionprops(labelled_plate): y0, x0, y1, x1 = regions.bbox y0 -= 1 region_height = y1 - y0 region_width = x1 - x0 if region_height > min_height and region_height < max_height and region_width > min_width and region_width < max_width: roi = binaria[y0:y1, x0:x1] rect_border = patches.Rectangle((x0, y0), x1 - x0, y1 - y0, edgecolor="red", linewidth=2, fill=False) ax1.add_patch(rect_border) # resize the characters to 20X20 and then append each character into the characters list resized_char = resize(roi, (20, 20)) characters.append(resized_char) Etiquetas.append(name[counter]) counter += 1 plt.show() for x in range(len(characters)): # Tradon = radon(characters[x], theta=np.arange(0,210,30), circle=True).flatten() Firma = characters[x].flatten() # Hu = cv2.HuMoments(cv2.moments(characters[x])).flatten() # Momentos.append(np.concatenate((Tradon, Firma,Hu), axis=None)) Momentos.append(Firma) if x == 0: fig, ax1 = plt.subplots(1) ax1.imshow(characters[x], cmap="binary_r") letters = [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' ] nums = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35 ] for letra in range(len(Etiquetas)): for x in range(len(letters)): if Etiquetas[letra] == letters[x]: Etiquetas[letra] = nums[x] X = np.array(Momentos) Y = np.array(Etiquetas) return X, Y
except IndexError: entrada = 'img_entrada.tif' try: saida = sys.argv[2] except IndexError: saida = 'img_saida.tif' # Faz a leitura da imagem img_entrada = misc.imread(entrada) # Converte os pixels em float, com valores entre 0 e 1 img_entrada = img_as_float(img_entrada.astype(np.uint8)) # Limiar de Otsu l_otsu = filters.threshold_otsu(img_entrada) # Segmenta a imagem por limiarização img_saida = img_entrada < l_otsu # Faz o salvamento das imagens de saída após o processamento misc.imsave(saida, img_saida.astype(np.uint8)) # Organiza o plote das imagens plt.figure() plt.subplot(221) plt.imshow(img_entrada, cmap='gray', interpolation='nearest') plt.title('img_entrada') plt.subplot(222) plt.imshow(img_saida, cmap='gray', interpolation='nearest') plt.title('img_saida')
def find_patches_from_slide(slide_path, filter_non_tissue=True): """Returns a dataframe of all patches in slide input: slide_path: path to WSI file output: samples: dataframe with the following columns: slide_path: path of slide is_tissue: sample contains tissue is_tumor: truth status of sample tile_loc: coordinates of samples in slide option: base_truth_dir: directory of truth slides option: filter_non_tissue: Remove samples no tissue detected """ #sampletotal = pd.DataFrame([]) #base_truth_dir = Path(BASE_TRUTH_DIR) #anno_path = Path(anno_path) #slide_contains_tumor = osp.basename(slide_paths[i]).startswith('tumor_') print(slide_path) dimensions = [] with openslide.open_slide(slide_path) as slide: dtotal = (slide.dimensions[0] / 224, slide.dimensions[1] / 224) thumbnail = slide.get_thumbnail((dtotal[0], dtotal[1])) thum = np.array(thumbnail) ddtotal = thum.shape dimensions.extend(ddtotal) hsv_image = cv2.cvtColor(thum, cv2.COLOR_BGR2HSV) h, s, v = cv2.split(hsv_image) hthresh = threshold_otsu(h) sthresh = threshold_otsu(s) vthresh = threshold_otsu(v) # be min value for v can be changed later minhsv = np.array([hthresh, sthresh, 70], np.uint8) maxhsv = np.array([180, 255, vthresh], np.uint8) thresh = [minhsv, maxhsv] #extraction the countor for tissue rgbbinary = cv2.inRange(hsv_image, thresh[0], thresh[1]) _, contours, _ = cv2.findContours(rgbbinary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) bboxtcols = ['xmin', 'xmax', 'ymin', 'ymax'] bboxt = pd.DataFrame(columns=bboxtcols) for c in contours: (x, y, w, h) = cv2.boundingRect(c) bboxt = bboxt.append(pd.Series([x, x + w, y, y + h], index=bboxtcols), ignore_index=True) bboxt = pd.DataFrame(bboxt) xxmin = list(bboxt['xmin'].get_values()) xxmax = list(bboxt['xmax'].get_values()) yymin = list(bboxt['ymin'].get_values()) yymax = list(bboxt['ymax'].get_values()) xxxmin = np.min(xxmin) xxxmax = np.max(xxmax) yyymin = np.min(yymin) yyymax = np.max(yymax) dcoord = (xxxmin, xxxmax, yyymin, yyymax) dimensions.extend(dcoord) # bboxt = math.floor(np.min(xxmin)*256), math.floor(np.max(xxmax)*256), math.floor(np.min(yymin)*256), math.floor(np.max(yymax)*256) samplesnew = pd.DataFrame( pd.DataFrame(np.array(thumbnail.convert('L')))) print(samplesnew) # very critical: y value is for row, x is for column samplesforpred = samplesnew.loc[yyymin:yyymax, xxxmin:xxxmax] dsample = samplesforpred.shape dimensions.extend(dsample) np.save( 'dimensions_%s' % (osp.splitext(osp.basename(slide_paths[i]))[0]), dimensions) print(samplesforpred) samplesforpredfinal = pd.DataFrame(samplesforpred.stack()) print(samplesforpredfinal) samplesforpredfinal['tile_loc'] = list(samplesforpredfinal.index) samplesforpredfinal.reset_index(inplace=True, drop=True) samplesforpredfinal['slide_path'] = slide_paths[i] print(samplesforpredfinal) return samplesforpredfinal
# T. Y. Zhang and C. Y. Suen, Communications of the ACM, # March 1984, Volume 27, Number 3. # # .. [Lee94] T.-C. Lee, R.L. Kashyap and C.-N. Chu, Building skeleton models # via 3-D medial surface/axis thinning algorithms. # Computer Vision, Graphics, and Image Processing, 56(6):462-478, # 1994. import matplotlib.pyplot as plt from skimage.morphology import skeletonize, skeletonize_3d from skimage.io import imread from skimage.filters import threshold_otsu data = imread('CharImage/hangul_882.jpeg') global_thresh = threshold_otsu(data) binary_global = data > global_thresh data = binary_global skeleton = skeletonize(data) skeleton3d = skeletonize_3d(data) fig, axes = plt.subplots(1, 3, figsize=(8, 4), sharex=True, sharey=True) ax = axes.ravel() ax[0].imshow(data, cmap=plt.cm.gray, interpolation='nearest') ax[0].set_title('original') ax[0].axis('off') ax[1].imshow(skeleton, cmap=plt.cm.gray, interpolation='nearest')
x_pixel = 6556 y_pixel = 4227 distance = 100 # plt.imsave(r'C:\Users\owner\Desktop\dataset\test.jpg', # image[y_pixel - 20:y_pixel + 20, x_pixel - 20:x_pixel + 20, :]) # # cc=filters.threshold_otsu(image[:,:,0]) cell_matrix = image[:, :, 0][y_pixel - distance:y_pixel + distance, x_pixel - distance:x_pixel + distance] # plt.imsave("check.jpg",image[y_pixel - distance:y_pixel + distance, x_pixel - distance:x_pixel + distance,:]) x,y=cell_matrix.shape print("image mean is : ", cell_matrix[cell_matrix>0].mean()) pixel_to_mictoMeter=0.203125 val = filters.threshold_otsu(cell_matrix[cell_matrix>0]) plt.figure(figsize=(20, 20)) plt.subplot(141) plt.title("histogram") ax = plt.gca() range=cell_matrix.max() plt.hist(cell_matrix.ravel(),range,[0,range]) # print ("threshold whole image is : "+ str(cc)) # # print (cell_matrix.std()) # # threshold=val+1*cell_matrix.std() # # print("threshold=",threshold) # plt.imshow(cell_matrix>threshold, cmap='gray') # print("ostu threshold is :",val)