Пример #1
0
    def __init__(self):
        #self.image_dir   = "../../OCT/beam_scanning/Data set/pic/NORMAL-BACKSIDE-center/"
        #self.roi_dir =  "../../OCT/beam_scanning/Data set/seg label/NORMAL-BACKSIDE-center/"
        #self.database_root = "../../OCT/beam_scanning/Data Set Reorganize/NORMAL/"
        #self.database_root = "../../OCT/beam_scanning/Data Set Reorganize/NORMAL-BACKSIDE-center/"
        #self.database_root = "../../OCT/beam_scanning/Data Set Reorganize/NORMAL-BACKSIDE/"
        # check the cuda device
        pth_save_dir = "../out/sheathCGAN_coordinates3/"
        # the portion of attated image to 2     sides
        self.attatch_rate = 0.2

        jason_tmp_dir = "D:/Deep learning/dataset/original/phantom/1/label/0.json"
        # read th jso fie in hte start :
        with open(jason_tmp_dir) as dir:
            self.jason_tmp = JSON.load(dir)
        self.shapeTmp = self.jason_tmp["shapes"]
        self.coordinates0 = self.jason_tmp["shapes"][1][
            "points"]  # remember add finding corred label 1!!!
        self.co_len = len(self.coordinates0)

        #self.database_root = "D:/Deep learning/dataset/original/phantom/2/"
        #self.database_root = "D:/Deep learning/dataset/original/dots/3/"
        self.database_root = "D:/Deep learning/dataset/original/new_catheter_ruler/2/"
        self.database_root = "D:/Deep learning/dataset/original/phantom_2th_march_2021/1/"
        self.database_root = "D:/Deep learning/dataset/original/paper_with_strong_shadow/1/"
        self.database_root = "D:/Deep learning/dataset/original/animal_tissue/2/"

        #self.database_root = "D:/Deep learning/dataset/original/animal_tissue/1/"
        #self.database_root = "D:/Deep learning/dataset/original/IVOCT/1/"

        self.f_downsample_factor = 30
        self.all_dir = self.database_root + "pic_all/"
        self.image_dir = self.database_root + "pic/"
        self.json_dir = self.database_root + "label/"  # for this class sthis dir ist save the modified json
        self.json_save_dir = self.database_root + "label_generate/"
        self.img_num = 0

        self.contours_x = []  # no predefines # predefine there are 4 contours
        self.contours_y = []  # predefine there are 4 contours
        #self.seger = Seg_One_1()
        self.saver = Save_Contour_pkl()
        self.display_flag = True

        # deep learning model
        print(torch.cuda.current_device())
        print(torch.cuda.device(0))
        print(torch.cuda.device_count())
        print(torch.cuda.get_device_name(0))
        print(torch.cuda.is_available())
        torch.set_num_threads(2)
        gancreator = cGAN_build2.CGAN_creator(
        )  # the Cgan for the segmentation
        self.GANmodel = gancreator.creat_cgan()  #  G and D are created here
        # for the detection just use the Gnets
        self.GANmodel.netG.load_state_dict(
            torch.load(pth_save_dir + 'cGANG_epoch_2.pth'))
        self.GANmodel.netG.cuda()
Пример #2
0
    def __init__(self):
        #self.image_dir   = "../../OCT/beam_scanning/Data set/pic/NORMAL-BACKSIDE-center/"
        #self.roi_dir =  "../../OCT/beam_scanning/Data set/seg label/NORMAL-BACKSIDE-center/"
        #self.database_root = "../../OCT/beam_scanning/Data Set Reorganize/NORMAL/"
        #self.database_root = "../../OCT/beam_scanning/Data Set Reorganize/NORMAL-BACKSIDE-center/"
        #self.database_root = "../../OCT/beam_scanning/Data Set Reorganize/NORMAL-BACKSIDE/"

        self.database_root = "../../OCT/beam_scanning/Data Set Reorganize/VARY/"

        self.image_dir = self.database_root + "pic/"
        self.roi_dir = self.database_root + "seg label/"
        self.img_num = 0

        self.contours_x = []  # predefine there are 4 contours
        self.contours_y = []  # predefine there are 4 contours

        self.saver = Save_Contour_pkl()
        self.display_flag = True
