def main(): ''' This part is for testing gvf.py with single image. Input: an original image Output: Thresholding image and seed image ''' images = [] temp = cv2.imread(sys.argv[1]) images.append(cv2.cvtColor(temp, cv2.COLOR_BGR2GRAY)) # Binarization th = athresh(images) threh = th.applythresh() # Nuclei center detection gvf = GVF(images, threh) dismap = gvf.distancemap() newimg = gvf.new_image(0.4, dismap) # choose alpha as 0.4. gradimg = gvf.compute_gvf(newimg) imgpair = gvf.find_certer(gradimg[0], 0) cv2.imwrite('imgpair_test.tif', (np.clip(imgpair, 0, 255)).astype(np.uint8))
def main(): use_simple_marker = True # Binarization if use_simple_marker: binarymark, mark = ws_simple(enhance_images) else: th = athresh(enhance_images) threh = th.applythresh() write_image(threh[0]*255, "threh", 0) # Nuclei center detection gvf = GVF(images, threh) dismap = gvf.distancemap() newimg = gvf.new_image(10, dismap) # choose alpha as 0.4. write_image((dismap[0]*10).astype(np.uint8), "dismap", 0) write_image(newimg[0], "newimg", 0) gradimg = gvf.compute_gvf(newimg) if not os.path.exists(temporary_result): os.makedirs(temporary_result) os.chdir(temporary_result) imgpairs = [] # bin_imgpairs = [] # imgpair_raws = [] for i, grad in enumerate(gradimg): load_img = load_image('imgpair', i) if load_img == []: imgpair, bin_imgpair, imgpair_raw = gvf.find_certer(grad, i) imgpairs.append(imgpair) # bin_imgpairs.append(bin_imgpair) # imgpair_raws.append(imgpair_raw) # write_image(imgpair_raw, 'imgpair_raw', i) # write_image(bin_imgpair, 'bin_imgpair', i) write_image(imgpair, 'imgpair', i) else: imgpair = load_img imgpairs.append(imgpair) os.chdir(os.pardir) # watershed ws = WS(newimg, imgpairs) wsimage, binarymark, mark = ws.watershed_compute() centroid = [] slope_length = [] # Build Delaunay Triangulation for i in range(len(images)): graph = GRAPH(mark, binarymark, i) tempcentroid, tempslope_length = graph.run(False) # how many centroid points, and slope and length connect to this point centroid.append(tempcentroid) slope_length.append(tempslope_length) print('building Delaunay Triangulation %d/%d '%(i, len(images))) # Build the Dissimilarity measure vector vector = [] for i in range(len(images)): print " feature vector: image ", i v = FEA() v.set_centroid(centroid[i]) v.set_spatial(slope_length[i]) v.set_shape(enhance_images[i], mark[i]) v.set_histogram() v.add_label() v.add_id(mark[i].max(), i) gen_vec = v.generate_vector() vector.append(gen_vec) print "num of nuclei: ", len(vector[i]) # Feature matching mask = [] for i in range(len(images)-1): print " Feature matching: image ", i m = MAT(i,i+1,[images[i], images[i+1]], vector) mask.append(m.generate_mask(mark[i], i)) m.find_match(0.3) mask = m.match_missing(mask, max_frame=2, max_distance=5) vector[i+1] = m.mitosis_refine() m.new_id() vector[i+1] = m.return_vectors() # write gif image showing the final result def find_max_id(temp_vector): max_id = 0 for pv in temp_vector: for p in pv: if p.id > max_id: max_id = p.id return max_id # This part is to mark the result in the normolized image and # write the gif image. max_id = find_max_id(vector) max_id = int(max_id) colors = [np.random.randint(0, 255, size=max_id), \ np.random.randint(0, 255, size=max_id), \ np.random.randint(0, 255, size=max_id)] font = cv2.FONT_HERSHEY_SIMPLEX selecy_id = 9 enhance_imgs = [] for i, m in enumerate(mask): print " write the gif image: image ", i enhance_imgs.append(cv2.cvtColor(enhance_images[i], cv2.COLOR_GRAY2RGB)) for pv in vector[i]: center = pv.c if not pv.l: color = (colors[0][int(pv.id) - 1], \ colors[1][int(pv.id) - 1], \ colors[2][int(pv.id) - 1],) else: color = (colors[0][int(pv.l) - 1], \ colors[1][int(pv.l) - 1], \ colors[2][int(pv.l) - 1],) if m[int(center[0]), int(center[1])]: enhance_imgs[i][m == pv.id] = color cv2.putText(enhance_imgs[i], \ str(int(pv.id)), (int(pv.c[1]), \ int(pv.c[0])), font, 0.5, \ (255, 255, 255), 1) os.chdir(temporary_result) imageio.mimsave('mitosis_final.gif', enhance_imgs, duration=0.6) cells = set_date(vector) write_info(cells, "res_track") render.plot_tracks(vector) print "finish!"
def main(): # # Binarization th = athresh(enhance_images) threh = th.applythresh() write_image(threh[0] * 255, "threh", 0) # Nuclei center detection gvf = GVF(images, threh) dismap = gvf.distancemap() newimg = gvf.new_image(4, dismap) # choose alpha as 0.4. write_image((dismap[0] * 10).astype(np.uint8), "dismap", 0) write_image(newimg[0], "newimg", 0) gradimg = gvf.compute_gvf(newimg) os.chdir("temporary_result") imgpairs = [] bin_imgpairs = [] imgpair_raws = [] for i, grad in enumerate(gradimg): imgpair, bin_imgpair, imgpair_raw = gvf.find_certer(grad, i) imgpairs.append(imgpair) bin_imgpairs.append(bin_imgpair) imgpair_raws.append(imgpair_raw) write_image(imgpair_raw, 'imgpair_raw', i) write_image(bin_imgpair, 'bin_imgpair', i) write_image(imgpair, 'imgpair', i) os.chdir(os.pardir) # watershed ws = WS(newimg, imgpair) wsimage, binarymark, mark = ws.watershed_compute() centroid = [] slope_length = [] # Build Delaunay Triangulation for i in range(len(images)): graph = GRAPH(mark, binarymark, i) tempcentroid, tempslope_length = graph.run(True) centroid.append(tempcentroid) slope_length.append(tempslope_length) # Build the Dissimilarity measure vector vector = [] for i in range(len(images)): print " feature vector: image ", i v = FEAVECTOR() v.set_centroid(centroid[i]) v.set_spatial(slope_length[i]) v.set_shape(images[i], markers[i]) v.set_histogram() v.add_label() v.add_id(markers[i].max(), i) vector.append(v.generate_vector()) print "num of nuclei: ", len(vector[i]) # Feature matching for i in range(len(images) - 1): print " Feature matching: image ", i m = SIMPLE_MATCH(i, i + 1, [images[i], images[i + 1]], vector) mask.append(m.generate_mask(markers[i], i)) m.find_match(0.3) mask = m.match_missing(mask, max_frame=2, max_distance=20) vector[i + 1] = m.mitosis_refine() m.new_id() vector[i + 1] = m.return_vectors() # write images if necessary os.chdir("temporary_result") for i in range(len(images)): write_image(imgpair_raw, 'imgpair_raw', i) write_image(threh[i].astype(np.uint8) * 255, 'threh', i) write_image(enhance_images[i], 'normalize', i) write_image(mark[i].astype(np.uint8), 'mark', i) write_image(binarymark[i].astype(np.uint8), 'binarymark', i) write_image(tempimg[i].astype(np.uint8), 'tempimg', i) write_image(dismap[i].astype(np.uint8), 'dismap', i) os.chdir(os.pardir)