def run_cmd(method, block_size=40): stdin = sys.stdin.read() if stdin == '\n': exit() img = Image.open(StringIO.StringIO(stdin)).convert('L') imgc = np.array(img) imggray = rgb2gray(imgc) if method is None or method == '': imgthresh = threshold_adaptive(imggray, block_size, 'gaussian', offset=10) elif method == 'gaussian': imgthresh = threshold_adaptive(imggray, block_size, 'gaussian', offset=10) elif method == 'median': imgthresh = threshold_adaptive(imggray, block_size, 'median', offset=10) elif method == 'mean': imgthresh = threshold_adaptive(imggray, block_size, 'mean', offset=10) elif method == 'otsu': thresh = threshold_otsu(imggray) imgthresh = imggray > thresh elif method == 'yen': thresh = threshold_yen(imggray) imgthresh = imggray > thresh elif method == 'iso': thresh = threshold_isodata(imggray) imgthresh = imggray > thresh rescaled = (255.0 / imgthresh.max() * (imgthresh - imgthresh.min())).astype(np.uint8) out = Image.fromarray(rescaled) out.save(sys.stdout, format='PNG')
def run_cmd(percentOfWindow, ratio): stdin = sys.stdin.read() if stdin == '\n': exit() img = Image.open(StringIO.StringIO(stdin)).convert('L') imgc = np.array(img) img = rgb2gray(imgc) thresh = threshold_yen(img) img = img > thresh height, width = img.shape imsize = width * height labels = label(img) bb = 'BoundingBox' regions = regionprops(labels, ['BoundingBox']) blobs = np.array([ l[bb] for l in regions if (maxX(l[bb]) - minX(l[bb])) * (maxY(l[bb]) - minY(l[bb])) > 2000 ]) if len(blobs) == 0: imout = np.uint8(imgc) out = Image.fromarray(imout) out.save(sys.stdout, format='PNG') exit() # Find closest blob to desired ratio and percent of window sizes = [(maxX(b) - minX(b)) * (maxY(b) - minY(b)) for b in blobs] percents = [s / float(imsize) for s in sizes] percent_diffs = [abs(percentOfWindow - p) for p in percents] aratios = [(maxX(b) - minX(b)) / float(maxY(b) - minY(b)) for b in blobs] aratio_diffs = [abs(r - ratio) for r in aratios] min_ratio_diff = min(aratio_diffs) min_percent_diff = min(percent_diffs) close_ratio = aratio_diffs.index(min_ratio_diff) close_percent = percent_diffs.index(min_percent_diff) if close_ratio != close_percent: min_ratio_score = min_ratio_diff / ratio min_ratio_percent_score = percent_diffs[close_ratio] / percentOfWindow min_percent_score = min_percent_diff / percentOfWindow min_percent_ratio_score = aratio_diffs[close_percent] if min_ratio_score * min_ratio_percent_score < min_percent_score * min_percent_ratio_score: b_index = close_ratio else: b_index = close_percent else: b_index = close_ratio # They're the same b = blobs[b_index] minx = minX(b) miny = minY(b) maxx = maxX(b) maxy = maxY(b) imgc = imgc[minx:maxx, miny:maxy] imout = np.uint8(imgc) out = Image.fromarray(imout) out.save(sys.stdout, format='PNG')
from skimage.measure import regionprops from skimage.draw import circle_perimeter #################################################### #################################### Distance Image - Internal Marker Locus image = io.imread("/Users/rishabgargeya/Desktop/database/DRIVE/training/images/22_training.tif") plt.imshow(image) gray_image = rgb2gray(image) io.imshow(gray_image) med = median(gray_image, disk(3)) io.imshow(med) clahe_image = exposure.equalize_adapthist(med) io.imshow(clahe_image) clahe_image = img_as_ubyte(clahe_image) binary = clahe_image > threshold_yen(clahe_image) io.imshow(binary) binary_label = label(binary) rps = regionprops(binary_label)[1:] #regionprops, ignore background areas = [rp.area for rp in rps] region_with_largest_area = np.argmax(areas) binary = binary_label == rps[region_with_largest_area].label #binary = convex_hull_image(binary) #binary = binary_dilation(binary, disk(15)) != 0 distance = distance_transform_edt(binary) local_maxi = peak_local_max(distance) # internal marker / center of [circle] <- (external marker) ####################################
shapes = tf.shape(image_patches).eval() nr, nc = shapes[1], shapes[2] fig = plt.figure() gs = gridspec.GridSpec(nr, nc) gs.update(wspace=0.01, hspace=0.01) for i in range(nr): for j in range(nc): x = tf.reshape(image_patches[0, i, j, ], [ksize_rows, ksize_cols, 3]) x = tf.squeeze(tf.image.rgb_to_grayscale(x)) x = sess.run(x) ax = plt.subplot(gs[i * nc + j]) plt.axis('off') ax.set_xticklabels([]) ax.set_yticklabels([]) ax.set_aspect('auto') cutoff = filter.threshold_yen(x) #cutoff = filter.threshold_isodata(x) #cutoff = filter.threshold_otsu(x) plt.imshow(x < cutoff, cmap='gray') cancer_cell_area = (x < cutoff).sum() / float(ksize_rows * ksize_cols) print('processed {},{} patch, {}, cancer cell area: {}.'.format( i, j, i * nc + j, cancer_cell_area), file=sys.stderr) plt.savefig('image_patches_gray.png', bbox_inches='tight', dpi=120) #patch1 = tf.reshape(image_patches[0,0,0,], [ksize_rows, ksize_cols, 3]) #patch1_gray = sess.run(tf.squeeze(tf.image.rgb_to_grayscale(patch1))) #yen_cutoff = filter.threshold_yen(patch1_gray)
def run_cmd(percentOfWindow, ratio): stdin = sys.stdin.read() if stdin == '\n': exit() img = Image.open(StringIO.StringIO(stdin)).convert('L') imgc = np.array(img) img = rgb2gray(imgc) thresh = threshold_yen(img) img = img > thresh height, width = img.shape imsize = width * height labels = label(img) bb = 'BoundingBox' regions = regionprops(labels, ['BoundingBox']) blobs = np.array([l[bb] for l in regions if ( maxX(l[bb]) - minX(l[bb])) * (maxY(l[bb]) - minY(l[bb])) > 2000]) if len(blobs) == 0: imout = np.uint8(imgc) out = Image.fromarray(imout) out.save(sys.stdout, format='PNG') exit() # Find closest blob to desired ratio and percent of window sizes = [(maxX(b) - minX(b)) * (maxY(b) - minY(b)) for b in blobs] percents = [s / float(imsize) for s in sizes] percent_diffs = [abs(percentOfWindow - p) for p in percents] aratios = [(maxX(b) - minX(b)) / float(maxY(b) - minY(b)) for b in blobs] aratio_diffs = [abs(r - ratio) for r in aratios] min_ratio_diff = min(aratio_diffs) min_percent_diff = min(percent_diffs) close_ratio = aratio_diffs.index(min_ratio_diff) close_percent = percent_diffs.index(min_percent_diff) if close_ratio != close_percent: min_ratio_score = min_ratio_diff / ratio min_ratio_percent_score = percent_diffs[close_ratio] / percentOfWindow min_percent_score = min_percent_diff / percentOfWindow min_percent_ratio_score = aratio_diffs[close_percent] if min_ratio_score * min_ratio_percent_score < min_percent_score * min_percent_ratio_score: b_index = close_ratio else: b_index = close_percent else: b_index = close_ratio # They're the same b = blobs[b_index] minx = minX(b) miny = minY(b) maxx = maxX(b) maxy = maxY(b) imgc = imgc[minx:maxx, miny:maxy] imout = np.uint8(imgc) out = Image.fromarray(imout) out.save(sys.stdout, format='PNG')
def testCompareSkimageAndIPThresholdYenValue(self): t1 = iptools.threshold_yen(image=self.rgb[...,0]) t2 = filters.threshold_yen(image=self.rgb[...,0]) self.assertEqual(t1, t2, msg='Different Yen algorithm')