class Seg_One_1(object): def __init__(self): self.operate_dir = "../../saved_original_for_seg/503.jpg" self.savedir_path = "../../saved_processed/" self.display_flag = True from A_line import A_line_process self.aline = A_line_process() self.bias = 5 self.region = 50 # the region in vertial self.aligh_flag =True def calculate_the_average_line(self,img,start_p): calculate_w = 5 # only calculate the front10 lines H,W = img.shape new = img[ start_p-self.region:start_p+self.region,5:5+calculate_w] ave_line=new.sum(axis=1)/calculate_w return ave_line #the validation functionfor check the matrix and can also be used for validate the correction result def seg_process(self, Img,start_p ): gray = cv2.cvtColor(Img, cv2.COLOR_BGR2GRAY) H,W = gray.shape #gray = gray [: ,100: W -100] #Img = Img [: ,100: W -100] H,W = gray.shape #gray = cv2.blur(gray,(3,3)) #gray = cv2.medianBlur(gray,5) #gray = cv2.blur(gray,(5,5)) gray = cv2.GaussianBlur(gray,(3,3),0) #gray = cv2.bilateralFilter(gray,15,75,75) #gray = cv2.bilateralFilter(gray,15,75,75) ave_line = self.calculate_the_average_line(gray,start_p) #peaks = self.aline.find_4peak(ave_line) #gray = cv2.blur(gray,(5,5),0) if self.display_flag == True : cv2.imshow('ini',Img) cv2.imshow('blur',gray.astype(np.uint8)) x_kernel = np.asarray([[-1, 0, 1], # Sobel kernel for x-direction [-2, 0, 2], [-1, 0, 1]]) #y_kernel = np.asarray([[-2, -2, -2], # Sobel kernel for y-direction # [1, 1, 1], # [1, 1, 1]]) y_kernel = np.asarray([[-1,-1,-1], # Sobel kernel for y-direction [-1,-1,-1], [-1,-1,-1], [6,6,6], [-1,-1,-1], [-1,-1,-1], [ -1,-1,-1]]) #y_kernel = np.asarray([ # Sobel kernel for y-direction # [-1,-1], # [-1,-1], # [-1,-1], # [-1,-1], # [12,12], # [-1,-1], # [-1,-1], # [-1,-1], # [-1,-1], # [-1,-1], # [-1,-1], # [-1,-1], # [-1,-1], # ]) y_kernel = np.asarray([ # Sobel kernel for y-direction [-1,0 ], [-1,0 ], [-1,0 ], [-1,0 ], [28,0 ], [-1,0 ], [-1,0 ], [-1 ,0], [-1,0 ], [-1,0 ], [-1,0 ], [-1 ,0], [-1,0 ], [-1,0 ], [-1,0 ], [-1,0 ], [-1,0 ], [-1 ,0], [-1,0 ], [-1,0 ], [-1,0 ], [-1,0 ], [-1,0 ], [-1 ,0], [-1,0 ], [-1,0 ], [-1,0 ], [-1,0 ], [-1,0 ], ]) y_kernel = y_kernel/9 gray = gray.astype(np.float) #sobel_x = signal.convolve2d(gray, x_kernel) # sobel_y = signal.convolve2d(gray, y_kernel) # convolve kernels over images sobel_y = np.clip(sobel_y+10, 1,254) sobel_y = cv2.medianBlur(sobel_y.astype(np.uint8),5) sobel_y = cv2.GaussianBlur(sobel_y,(5,5),0) sobel_y = cv2.blur(sobel_y,(5,5)) #sobel_y = cv2.GaussianBlur(sobel_y,(5,5),0) #sobel_y = cv2.bilateralFilter(sobel_y.astype(np.uint8),9,175,175) #find the start 4 point sobel_y=sobel_y[self.bias:H-self.bias, :] Img=Img[self.bias:H-self.bias, :] Rever_img = 255 - sobel_y #ave_line = self.calculate_the_average_line(sobel_y) #peaks = self.aline.find_4peak(ave_line) ave_line = self.calculate_the_average_line(gray,start_p) peaks = self.aline.find_4peak(ave_line) #start_point = 596 peaks = np.clip(peaks, 1,Rever_img.shape[0]-1) #new_peak = np.zeros(4) #new_peak=peaks #new_peak[3] = peaks[2]+35 #peaks = new_peak path1,path_cost1=PATH.search_a_path(Rever_img,start_p- self.region + peaks[0]) path1 =gaussian_filter1d(path1,2) path1 = np.clip(path1, 0,sobel_y.shape[0]-1) for i in range ( len(path1)): sobel_y[int(path1[i]),i]=254 if self.display_flag == True: cv2.imshow('revert',Rever_img.astype(np.uint8)) #cv2.imshow('path on blur',gray.astype(np.uint8)) cv2.imshow('Seg2',Img) #sobel_y = sobel_y*0.1 #edges = cv2.Canny(gray,50, 300,10) cv2.imshow('seg',sobel_y.astype(np.uint8)) cv2.waitKey(1) return path1
class Seg_One_Frame(object): def __init__(self): self.operate_dir = "../../saved_original_for_refraction/2/" #self.operate_dir = "D:/PhD/IPB meeting/natalia paper/fractional correction/Bscan/original/cancer-CSCAN15-BSCAN15-spaing10um-02.jpg" self.savedir_path = "../../saved_processed/2/" #self.savedir_path = "D:/PhD/IPB meeting/natalia paper/fractional correction/Bscan/correct/hyper-bottom.jpg" self.display_flag = True from A_line import A_line_process self.aline = A_line_process() self.bias = 50 self.aligh_flag = True self.rc_corrector = RC_function() #input image to calculate the fist aveline, for get the local minimal value calculation def calculate_the_average_line(self, img): calculate_w = 5 # only calculate the front10 lines H, W = img.shape new = img[:, 5:5 + calculate_w] ave_line = new.sum(axis=1) / calculate_w return ave_line #the validation functionfor check the matrix and can also be used for validate the correction result def seg_process(self, Img_ini): gray = Img_ini H, W = gray.shape #gray = gray [: ,100: W -100] #Img = Img [: ,100: W -100] ave_line = self.calculate_the_average_line(gray) peaks = self.aline.find_4peak(ave_line) H, W = gray.shape original = gray #gray = cv2.blur(gray,(3,3)) #gray = cv2.medianBlur(gray,5) #gray = cv2.blur(gray,(5,5)) gray = cv2.GaussianBlur(gray, (3, 3), 0) #gray = cv2.bilateralFilter(gray,15,75,75) #gray = cv2.bilateralFilter(gray,15,75,75) ave_line = self.calculate_the_average_line(gray) peaks = self.aline.find_4peak(ave_line) #gray = cv2.blur(gray,(5,5),0) if self.display_flag == True: cv2.imshow('ini', Img_ini) cv2.imshow('blur', gray.astype(np.uint8)) x_kernel = np.asarray([ [-1, 0, 1], # Sobel kernel for x-direction [-2, 0, 2], [-1, 0, 1] ]) #y_kernel = np.asarray([[-2, -2, -2], # Sobel kernel for y-direction # [1, 1, 1], # [1, 1, 1]]) y_kernel = np.asarray([ [-1, -1, -1], # Sobel kernel for y-direction [-1, -1, -1], [-1, -1, -1], [6, 6, 6], [-1, -1, -1], [-1, -1, -1], [-1, -1, -1] ]) #y_kernel = np.asarray([ # Sobel kernel for y-direction # [-1,-1], # [-1,-1], # [-1,-1], # [-1,-1], # [12,12], # [-1,-1], # [-1,-1], # [-1,-1], # [-1,-1], # [-1,-1], # [-1,-1], # [-1,-1], # [-1,-1], # ]) y_kernel = np.asarray([ # Sobel kernel for y-direction [-1, 0], [-1, 0], [-1, 0], [-1, 0], [28, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], ]) y_kernel = y_kernel / 9 gray = gray.astype(np.float) #sobel_x = signal.convolve2d(gray, x_kernel) # sobel_y = signal.convolve2d(gray, y_kernel) # convolve kernels over images sobel_y = np.clip(sobel_y + 10, 1, 254) sobel_y = cv2.medianBlur(sobel_y.astype(np.uint8), 5) sobel_y = cv2.GaussianBlur(sobel_y, (5, 5), 0) sobel_y = cv2.blur(sobel_y, (5, 5)) #sobel_y = cv2.GaussianBlur(sobel_y,(5,5),0) #sobel_y = cv2.bilateralFilter(sobel_y.astype(np.uint8),9,175,175) #find the start 4 point #sobel_y=sobel_y[self.bias:H-self.bias, :] #Img=Img[self.bias:H-self.bias, :] ave_line = self.calculate_the_average_line(sobel_y) peaks = self.aline.find_4peak(ave_line) Rever_img = 255 - sobel_y #start_point = 596 peaks = np.clip(peaks, 1, Rever_img.shape[0] - 1) #new_peak = np.zeros(4) #new_peak=peaks #new_peak[3] = peaks[2]+35 #peaks = new_peak if Manual_start_flag == True: peaks[1] = peaks[0] + 472 - 293 peaks[2] = peaks[0] + 500 - 293 peaks[3] = peaks[0] + 677 - 293 path1, path_cost1 = PATH.search_a_path(Rever_img, int(peaks[0])) #path2,path_cost1=PATH.search_a_path(Rever_img,int(peaks[1])) #path3,path_cost1=PATH.search_a_path(Rever_img,int(peaks[2])) #path4,path_cost1=PATH.search_a_path(Rever_img,int(peaks[3])) path1 = gaussian_filter1d(path1, 6) #path2 =gaussian_filter1d(path2,2) #path3 =gaussian_filter1d(path3,2) #path4 =gaussian_filter1d(path4,2) #path4=path3 ##path2 = path3 #path3 = path2+35 #path2 = path2 - 5 #path3,path_cost1=PATH.search_a_path_based_on_path(Rever_img,path3) #path3 =gaussian_filter1d(path3,4) path1 = path1 #-10 path1 = np.clip(path1, 0, sobel_y.shape[0] - 1) #[path1,path2,path3,path4]=np.clip([path1,path2,path3,path4], # 0,sobel_y.shape[0]-1) for i in range(len(path1)): sobel_y[int(path1[i]), i] = 254 Dark_boundaries = sobel_y * 0 path1 = np.clip(path1, 0, Dark_boundaries.shape[0] - 2) for i in range(len(path1)): Dark_boundaries[int(path1[i]), i] = 254 for i in range(gray.shape[1]): gray[int(path1[i]) + 1, i] = gray[int(path1[i]), i] = 254 # corect tje RC #original = cv2.resize(original, (int(W/2),int(H/2)), interpolation=cv2.INTER_LINEAR) new = self.rc_corrector.correct(original, path1) if self.display_flag == True: cv2.imshow('revert', Rever_img.astype(np.uint8)) #cv2.imshow('path on blur',gray.astype(np.uint8)) cv2.imshow('Seg2', Img) #sobel_y = sobel_y*0.1 #edges = cv2.Canny(gray,50, 300,10) cv2.imshow('seg', sobel_y.astype(np.uint8)) cv2.imshow('correct', new.astype(np.uint8)) #cv2.imwrite(self.savedir_path + str(1) +".jpg",sobel_y .astype(np.uint8)) cv2.waitKey(1) return new # Img,sobel_y,Dark_boundaries,[path1,path2,path3,path4] def reference(self, Img): gray = Img H, W = gray.shape #gray = gray [: ,100: W -100] #Img = Img [: ,100: W -100] ave_line = self.calculate_the_average_line(gray) peaks = self.aline.find_4peak(ave_line) H, W = gray.shape original = gray #gray = cv2.blur(gray,(3,3)) #gray = cv2.medianBlur(gray,5) #gray = cv2.blur(gray,(5,5)) gray = cv2.GaussianBlur(gray, (3, 3), 0) #gray = cv2.bilateralFilter(gray,15,75,75) #gray = cv2.bilateralFilter(gray,15,75,75) ave_line = self.calculate_the_average_line(gray) peaks = self.aline.find_4peak(ave_line) #gray = cv2.blur(gray,(5,5),0) if self.display_flag == True: cv2.imshow('ini', Img) cv2.imshow('blur', gray.astype(np.uint8)) x_kernel = np.asarray([ [-1, 0, 1], # Sobel kernel for x-direction [-2, 0, 2], [-1, 0, 1] ]) #y_kernel = np.asarray([[-2, -2, -2], # Sobel kernel for y-direction # [1, 1, 1], # [1, 1, 1]]) y_kernel = np.asarray([ [-1, -1, -1], # Sobel kernel for y-direction [-1, -1, -1], [-1, -1, -1], [6, 6, 6], [-1, -1, -1], [-1, -1, -1], [-1, -1, -1] ]) #y_kernel = np.asarray([ # Sobel kernel for y-direction # [-1,-1], # [-1,-1], # [-1,-1], # [-1,-1], # [12,12], # [-1,-1], # [-1,-1], # [-1,-1], # [-1,-1], # [-1,-1], # [-1,-1], # [-1,-1], # [-1,-1], # ]) y_kernel = np.asarray([ # Sobel kernel for y-direction [-1, 0], [-1, 0], [-1, 0], [-1, 0], [28, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], ]) y_kernel = y_kernel / 9 gray = gray.astype(np.float) #sobel_x = signal.convolve2d(gray, x_kernel) # sobel_y = signal.convolve2d(gray, y_kernel) # convolve kernels over images sobel_y = np.clip(sobel_y + 10, 1, 254) sobel_y = cv2.medianBlur(sobel_y.astype(np.uint8), 5) sobel_y = cv2.GaussianBlur(sobel_y, (5, 5), 0) sobel_y = cv2.blur(sobel_y, (5, 5)) #sobel_y = cv2.GaussianBlur(sobel_y,(5,5),0) #sobel_y = cv2.bilateralFilter(sobel_y.astype(np.uint8),9,175,175) #find the start 4 point sobel_y = sobel_y[self.bias:H - self.bias, :] Img = Img[self.bias:H - self.bias, :] ave_line = self.calculate_the_average_line(sobel_y) peaks = self.aline.find_4peak(ave_line) Rever_img = 255 - sobel_y #start_point = 596 peaks = np.clip(peaks, 1, Rever_img.shape[0] - 1) #new_peak = np.zeros(4) #new_peak=peaks #new_peak[3] = peaks[2]+35 #peaks = new_peak if Manual_start_flag == True: peaks[1] = peaks[0] + 472 - 293 peaks[2] = peaks[0] + 500 - 293 peaks[3] = peaks[0] + 677 - 293 path1, path_cost1 = PATH.search_a_path(Rever_img, int(peaks[0])) #path2,path_cost1=PATH.search_a_path(Rever_img,int(peaks[1])) #path3,path_cost1=PATH.search_a_path(Rever_img,int(peaks[2])) #path4,path_cost1=PATH.search_a_path(Rever_img,int(peaks[3])) path1 = gaussian_filter1d(path1, 2) #path2 =gaussian_filter1d(path2,2) #path3 =gaussian_filter1d(path3,2) #path4 =gaussian_filter1d(path4,2) #path4=path3 ##path2 = path3 #path3 = path2+35 #path2 = path2 - 5 #path3,path_cost1=PATH.search_a_path_based_on_path(Rever_img,path3) #path3 =gaussian_filter1d(path3,4) path1 = path1 #-10 path1 = np.clip(path1, 0, sobel_y.shape[0] - 1) #[path1,path2,path3,path4]=np.clip([path1,path2,path3,path4], # 0,sobel_y.shape[0]-1) for i in range(len(path1)): sobel_y[int(path1[i]), i] = 254 Dark_boundaries = sobel_y * 0 path1 = np.clip(path1, 0, Dark_boundaries.shape[0] - 2) for i in range(len(path1)): Dark_boundaries[int(path1[i]), i] = 254 for i in range(Img.shape[1]): Img[int(path1[i]) + 1, i] = Img[int(path1[i]), i] = 254 # corect tje RC #original = cv2.resize(original, (int(W/1),int(H/1)), interpolation=cv2.INTER_LINEAR) #new = self.rc_corrector.correct(original,path1) if self.display_flag == True: cv2.imshow('revert', Rever_img.astype(np.uint8)) #cv2.imshow('path on blur',gray.astype(np.uint8)) cv2.imshow('Seg2', Img) #sobel_y = sobel_y*0.1 #edges = cv2.Canny(gray,50, 300,10) cv2.imshow('seg', sobel_y.astype(np.uint8)) #cv2.imshow('correct',new.astype(np.uint8)) #cv2.imwrite(self.savedir_path + str(1) +".jpg",sobel_y .astype(np.uint8)) cv2.waitKey(1) return path1
class Seg_One_Frame(object): def __init__(self): self.operate_dir = "../../saved_original_for_seg/503.jpg" self.savedir_path = "../../saved_processed/" self.display_flag = True from A_line import A_line_process self.aline = A_line_process() self.bias = 50 self.aligh_flag = True #input image to calculate the fist aveline, for get the local minimal value calculation def calculate_the_average_line(self, img): calculate_w = 5 # only calculate the front10 lines H, W = img.shape new = img[:, 5:5 + calculate_w] ave_line = new.sum(axis=1) / calculate_w return ave_line #the validation functionfor check the matrix and can also be used for validate the correction result def seg_process(self, Img): gray = cv2.cvtColor(Img, cv2.COLOR_BGR2GRAY) H, W = gray.shape #gray = gray [: ,100: W -100] #Img = Img [: ,100: W -100] ave_line = self.calculate_the_average_line(gray) peaks = self.aline.find_4peak(ave_line) H, W = gray.shape #gray = cv2.blur(gray,(3,3)) #gray = cv2.medianBlur(gray,5) #gray = cv2.blur(gray,(5,5)) gray = cv2.GaussianBlur(gray, (3, 3), 0) #gray = cv2.bilateralFilter(gray,15,75,75) #gray = cv2.bilateralFilter(gray,15,75,75) ave_line = self.calculate_the_average_line(gray) peaks = self.aline.find_4peak(ave_line) #gray = cv2.blur(gray,(5,5),0) if self.display_flag == True: cv2.imshow('ini', Img) cv2.imshow('blur', gray.astype(np.uint8)) x_kernel = np.asarray([ [-1, 0, 1], # Sobel kernel for x-direction [-2, 0, 2], [-1, 0, 1] ]) #y_kernel = np.asarray([[-2, -2, -2], # Sobel kernel for y-direction # [1, 1, 1], # [1, 1, 1]]) y_kernel = np.asarray([ [-1, -1, -1], # Sobel kernel for y-direction [-1, -1, -1], [-1, -1, -1], [6, 6, 6], [-1, -1, -1], [-1, -1, -1], [-1, -1, -1] ]) #y_kernel = np.asarray([ # Sobel kernel for y-direction # [-1,-1], # [-1,-1], # [-1,-1], # [-1,-1], # [12,12], # [-1,-1], # [-1,-1], # [-1,-1], # [-1,-1], # [-1,-1], # [-1,-1], # [-1,-1], # [-1,-1], # ]) y_kernel = np.asarray([ # Sobel kernel for y-direction [-1, 0], [-1, 0], [-1, 0], [-1, 0], [28, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], ]) y_kernel = y_kernel / 9 gray = gray.astype(np.float) #sobel_x = signal.convolve2d(gray, x_kernel) # sobel_y = signal.convolve2d(gray, y_kernel) # convolve kernels over images sobel_y = np.clip(sobel_y + 10, 1, 254) sobel_y = cv2.medianBlur(sobel_y.astype(np.uint8), 5) sobel_y = cv2.GaussianBlur(sobel_y, (5, 5), 0) sobel_y = cv2.blur(sobel_y, (5, 5)) #sobel_y = cv2.GaussianBlur(sobel_y,(5,5),0) #sobel_y = cv2.bilateralFilter(sobel_y.astype(np.uint8),9,175,175) #find the start 4 point sobel_y = sobel_y[self.bias:H - self.bias, :] Img = Img[self.bias:H - self.bias, :] ave_line = self.calculate_the_average_line(sobel_y) peaks = self.aline.find_4peak(ave_line) Rever_img = 255 - sobel_y #start_point = 596 peaks = np.clip(peaks, 1, Rever_img.shape[0] - 1) #new_peak = np.zeros(4) #new_peak=peaks #new_peak[3] = peaks[2]+35 #peaks = new_peak if Manual_start_flag == True: peaks[1] = peaks[0] + 472 - 293 peaks[2] = peaks[0] + 500 - 293 peaks[3] = peaks[0] + 677 - 293 path1, path_cost1 = PATH.search_a_path(Rever_img, int(peaks[0])) path2, path_cost1 = PATH.search_a_path(Rever_img, int(peaks[1])) path3, path_cost1 = PATH.search_a_path(Rever_img, int(peaks[2])) path4, path_cost1 = PATH.search_a_path(Rever_img, int(peaks[3])) path1 = gaussian_filter1d(path1, 2) path2 = gaussian_filter1d(path2, 2) path3 = gaussian_filter1d(path3, 2) path4 = gaussian_filter1d(path4, 2) path4 = path3 #path2 = path3 path3 = path2 + 35 path2 = path2 - 5 path3, path_cost1 = PATH.search_a_path_based_on_path(Rever_img, path3) path3 = gaussian_filter1d(path3, 4) [path1, path2, path3, path4] = np.clip([path1, path2, path3, path4], 0, sobel_y.shape[0] - 1) for i in range(len(path1)): sobel_y[int(path1[i]), i] = 254 sobel_y[int(path2[i]), i] = 254 sobel_y[int(path3[i]), i] = 254 sobel_y[int(path4[i]), i] = 254 Dark_boundaries = sobel_y * 0 [path1, path2, path3, path4] = np.clip([path1, path2, path3, path4], 0, Dark_boundaries.shape[0] - 2) for i in range(len(path1)): Dark_boundaries[int(path1[i]), i] = 254 Dark_boundaries[int(path2[i]), i] = 220 Dark_boundaries[int(path3[i]), i] = 199 Dark_boundaries[int(path4[i]), i] = 180 [path1, path2, path3, path4] = np.clip([path1, path2, path3, path4], 0, Img.shape[0] - 2) for i in range(Img.shape[1]): Img[int(path1[i]) + 1, i, :] = Img[int(path1[i]), i, :] = [254, 0, 0] Img[int(path2[i]) + 1, i, :] = Img[int(path2[i]), i, :] = [0, 254, 0] Img[int(path3[i]) + 1, i, :] = Img[int(path3[i]), i, :] = [0, 0, 254] Img[int(path4[i]) + 1, i, :] = Img[int(path4[i]), i, :] = [0, 0, 0] if self.display_flag == True: cv2.imshow('revert', Rever_img.astype(np.uint8)) #cv2.imshow('path on blur',gray.astype(np.uint8)) cv2.imshow('Seg2', Img) #sobel_y = sobel_y*0.1 #edges = cv2.Canny(gray,50, 300,10) cv2.imshow('seg', sobel_y.astype(np.uint8)) cv2.imwrite(self.savedir_path + str(1) + ".jpg", sobel_y.astype(np.uint8)) cv2.waitKey(1) return Img, sobel_y, Dark_boundaries, [path1, path2, path3, path4]