Пример #3
0
    def __init__(self):
        #self.image_dir   = "../../OCT/beam_scanning/Data set/pic/NORMAL-BACKSIDE-center/"
        #self.roi_dir =  "../../OCT/beam_scanning/Data set/seg label/NORMAL-BACKSIDE-center/"
        #self.database_root = "../../OCT/beam_scanning/Data Set Reorganize/NORMAL/"
        #self.database_root = "../../OCT/beam_scanning/Data Set Reorganize/NORMAL-BACKSIDE-center/"
        #self.database_root = "../../OCT/beam_scanning/Data Set Reorganize/NORMAL-BACKSIDE/"
        sub_folder = "8/"
        self.database_root = "D:/Deep learning/dataset/label data/"

        self.image_dir = self.database_root + "img/" + sub_folder
        self.json_dir = self.database_root + "label/" + sub_folder
        self.save_dir = self.database_root + "seg label pkl/" + sub_folder
        self.img_num = 0

        self.contours_x = []  # no predefines # predefine there are 4 contours
        self.contours_y = []  # predefine there are 4 contours

        self.saver = Save_Contour_pkl()
        self.display_flag = True
Пример #4
0
class Read_read_check_json_label(object):
    def __init__(self):
        #self.image_dir   = "../../OCT/beam_scanning/Data set/pic/NORMAL-BACKSIDE-center/"
        #self.roi_dir =  "../../OCT/beam_scanning/Data set/seg label/NORMAL-BACKSIDE-center/"
        #self.database_root = "../../OCT/beam_scanning/Data Set Reorganize/NORMAL/"
        #self.database_root = "../../OCT/beam_scanning/Data Set Reorganize/NORMAL-BACKSIDE-center/"
        #self.database_root = "../../OCT/beam_scanning/Data Set Reorganize/NORMAL-BACKSIDE/"
        sub_folder = "8/"
        self.database_root = "D:/Deep learning/dataset/label data/"

        self.image_dir = self.database_root + "img/" + sub_folder
        self.json_dir = self.database_root + "label/" + sub_folder
        self.save_dir = self.database_root + "seg label pkl/" + sub_folder
        self.img_num = 0

        self.contours_x = []  # no predefines # predefine there are 4 contours
        self.contours_y = []  # predefine there are 4 contours

        self.saver = Save_Contour_pkl()
        self.display_flag = True

    def draw_coordinates_color(self, img1, vx, vy, color):

        if color == 0:
            painter = [254, 0, 0]
        elif color == 1:
            painter = [0, 254, 0]
        elif color == 2:
            painter = [0, 0, 254]
        else:
            painter = [0, 0, 0]
            #path0  = signal.resample(path0, W)
        H, W, _ = img1.shape
        for j in range(len(vx)):
            #path0l[path0x[j]]
            dy = np.clip(vy[j], 2, H - 2)
            dx = np.clip(vx[j], 2, W - 2)

            img1[int(dy) + 1, int(dx), :] = img1[int(dy), int(dx), :] = painter
            #img1[int(dy)+1,dx,:]=img1[int(dy)-1,dx,:]=img1[int(dy),dx,:]=painter

        return img1

    def check_one_folder(self):
        for i in os.listdir(self.json_dir):
            #for i in os.listdir("E:\\estimagine\\vs_project\\PythonApplication_data_au\\pic\\"):
            # separath  the name of json
            a, b = os.path.splitext(i)
            # if it is a json it will have corresponding image
            if b == ".json":
                img_path = self.image_dir + a + ".jpg"
                img1 = cv2.imread(img_path)
                if img1 is None:
                    print("no_img for this zip")
                else:
                    json_dir = self.json_dir + a + b
                    with open(json_dir) as f_dir:
                        data = JSON.load(f_dir)
                    shape = data["shapes"]
                    num_line = len(shape)
                    len_list = num_line
                    #with ZipFile(json_dir, 'r') as zipObj:
                    #       # Get list of files names in zip
                    #       #listOfiles = zipObj.namelist()
                    #       # this line of code is importanct sice the the formmer one will change the sequence
                    #       listOfiles = zipObj.infolist()
                    #       len_list = len(listOfiles)

                    #rois = read_roi_zip(roi_dir) # get all the coordinates
                    gray = cv2.cvtColor(
                        img1, cv2.COLOR_BGR2GRAY)  # transfer into gray image
                    H, W = gray.shape
                    max_buffer = np.zeros(len_list)
                    contoursx = [None] * len_list
                    contoursy = [None] * len_list

                    for iter in range(len_list):
                        # get the name of one contour
                        #  iter
                        coordinates = shape[iter]["points"]
                        coordinates = np.array(coordinates)
                        #line_name  = os.path.splitext(listOfiles[iter].filename)
                        #line_name =line_name[0] #just use the former one
                        #pathy = rois[line_name]['y']
                        #pathx = rois[line_name]['x']

                        # delete the coordinate out side the boundary of image
                        len_ori, _ = coordinates.shape
                        target = 0
                        for j in range(len_ori):

                            if (coordinates[target, 0] < 0
                                    or coordinates[target, 0] >
                                (W - 1)):  # check the x coordinates
                                coordinates = np.delete(coordinates,
                                                        target,
                                                        axis=0)
                            else:
                                target += 1

                        pathy = coordinates[:, 1]
                        pathx = coordinates[:, 0]

                        num_points = len(pathx)
                        path_w = pathx[num_points - 1] - pathx[0]
                        path_w = int(path_w)
                        # sometimes the contour is plot in reversed direction
                        if path_w < 0:
                            path_w = -path_w
                            pathy = pathy[::-1]
                            pathx = pathx[::-1]

                        pathyl = np.ones(int(path_w)) * np.nan

                        for j in range(num_points):
                            #importante sometimes the start point is nnot the lestmost
                            this_index = np.clip(pathx[j] - pathx[0], 0,
                                                 path_w - 1)
                            pathyl[int(this_index)] = float(pathy[j] - 1)
                        add_3 = np.append(pathyl[::-1], pathyl,
                                          axis=0)  # cascade
                        add_3 = np.append(add_3, pathyl[::-1],
                                          axis=0)  # cascade
                        s = pd.Series(add_3)
                        pathyl = s.interpolate(method="linear")
                        pathyl = pathyl[path_w:2 * path_w].to_numpy()
                        pathxl = np.arange(int(pathx[0]),
                                           int(pathx[0]) + path_w)

                        if len(pathxl
                               ) > 0.96 * W:  #  correct  the unperfect label
                            # rememver to add resacle later
                            pathyl = signal.resample(pathyl, W)
                            pathxl = np.arange(0, W)

                        contoursx[iter] = pathxl
                        contoursy[iter] = pathyl
                        #max_buffer[iter] = np.min(pathyl)  #  use the minimal value to detemine which contour as the 1st and 2nd .....
                        max_buffer[iter] = np.max(
                            pathyl
                        )  #  use the mean value to detemine which contour as the 1st and 2nd .....

                        pass

                    #  use the minimal value to detemine which contour as the 1st and 2nd .....
                    new_index = np.argsort(max_buffer)
                    self.contours_x = [None] * len_list
                    self.contours_y = [None] * len_list

                    for iter in range(len_list):
                        self.contours_x[iter] = contoursx[new_index[iter]]
                        self.contours_y[iter] = contoursy[new_index[iter]]
                        if self.display_flag == True:
                            img1 = self.draw_coordinates_color(
                                img1, self.contours_x[iter],
                                self.contours_y[iter], iter)

                    #save this result
                    self.img_num = a
                    #self.contours_x = [path0ln, path1ln, path2ln, path3ln]
                    #self.saver.append_new_name_contour (self.img_num,self.contours,self.database_root)
                    self.saver.append_new_name_contour(self.img_num,
                                                       self.contours_x,
                                                       self.contours_y,
                                                       self.save_dir)

                    cv2.imshow('pic', img1)
                    print(str(a))
                    cv2.waitKey(10)
