caffee学习——图像切割

来源:互联网 发布:扑克游戏源码 编辑:程序博客网 时间:2024/06/05 22:54

参考使用https://github.com/dajunC/DSB3Tutorial

最近在使用caffee进行Deep learning的图像处理学习。学习过程中,需要对图片进行切割,用以消除不需要的训练杂质。通过查找大牛的方法,找到一个很好的方案。但大牛的方法是基于Tensorflow的,所以不能使用Keras库,但不影响图像切割的使用,只是需要一定的修改。

(1)对图像进行mask

from __future__ import print_function, divisionimport SimpleITK as sitkimport numpy as npimport osimport csvfrom glob import globimport pandas as pdtry:    from tqdm import tqdm # 进度条插件except:    print('TQDM does make much nicer wait bars...')    tqdm = lambda x: xdef make_mask(center,diam,z,width,height,spacing,origin):    '''Center : centers of circles px -- list of coordinates x,y,zdiam : diameters of circles px -- diameterwidthXheight : pixel dim of imagespacing = mm/px conversion rate np array x,y,zorigin = x,y,z mm np.arrayz = z position of slice in world coordinates mm    '''    mask = np.zeros([height,width]) # 0's everywhere except nodule swapping x,y to match img    #convert to nodule space from world coordinates    # Defining the voxel range in which the nodule falls    v_center = (center-origin)/spacing    v_diam = int(diam/spacing[0]+5)    v_xmin = np.max([0,int(v_center[0]-v_diam)-5])    v_xmax = np.min([width-1,int(v_center[0]+v_diam)+5])    v_ymin = np.max([0,int(v_center[1]-v_diam)-5])     v_ymax = np.min([height-1,int(v_center[1]+v_diam)+5])    v_xrange = range(v_xmin,v_xmax+1)    v_yrange = range(v_ymin,v_ymax+1)    # Convert back to world coordinates for distance calculation    x_data = [x*spacing[0]+origin[0] for x in range(width)]    y_data = [x*spacing[1]+origin[1] for x in range(height)]    # Fill in 1 within sphere around nodule    for v_x in v_xrange:        for v_y in v_yrange:            p_x = spacing[0]*v_x + origin[0]            p_y = spacing[1]*v_y + origin[1]            if np.linalg.norm(center-np.array([p_x,p_y,z]))<=diam:                mask[int((p_y-origin[1])/spacing[1]),int((p_x-origin[0])/spacing[0])] = 1.0    return(mask)def matrix2int16(matrix):    ''' matrix must be a numpy array NXNReturns uint16 version    '''    m_min= np.min(matrix)    m_max= np.max(matrix)    matrix = matrix-m_min    return(np.array(np.rint( (matrix-m_min)/float(m_max-m_min) * 65535.0),dtype=np.uint16))############## Getting list of image filesluna_path = "F:/test/"luna_subset_path = luna_path+"dataset/"output_path = "F:/test/output/"file_list=glob(luna_subset_path+"*.mhd")####################### Helper function to get rows in data frame associated # with each filedef get_filename(file_list, case):    for f in file_list:        if case in f:            return(f)## The locations of the nodesdf_node = pd.read_csv(luna_path+"test.csv")df_node["file"] = df_node["seriesuid"].map(lambda file_name: get_filename(file_list, file_name))df_node = df_node.dropna()####### Looping over the image files#for fcount, img_file in enumerate(tqdm(file_list)):    mini_df = df_node[df_node["file"]==img_file] #get all nodules associate with file    if mini_df.shape[0]>0: # some files may not have a nodule--skipping those         # load the data once        itk_img = sitk.ReadImage(img_file)         img_array = sitk.GetArrayFromImage(itk_img) # indexes are z,y,x (notice the ordering)        num_z, height, width = img_array.shape        #heightXwidth constitute the transverse plane        origin = np.array(itk_img.GetOrigin())      # x,y,z  Origin in world coordinates (mm)        spacing = np.array(itk_img.GetSpacing())    # spacing of voxels in world coor. (mm)        # go through all nodes (why just the biggest?)        for node_idx, cur_row in mini_df.iterrows():                   node_x = cur_row["coordX"]            node_y = cur_row["coordY"]            node_z = cur_row["coordZ"]            diam = cur_row["diameter_mm"]            # just keep 3 slices            imgs = np.ndarray([3,height,width],dtype=np.float32)            masks = np.ndarray([3,height,width],dtype=np.uint8)            center = np.array([node_x, node_y, node_z])   # nodule center            v_center = np.rint((center-origin)/spacing)  # nodule center in voxel space (still x,y,z ordering)            for i, i_z in enumerate(np.arange(int(v_center[2])-1,                             int(v_center[2])+2).clip(0, num_z-1)): # clip prevents going out of bounds in Z                mask = make_mask(center, diam, i_z*spacing[2]+origin[2],                                 width, height, spacing, origin)                masks[i] = mask                imgs[i] = img_array[i_z]            np.save(os.path.join(output_path,"images_%04d_%04d.npy" % (fcount, node```````idx)),imgs)            np.save(os.path.join(output_path,"masks_%04d_%04d.npy" % (fcount, node_idx)),masks)
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 地包天矫正期间嘴歪了怎么办 5个月的宝宝歪胫怎么办 三个多月的宝宝有点斜颈怎么办 5月宝宝有点斜颈偏向左侧怎么办 外斜术后变周期性内斜怎么办 右腰劳损休息一个多月还痛怎么办 宝宝鼻子不通气用嘴呼吸怎么办 窦性心律过速怎么办st-t改变 跑步机跑带上撒油跑起来滑怎么办 娃儿上嘴唇里面破了怎么办图片 支付宝添加好友点忽略了怎么办 如果综合评价考了两间大学怎么办 国二报名时民族错了怎么办 屁股凹陷下去一块已经萎缩了怎么办 我家孩子上高中了英语不好怎么办 驼背脖子向前倾怎么办用背背佳 屁股大又肥是骨盆前倾怎么办 手术40多天引起小腿变细了怎么办 老师教育学生学生跑出教室你怎么办 有矛盾舍友发朋友圈攻击我怎么办 飞机杯送的润滑油很快用完了怎么办 人笨记性不好内向反应迟钝该怎么办 洗衣机滋生了大量的霉菌在怎么办? 爸妈给兄弟买的房子贵很多怎么办 输了十几万 不敢跟爸妈讲怎么办 赌博欠2万不敢和爸妈讲怎么办 学校凳子坏了丢了 现在要查怎么办 我想写字可就是写的不好看怎么办 我的字写的很丑怎么办 大母指腱鞘炎好了之后又犯了怎么办 被蚊子咬了好大一个包怎么办 欠我钱的人跑了怎么办 下面旁边破了沙的疼怎么办 协助民警执法时质疑辅警身份怎么办 跑步每次腿落地小腿骨头疼怎么办 两年义务兵完了想继续当兵怎么办 保研联系老师说名额已满怎么办 在部队报函授不发毕业证怎么办 济南自考信息修改后原成绩怎么办 自考本科档案一直在自己手里怎么办 想考警校可身高差两公分怎么办