def get_segmented_image(sigma, neighbor, K, min_comp_size, input_file, output_file): if neighbor != 4 and neighbor != 8: logger.warn( 'Invalid neighborhood choosed. The acceptable values are 4 or 8.') logger.warn('Segmenting with 4-neighborhood...') start_time = time.time() image_file = Image.open(input_file) size = image_file.size # (width, height) in Pillow/PIL logger.info('Image info: {} | {} | {}'.format(image_file.format, size, image_file.mode)) # Gaussian Filter smooth = image_file.filter(ImageFilter.GaussianBlur(sigma)) smooth = np.array(smooth) logger.info("Creating graph...") graph_edges = build_graph(smooth, size[1], size[0], diff, neighbor == 8) #返回8邻域中任意两个节点间的连接权值 logger.info("Merging graph...") forest = segment_graph(graph_edges, size[0] * size[1], K, min_comp_size, threshold) logger.info( "Visualizing segmentation and saving into: {}".format(output_file)) image = generate_image(forest, size[1], size[0]) image.save(output_file) logger.info('Number of components: {}'.format(forest.num_sets)) logger.info('Total running time: {:0.4}s'.format(time.time() - start_time))
def graph_based_segmentation(neighbor_list, num_nodes, K, min_size, threshold): xcoor = [] ycoor = [] diff = [] length = 0 for i in range(len(neighbor_list)): xcoor.append(neighbor_list[i][0]) ycoor.append(neighbor_list[i][1]) diff.append(neighbor_list[i][2]) graph = build_graph(length, diff, xcoor, ycoor) forest = segment_graph(graph, num_nodes + 1, K, min_size, threshold) # output prediction results prediction = [] for x in xrange(num_nodes + 1): prediction.append(forest.find(x)) # prediction clusters rename name_set = [] for name in prediction: if name not in name_set: name_set.append(name) rename = {} for i in range(len(name_set)): rename[name_set[i]] = i + 1 for i in range(len(prediction)): prediction[i] = rename[prediction[i]] # Write results to files and save # with open('prediction','w') as f: # for i in prediction: # f.write(str(i) + '\n') return [forest, prediction]
def main(sigma, neighborhood, K ,min_size ,input_file): neighbor = neighborhood if neighbor != 4 and neighbor!= 8: print('Invalid neighborhood choosed. The acceptable values are 4 or 8.') print('Segmenting with 4-neighborhood...') image_file = Image.open(input_file) image_file = image_file.resize((250,250)) size = image_file.size #print ('Image info: ', image_file.format, size, image_file.mode) grid = gaussian_grid(sigma) #2print(image_file.mode) if image_file.mode == 'RGB': image_file.load() r, g, b = image_file.split() r = filter_image(r, grid) g = filter_image(g, grid) b = filter_image(b, grid) smooth = (r, g, b) diff = diff_rgb else: return 0 graph = build_graph(smooth, size[1], size[0], diff, neighbor == 8) forest = segment_graph(graph, size[0]*size[1], K, min_size, threshold) image = generate_image(image_file , forest, size[1], size[0]) return image print ('Number of components: %d' % forest.num_sets)
def get_segmented_image(sigma, neighbor, K, min_comp_size, input_file, output_file): if neighbor != 4 and neighbor!= 8: logger.warn('Invalid neighborhood choosed. The acceptable values are 4 or 8.') logger.warn('Segmenting with 4-neighborhood...') start_time = time.time() image_file = Image.open(input_file) size = image_file.size # (width, height) in Pillow/PIL logger.info('Image info: {} | {} | {}'.format(image_file.format, size, image_file.mode)) # Gaussian Filter smooth = image_file.filter(ImageFilter.GaussianBlur(sigma)) smooth = np.array(smooth) logger.info("Creating graph...") graph_edges = build_graph(smooth, size[1], size[0], diff, neighbor==8) logger.info("Merging graph...") forest = segment_graph(graph_edges, size[0]*size[1], K, min_comp_size, threshold) logger.info("Visualizing segmentation and saving into: {}".format(output_file)) image = generate_image(forest, size[1], size[0]) image.save(output_file) logger.info('Number of components: {}'.format(forest.num_sets)) logger.info('Total running time: {:0.4}s'.format(time.time() - start_time))
def get_segmented_image(sigma, neighbor, K, min_comp_size, input_file, output_file): if neighbor != 4 and neighbor != 8: logger.warn( 'Invalid neighborhood choosed. The acceptable values are 4 or 8.') logger.warn('Segmenting with 4-neighborhood...') start_time = time.time() image_file = Image.open(input_file) size = image_file.size # (width, height) in Pillow/PIL logger.info('Параметры изображения: {} | {} | {}'.format( image_file.format, size, image_file.mode)) # Gaussian Filter smooth = image_file.filter(ImageFilter.GaussianBlur(sigma)) smooth = np.array(smooth).astype(int) logger.info("Создание графа...") graph_edges = build_graph(smooth, size[1], size[0], diff, neighbor == 8) logger.info("Вычисление разреза...") forest = segment_graph(graph_edges, size[0] * size[1], K, min_comp_size, threshold) logger.info("Визуализация: {}".format(output_file)) image = generate_image(forest, size[1], size[0]) image.save(output_file) logger.info('Количество сегментов: {}'.format(forest.num_sets)) logger.info('Общее время выполнения: {:0.4}s'.format(time.time() - start_time))
def seg_a_img(img, img_name, img_mode="remote_sensing"): grid = gaussian_grid(SIGMA) if img_mode == "remote_sensing": c, h, w = img.shape[0], img.shape[1], img.shape[2] print("we are working in geo-tif mode, img is read by gdal") img_bands = [] for i in range(c): img_bands.append(filter_image(img[i], grid)) smooth = tuple(img_bands) diff = diff_geoTif elif img_mode == "BGR": print("we are working in BGR mode, the img is read by opencv") h, w, c = img.shape[0], img.shape[1], img.shape[2] smooth = [] for i in range(3): smooth.append(filter_image(img[:, :, i], grid)) smooth = tuple(smooth) diff = diff_rgb else: h, w = img.shape[0], img.shape[1] smooth = filter_image(img, grid) diff = diff_grey graph = build_graph(smooth, w, h, diff, neighbor == 8) forest = segment_graph(graph, h * w, K, MIN_SIZE, threshold) image = generate_image(forest, w, h) image.save("seg_res/%s.png" % img_name) # image.save(sys.argv[6]) print 'Number of components: %d' % forest.num_sets
def run_segmentation(self): self.graph = build_graph(img=self.image_smooth, width=self.width, height=self.height, diff=self.diff, neighborhood_8=self.neighbor == 8) self.forest = segment_graph(self.graph, self.height * self.width, self.K, self.min_size, threshold)
def train(): features = [] roots = [] # 用前10张图作为训练集 for i in range(1, 21): print("当前训练图片", str(i) + '.png') pic_path = '../data/imgs/' + str(i) + '.png' image = cv2.imread(pic_path, cv2.IMREAD_COLOR) # 高斯滤波 source = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image = cv2.GaussianBlur(source, (3, 3), sigmaX=1.0, sigmaY=1.0) height = image.shape[0] width = image.shape[1] graph_edges = graph.build_graph(image) forest = graph.segment_graph(graph_edges, height * width, 200, 20) gt_root = graph.gt(forest, i) # 计算全图颜色直方图 hist = cv2.calcHist([image], [0, 1, 2], None, [8, 8, 8], [0, 256, 0, 256, 0, 256]) g_feature = hist.ravel().tolist() for c in forest.roots: l_feature = local(image, forest, c) # 拼接区域颜色直方图和全图颜色直方图 feature = np.concatenate((g_feature, l_feature), axis=0) roots.append(gt_root[c]) features.append(feature) X = np.array(features) Y = np.array(roots) # PCA降维至50维 pca = PCA(n_components=50) new_X = pca.fit_transform(X) clf = svm.SVC(gamma=0.001, C=100.) print("训练开始") model = clf.fit(new_X, Y) print("训练结束") return model
def run_segmentation(image, print_info=False): image_file = Image.fromarray(image) #Image.open(filename).convert('L') height, width = image_file.size neighbor = 8 # only 4 or 8 sigma = 0.8 # float(sys.argv[1]) K = 3000 # float(sys.argv[3]) min_size = height * width / 500 # int(sys.argv[4]) grid = gaussian_grid(sigma) if image_file.mode == 'RGB': image_file.load() r, g, b = image_file.split() r = filter_image(r, grid) g = filter_image(g, grid) b = filter_image(b, grid) smooth = (r, g, b) diff = diff_rgb else: smooth = filter_image(image_file, grid) diff = diff_grey graph = build_graph(img=smooth, width=width, height=height, diff=diff, neighborhood_8=neighbor == 8) forest = segment_graph(graph, height * width, K, min_size, threshold) output = build_map(forest, width, height) #output = build_image(forest, width, height) if print_info: print 'Image info: ', image_file.format, height, width, image_file.mode print 'Number of components: %d' % forest.num_sets return output
K = float(sys.argv[3]) min_size = int(sys.argv[4]) size = image_file.size print 'Image info: ', image_file.format, size, image_file.mode grid = gaussian_grid(sigma) if image_file.mode == 'RGB': image_file.load() r, g, b = image_file.split() r = filter_image(r, grid) g = filter_image(g, grid) b = filter_image(b, grid) smooth = (r, g, b) diff = diff_rgb else: smooth = filter_image(image_file, grid) diff = diff_grey graph = build_graph(smooth, size[1], size[0], diff, neighbor == 8) forest = segment_graph(graph, size[0] * size[1], K, min_size, threshold) image = generate_image(forest, size[1], size[0]) image.save(sys.argv[6]) print 'Number of components: %d' % forest.num_sets
sigma = float(sys.argv[1]) K = float(sys.argv[3]) min_size = int(sys.argv[4]) size = image_file.size print 'Image info: ', image_file.format, size, image_file.mode grid = gaussian_grid(sigma) if image_file.mode == 'RGB': # image_file.load() # Useless r, g, b = image_file.split() r = filter_image(r, grid) g = filter_image(g, grid) b = filter_image(b, grid) smooth = (r, g, b) diff = diff_rgb # function pointer else: smooth = filter_image(image_file, grid) diff = diff_grey # function pointer graph = build_graph(smooth, size[0], size[1], diff, neighbor == 8) forest = segment_graph(graph, size[0]*size[1], K, min_size, threshold) image = generate_image(forest, size[0], size[1]) image.save(sys.argv[6]) print 'Number of components: %d' % forest.num_sets
return new_img if __name__ == '__main__': for i in range(1, 1001): if i % 100 == 66: print("当前处理图片", str(i) + '.png') pic_path = '../data/imgs/' + str(i) + '.png' image = cv2.imread(pic_path) # 高斯滤波 source = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) smooth = cv2.GaussianBlur(source, (3, 3), sigmaX=1.0, sigmaY=1.0) graph_edges = build_graph(smooth) forest = segment_graph(graph_edges, image.shape[0] * image.shape[1], 200, 20) seg_img = generate_image(forest, image.shape[0], image.shape[1]) new_img = gt(forest, i) # 输出结果并保存 plt.figure() plt.subplot(121) plt.imshow(seg_img) plt.axis('off') plt.subplot(122) plt.imshow(new_img, cmap=plt.cm.gray) plt.axis('off') plt.savefig('result/' + str(i) + '.png') plt.show()
features = [] roots = [] for i in range(1, 1001): if i % 100 == 66: print("当前测试图片", str(i) + '.png') pic_path = '../data/imgs/' + str(i) + '.png' image = cv2.imread(pic_path, cv2.IMREAD_COLOR) # 高斯滤波 source = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image = cv2.GaussianBlur(source, (3, 3), sigmaX=1.0, sigmaY=1.0) height = image.shape[0] width = image.shape[1] graph_edges = graph.build_graph(image) forest = graph.segment_graph(graph_edges, height * width, 200, 20) gt_root = graph.gt(forest, i) # 计算全图颜色直方图 hist = cv2.calcHist([image], [0, 1, 2], None, [8, 8, 8], [0, 256, 0, 256, 0, 256]) g_feature = hist.ravel().tolist() for c in forest.roots: l_feature = local(image, forest, c) # 拼接区域颜色直方图和全图颜色直方图 feature = np.concatenate((g_feature, l_feature), axis=0) roots.append(gt_root[c]) features.append(feature) X = np.array(features)
im_predicted = Image.fromarray(pre_large_im) im_predicted.save('PCNN.tif') # This is Pixel-based CNN plt.savefig("PCNN.png", dpi=300) # Begain object-based CNN classification grid = gaussian_grid(sigma) if img.shape[2] == 3: r = filter_image(img[:, :, 0], grid) g = filter_image(img[:, :, 1], grid) b = filter_image(img[:, :, 2], grid) smooth = (r, g, b) diff = diff_rgb elif img.shape[2] > 3: print("Not supported more than 3 bands.") else: smooth = filter_image(img, grid) diff = diff_grey graph = build_graph(smooth, img.shape[0], img.shape[1], diff, neighbor == 8) forest = segment_graph(graph, img.shape[0] * img.shape[1], K, min_size, threshold) image_seg = generate_image(forest, img.shape[0], img.shape[1]) ocnn_labels = ocnn(image_seg, img.shape[0], img.shape[1], pre_large_im) plt.imshow(ocnn_labels) plt.savefig("OCNN.png", dpi=300) #sess.close() else: print "no pre-trained model found!" tf.reset_default_graph() # clear all stored paramters