Пример #5
0
class Read_read_check_ROI_label(object):
    def __init__(self):
        #self.image_dir   = "../../OCT/beam_scanning/Data set/pic/NORMAL-BACKSIDE-center/"
        #self.roi_dir =  "../../OCT/beam_scanning/Data set/seg label/NORMAL-BACKSIDE-center/"
        #self.database_root = "../../OCT/beam_scanning/Data Set Reorganize/NORMAL/"
        #self.database_root = "../../OCT/beam_scanning/Data Set Reorganize/NORMAL-BACKSIDE-center/"
        #self.database_root = "../../OCT/beam_scanning/Data Set Reorganize/NORMAL-BACKSIDE/"

        self.database_root = "../../OCT/beam_scanning/Data Set Reorganize/VARY/"

        self.image_dir = self.database_root + "pic/"
        self.roi_dir = self.database_root + "seg label/"
        self.img_num = 0

        self.contours_x = []  # predefine there are 4 contours
        self.contours_y = []  # predefine there are 4 contours

        self.saver = Save_Contour_pkl()
        self.display_flag = True

    def draw_coordinates_color(self, img1, vx, vy, color):

        if color == 0:
            painter = [254, 0, 0]
        elif color == 1:
            painter = [0, 254, 0]
        elif color == 2:
            painter = [0, 0, 254]
        else:
            painter = [0, 0, 0]
            #path0  = signal.resample(path0, W)
        H, W, _ = img1.shape
        for j in range(len(vx)):
            #path0l[path0x[j]]
            dy = np.clip(vy[j], 2, H - 2)
            dx = np.clip(vx[j], 2, W - 2)

            img1[int(dy) + 1, dx, :] = img1[int(dy), dx, :] = painter
            #img1[int(dy)+1,dx,:]=img1[int(dy)-1,dx,:]=img1[int(dy),dx,:]=painter

        return img1

    def check_one_folder(self):
        for i in os.listdir(self.roi_dir):
            #for i in os.listdir("E:\\estimagine\\vs_project\\PythonApplication_data_au\\pic\\"):
            # 分离文件名与后缀
            a, b = os.path.splitext(i)
            # 如果后缀名是“.xml”就旋转related的图像
            if b == ".zip" or b == ".ZIP":
                img_path = self.image_dir + a + ".jpg"
                img1 = cv2.imread(img_path)
                if img1 is None:
                    print("no_img for this zip")
                else:
                    roi_dir = self.roi_dir + a + b
                    with ZipFile(roi_dir, 'r') as zipObj:
                        # Get list of files names in zip
                        #listOfiles = zipObj.namelist()
                        # this line of code is importanct sice the the formmer one will change the sequence
                        listOfiles = zipObj.infolist()
                        len_list = len(listOfiles)

                    rois = read_roi_zip(roi_dir)  # get all the coordinates
                    gray = cv2.cvtColor(
                        img1, cv2.COLOR_BGR2GRAY)  # transfer into gray image
                    H, W = gray.shape
                    max_buffer = np.zeros(len_list)
                    contoursx = [None] * len_list
                    contoursy = [None] * len_list

                    for iter in range(len_list):
                        # get the name of one contour
                        #  iter
                        line_name = os.path.splitext(listOfiles[iter].filename)
                        line_name = line_name[0]  #just use the former one
                        pathy = rois[line_name]['y']
                        pathx = rois[line_name]['x']
                        num_points = len(pathx)
                        path_w = pathx[num_points - 1] - pathx[0]
                        # sometimes the contour is plot in reversed direction
                        if path_w < 0:
                            path_w = -path_w
                            pathy = pathy[::-1]
                            pathx = pathx[::-1]

                        pathyl = np.ones(path_w) * np.nan

                        for j in range(num_points):
                            #importante sometimes the start point is nnot the lestmost
                            this_index = np.clip(pathx[j] - pathx[0], 0,
                                                 path_w - 1)
                            pathyl[this_index] = float(pathy[j] - 1)
                        add_3 = np.append(pathyl[::-1], pathyl,
                                          axis=0)  # cascade
                        add_3 = np.append(add_3, pathyl[::-1],
                                          axis=0)  # cascade
                        s = pd.Series(add_3)
                        pathyl = s.interpolate()
                        pathyl = pathyl[path_w:2 * path_w].to_numpy()
                        pathxl = np.arange(pathx[0], pathx[num_points - 1])

                        contoursx[iter] = pathxl
                        contoursy[iter] = pathyl
                        max_buffer[iter] = np.min(pathyl)

                        pass

                    new_index = np.argsort(max_buffer)
                    self.contours_x = [None] * len_list
                    self.contours_y = [None] * len_list

                    for iter in range(len_list):
                        self.contours_x[iter] = contoursx[new_index[iter]]
                        self.contours_y[iter] = contoursy[new_index[iter]]
                        if self.display_flag == True:
                            img1 = self.draw_coordinates_color(
                                img1, self.contours_x[iter],
                                self.contours_y[iter], iter)

                    #save this result
                    self.img_num = a
                    #self.contours_x = [path0ln, path1ln, path2ln, path3ln]
                    #self.saver.append_new_name_contour (self.img_num,self.contours,self.database_root)
                    self.saver.append_new_name_contour(self.img_num,
                                                       self.contours_x,
                                                       self.contours_y,
                                                       self.database_root)

                    cv2.imshow('pic', img1)
                    print(str(a))
                    cv2.waitKey(10)
