def roi_edge_1(filename_1): #clean_im, pts2 = main() # for filename_1, filename_2 in zip(files_1, files_2): #filename = 'D:\\Project-tumor-detection\\slike\\test\\roi\\canny\\.jpg' print(filename_1) im = cv2.imread(filename_1) h = np.size(im, 0) w = np.size(im, 1) # cv2.imshow("i", im) # cv2.waitKey(0) PATCH_SIZE = 10 #### 10 im_2, pts2, a = main(PATCH_SIZE, im) ret1, im_2 = cv2.threshold(im_2, 100, 255, cv2.THRESH_BINARY) #print(pts2) pts_new = [] for px in pts2: #print(im_2.shape) px_value = 0 value_list = [] #print(px) value_1 = im_2[px[0] - 1, px[1]] value_list.append(value_1) #print("value_1", value_1) value_2 = im_2[px[0] + 1, px[1]] value_list.append(value_2) #print("value_2", value_2) value_3 = im_2[px[0], px[1] - 1] value_list.append(value_3) #print("value_3", value_3) value_4 = im_2[px[0], px[1] + 1] value_list.append(value_4) #print("value_4", value_4) for k in value_list: px_value += k #print("px_value", px_value) if px_value >= 255 * 3: ###########> pts_new.append(px) # print("px", px) #print("pts_new", pts_new) clean_img = CleanImage(im_2, pts_new) # cv2.imwrite(filename, clean_img) # cv2.imshow("clean", clean_img) # cv2.waitKey(0) print("curve_fit") # for value_list in pts_new: # x_coord = value_list[0] # y_coord = value_list[1] # plt.plot(x_coord, y_coord, 'ro', ms='5') #plt.show() center = centroid(pts_new) new_pts = calc_angle(pts_new, center) ########## REGRESSION ############# # POLYFIT SPLINE ########## print(clean_img.shape) xP, yP = curve_fit(new_pts) print("join edge") for x, y in zip(xP, yP): if x <= h and y <= w: clean_img[int(x)][int(y)] = 255 # cv2.imwrite(filename_2, clean_img) cv2.imshow("join edge", clean_img) cv2.waitKey(0) cv2.destroyAllWindows() return clean_img
def segmentation(filenames_1, filenames_2, filenames_3): """segment images in folder filenames_1, save mask in folder filenames_2 and save roi for each image in folder filenames_3 """ for files_1, files_2, files_3 in zip(filenames_1, filenames_2, filenames_3): print(files_1) ### dobijanje 1. regiona img = cv2.imread(files_1, 0) img_original = cv2.imread(files_1) ### EDGE edges = canny_img(img) # cv2.imshow("i1",img) # cv2.waitKey(0) ### POLYGON PATCH_SIZE = 10 #### 10 img_edge, pts2, a = main(PATCH_SIZE, edges) ### HOUGH TRANSFORMATION lines = StraightLineDetection(img, img_edge) #original? ### INTERPOLATION interpolated = roi_edge_2(lines, img_original) mask_binary = fill_area(interpolated) # cv2.imshow("i1",im) # cv2.waitKey(0) ## cv2.imwrite(files_2, mask_binary) new_img = mask(mask_binary, img_original) # cv2.imwrite(files_2, new_img) ### dobijanje 2. regiona # new_img = cv2.imread(files_2,0) edges_2 = canny_img(new_img) #edge_tumor PATCH_SIZE = 10 #### 10 img_edge_2, pts2, a = main(PATCH_SIZE, edges_2) lines_2 = StraightLineDetection(img_edge, img_edge) # # cv2.imshow("i", lines_2) # # cv2.waitKey(0) interpolated_2 = roi_edge_2(lines_2, img_original) mask_binary_2 = fill_area(interpolated_2) ### SAVE MASK ### cv2.imwrite(files_2, mask_binary_2) ########### DILATION ######## # mask_binary_2 = dilation_func(mask_binary_2) # cv2.imwrite(files_3, mask_binary_2) # new_img_2 = mask(mask_binary_2, im) # mask_binary_2 = cv2.imread(files_3,0) new_img_2 = mask(mask_binary_2, img_original) cv2.imwrite(files_3, new_img_2) return
# cv2.imshow("image1", original_img) # cv2.waitKey(0) return image if __name__ == "__main__": # filename = 'D:\\Project-tumor-detection\\slike\\test\\roi\\canny\\131.jpg' # image = roi_edge_2(filename) # image = get_contours(image, image) # image = fill_area(image) files_1 = glob.glob( 'D:\\Project-tumor-detection\\slike\\training&test set-edge\\*.jpg') files_2 = glob.glob( 'D:\\Project-tumor-detection\\slike\\training&test set\\*.jpg') files_3 = glob.glob( 'D:\\Project-tumor-detection\\slike\\training&test set-edge\\roi\\*.jpg' ) for filename_1, filename_2, filename_3 in zip(files_1, files_2, files_3): img = cv2.imread(filename_1, 0) PATCH_SIZE = 10 #### 10 img, pts2, a = main(PATCH_SIZE, img) original_img = cv2.imread(filename_2) im = roi_edge_2(img, original_img) #cv2.imwrite(filename_2, im) im = fill_area(im) cv2.imwrite(filename_3, im)
) for files_1, files_2, files_3, write_1, write_2, write_3 in zip( filename_1, filename_2, filename_3, write_edge, write_hough, write_masks): img = cv2.imread(files_1, 0) # img2 = cv2.imread("D:\\Project-tumor-detection\\slike\\test\\hough-polygon\\*.jpeg") original_img = cv2.imread(files_2, 0) original_img2 = cv2.imread(files_3) img_2 = mask(img, original_img) edge = edge_tumor(img_2) PATCH_SIZE = 30 #### 10 cv2.imwrite(write_1, edge) # try only edges edge, pts2, a = main(PATCH_SIZE, edge) img = StraightLineDetection(original_img, edge) cv2.imwrite(write_2, img) im = roi_edge_2(img, original_img2) im = get_contours(im, im) # roi_edge_1(edge) im = fill_area(im) cv2.imwrite(write_3, im) im3 = mask(im, original_img) cv2.destroyAllWindows()
from skimage.segmentation import active_contour import cv2 from polygon_correct2 import main img = cv2.imread( 'D:\\Project-tumor-detection\\slike\\test\\edge-operators\\canny\\age 40, m.jpeg', 0) #img = rgb2gray(img) # s = np.linspace(0, 2*np.pi, 400) # x = 920 + 100*np.cos(s) # y = 500 + 100*np.sin(s) # init = np.array([x, y]).T PATCH_SIZE = 50 #### 10 im_2, pts2, array_hull = main(PATCH_SIZE, img) init = [] for i in array_hull: init.append([i[0], i[1]]) print(init) snake = active_contour(gaussian(img, 3), init, alpha=0.015, beta=10, gamma=0.001) print(snake) # fig, ax = plt.subplots(figsize=(7, 7)) # ax.imshow(img, cmap=plt.cm.gray) # #ax.plot(init[:, 0], init[:, 1], '--r', lw=3)