Пример #6
0
class  Read_read_check_ROI_label(object):
    def __init__(self ):
        #self.image_dir   = "../../OCT/beam_scanning/Data set/pic/NORMAL-BACKSIDE-center/"
        #self.roi_dir =  "../../OCT/beam_scanning/Data set/seg label/NORMAL-BACKSIDE-center/"
        self.database_root = "../../OCT/beam_scanning/Data Set Reorganize/NORMAL/"
        self.image_dir   = self.database_root + "pic/"
        self.roi_dir =  self.database_root + "seg label/"
        self.img_num = 0
        self.contours = []
        self.saver = Save_Contour_pkl()

    def check_one_folder (self):
        for i in os.listdir(self.roi_dir):
    #for i in os.listdir("E:\\estimagine\\vs_project\\PythonApplication_data_au\\pic\\"):
        # 分离文件名与后缀
            a, b = os.path.splitext(i)
            # 如果后缀名是“.xml”就旋转related的图像
            if b == ".zip"or b== ".ZIP":
                img_path = self.image_dir + a + ".jpg"
                img1 = cv2.imread(img_path)
                if img1 is None:
                    print ("no_img for this zip")
                else:
                    roi_dir = self.roi_dir + a  +b
                    with ZipFile(roi_dir, 'r') as zipObj:
                           # Get list of files names in zip
                           #listOfiles = zipObj.namelist()
                           # this line of code is importanct sice the the formmer one will change the sequence 
                           listOfiles = zipObj.infolist()

                           line_name0,_ = os.path.splitext(listOfiles[0].filename)
                           number0   = int(line_name0.split("-")[0])
                 
                           line_name1,_ = os.path.splitext(listOfiles[1].filename)
                           number1   = int(line_name1.split("-")[0])

                           line_name2,_ = os.path.splitext(listOfiles[2].filename)
                           number2   = int(line_name2.split("-")[0])

                           line_name3,_ = os.path.splitext(listOfiles[3].filename)
                           number3   = int(line_name3.split("-")[0])

                           number_list= [number0,number1,number2,number3]
                           new_index  = np.argsort( number_list)

                           name_list = [line_name0,line_name1,line_name2,line_name3]
                           line_name0 = name_list[new_index[0]]
                           line_name1 = name_list[new_index[1]]
                           line_name2 = name_list[new_index[2]]
                           line_name3 = name_list[new_index[3]]




                        
                    rois = read_roi_zip(roi_dir)
                     
                    gray  =   cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
                    H,W   = gray.shape
                    path0y = rois[line_name0]['y']
                    path0x = rois[line_name0]['x']
                    path0l =  np.ones(W) * np.nan
                    path1y = rois[line_name1]['y']
                    path1x = rois[line_name1]['x']
                    path1l =  np.ones(W) * np.nan
                    path2y = rois[line_name2]['y']
                    path2x = rois[line_name2]['x']
                    path2l =  np.ones(W) * np.nan
                    path3y = rois[line_name3]['y']
                    path3x = rois[line_name3]['x']
                    path3l =  np.ones(W) * np.nan
                    for j in range (len(path0x)):
                         this_index = np.clip(  path0x[j]-1, 0,W-1)
                         path0l[this_index] = float (path0y[j] -1)
                    for j in range (len(path1x)):
                         this_index = np.clip(  path1x[j]-1, 0,W-1)
                         path1l[this_index] = float (path1y[j] -1)
                    for j in range (len(path2x)):
                         this_index = np.clip(  path2x[j]-1, 0,W-1)
                         path2l[this_index] = float (path2y[j] -1)                 
                    for j in range (len(path3x)):
                         this_index = np.clip(  path3x[j]-1, 0,W-1)
                         path3l[this_index] = float (path3y[j] -1)

                    add_3   = np.append(path0l[::-1],path0l,axis=0) # cascade
                    add_3   = np.append(add_3,path0l[::-1],axis=0) # cascade
                    s = pd.Series(add_3)
                    path0ln = s.interpolate(  )
                    path0ln = path0ln[W:2*W].to_numpy() 

                    add_3   = np.append(path1l[::-1],path1l,axis=0) # cascade
                    add_3   = np.append(add_3,path1l[::-1],axis=0) # cascade
                    s = pd.Series(add_3)
                    path1ln = s.interpolate( )
                    path1ln = path1ln[W:2*W].to_numpy() 

                    add_3   = np.append(path2l[::-1],path2l,axis=0) # cascade
                    add_3   = np.append(add_3,path2l[::-1],axis=0) # cascade
                    s = pd.Series(add_3)
                    path2ln = s.interpolate(  )
                    path2ln = path2ln[W:2*W].to_numpy() 

                    add_3   = np.append(path3l[::-1],path3l,axis=0) # cascade
                    add_3   = np.append(add_3,path3l[::-1],axis=0) # cascade
                    s = pd.Series(add_3)
                    path3ln = s.interpolate(  )
                    path3ln = path3ln[W:2*W].to_numpy() 

                    #path0  = signal.resample(path0, W)
                    for j in range (len(path0ln)):
                         #path0l[path0x[j]]
                         img1[int(path0ln[j])+1,j,:]=img1[int(path0ln[j])-1,j,:]=img1[int(path0ln[j]),j,:]=[254,0,0]
                         img1[int(path1ln[j]+1),j,:]=img1[int(path1ln[j]-1),j,:]=img1[int(path1ln[j]),j,:]=[0,254,0]
                         img1[int(path2ln[j]+1),j,:]=img1[int(path2ln[j]-1),j,:]=img1[int(path2ln[j]),j,:]=[0,0,254]
                         img1[int(path3ln[j]+1),j,:]=img1[int(path3ln[j]-1),j,:]=img1[int(path3ln[j]),j,:]=[254,254,254]
                    
                    #save this result 
                    self.img_num = a
                    self.contours = [path0ln, path1ln, path2ln, path3ln]
                    self.saver.append_new_name_contour (self.img_num,self.contours,self.database_root)

                    cv2.imshow('pic',img1)
                    print(str(a))
                    cv2.waitKey(10)