Exemplo n.º 1
0
def convert(nav_acq):
  save_folder=nav_acq+"_8bit/"

  if not os.path.isdir(save_folder):
    os.makedirs(save_folder)
        
  for dirpath, dirnames, filenames in os.walk(nav_acq):
    for file_j in filenames:
      print file_j
      file_org = os.path.join(dirpath, file_j)
      file_new = os.path.join(save_folder, file_j)
      if (file_j[-3:]=='raw') and 'mask' not in file_j:

        if (file_j[-4:]=='zraw'):
          file_org_mhd=file_org.replace('zraw','mhd')
          file_new_mhd=file_new.replace('zraw','mhd')
        else:
          file_org_mhd=file_org.replace('raw','mhd')
          file_new_mhd=file_new.replace('raw','mhd')

        org_file_mhd = open(file_org_mhd)
        new_file_mhd = open(file_new_mhd,'w')
        convert=False

        for line in org_file_mhd:
          if 'ElementNumberOfChannels = 3' in line:
            newline=''
            convert=True
          elif 'ElementSize' in line:
            newline=''            
          elif 'DimSize' in line:
            newline=line
            temp=line.split()
            if len(temp) == 5:
                dimSize=[int(temp[2]),int(temp[3]),int(temp[4])]
            elif len(temp) == 4:
                dimSize=[int(temp[2]),int(temp[3])]
           
          else:
            newline =line
          new_file_mhd.write(newline)
        new_file_mhd.close()
        org_file_mhd.close()

        dimSize=np.hstack([3,dimSize])
        if convert:
          raw_file = CxDataHandler.rawFileReader(file_org,dimSize)
          data=raw_file.get_samples()
          data=np.mean(data,axis=0)
          rawWrither = CxDataHandler.rawFileWrither(file_new,data)
        else:
          shutil.copy2(file_org, file_new)

      elif (file_j[-3:]=='mhd') and not ('mask' in file_j):
        continue
      else:
        shutil.copy2(file_org, file_new)
Exemplo n.º 2
0
def RemoveRect(nav_acq):
    if nav_acq[-1]=='/':
        nav_acq=nav_acq[:-1]
    if os.path.isdir(nav_acq+"_removed_rect"):
        print "Folder already exists"
        return
    shutil.copytree(nav_acq,nav_acq+"_removed_rect")

    CxData=CxDataHandler.cxOpenCV.from_acq_folder(nav_acq+"_removed_rect")

    for frame_no in range(0,CxData.get_no_of_frames()):
        print frame_no
        vtk=CxData.load_mhd(frame_no)
        data=vtk.loadRawData_removeRect()
        CxDataHandler.rawFileWrither(vtk.getRawFilePath(),data,True)
        vtk.save_to_new_file(vtk.getFilePath(),overwrite=True)
def anonymizeFace(filePathMhd):
    if RM_FACE_CUT_OFF <= 0:
        return

    cutOff = 0.15


    mhdFile=CxDataHandler.mhdFile(filePathMhd)
    if 'Modality'not in mhdFile.Params:
        if not 'MR'in filePathMhd:
            return
    elif not mhdFile.Params['Modality'] == 'MR':
        return
    elif not mhdFile.get_DimSize() == 3:
        pass        
        #return
    
    print filePathMhd
    data=mhdFile.get_samples()
    
   
    X=[]
    Y=[]
    Z=[]
    for x in range(data.shape[0]):
        for y in range(data.shape[1]):
            for z in range(data.shape[2]):
                if data[x,y,z]>1:
                    X.append(x)
                    Y.append(y)
                    Z.append(z)
    
    x_com=np.mean(X)
    y_com=np.mean(Y)
    z_com=np.mean(Z)

    def avgElAboveLimit(plane,limit=10):
        res=0
        for i in range(plane.shape[0]):
            for j in range(plane.shape[1]):
                    if plane[i,j]>limit:
                        res=res+1
        return res/float(plane.shape[0]*plane.shape[1])
        
        
    def findN(vec):
        temp=None
        k=0
        for el in vec:
            if not temp and el > 0.015:
                temp=k
            if temp and el >cutOff:
                return k
                return k-temp
            k=k+1

        
    x=[]
    for i in range(data.shape[0]):
        temp=avgElAboveLimit(data[i,:,:])
        x.append(temp)
    x1=findN(x)            
    x2=findN(x[::-1])

    y=[]        
    for i in range(data.shape[1]):
        temp=avgElAboveLimit(data[:,i,:])
        y.append(temp)
    y1=findN(y)            
    y2=findN(y[::-1])   

    z=[]
    z1=None
    z2=None
    for i in range(data.shape[2]):
        temp=avgElAboveLimit(data[:,:,i])
        z.append(temp)
    z1=findN(z)            
    z2=findN(z[::-1])

    import matplotlib.pyplot as plt    
    
    plt.plot(x)
    plt.show()  
    print x1,x2
    plt.plot(y)
    plt.show()  
    print y1,y2
    plt.plot(z)
    plt.show()  
    print z1,z2

    print
    print x_com
    print y_com
    print z_com
    
    
    I00=0
    I11=0
    I22=0
    I01=0
    I12=0
    I02=0
    
    Ixx=0
    Iyy=0
    Izz=0
    
    for i in range(len(X)):
        I00=I00+(Y[i]-y_com)**2+(Z[i]-z_com)**2
        I11=I11+(X[i]-x_com)**2+(Z[i]-z_com)**2
        I22=I22+(X[i]-x_com)**2+(Y[i]-y_com)**2
        I01=I01+(X[i]-x_com)*(Y[i]-y_com)
        I12=I12+(Y[i]-y_com)*(Z[i]-z_com)
        I02=I02+(X[i]-x_com)*(Z[i]-z_com)
        
        Ixx=Ixx+(X[i]-x_com)*(X[i]-x_com)
        Iyy=Iyy+(Y[i]-y_com)*(Y[i]-y_com)
        Izz=Izz+(Z[i]-z_com)*(Z[i]-z_com)


    I00=I00/len(X)
    I11=I11/len(X)
    I22=I22/len(X)
    I01=I01/len(X)
    I12=I12/len(X)
    I02=I02/len(X)
    
    Ixx=Ixx/len(X)    
    Iyy=Iyy/len(X)    
    Izz=Izz/len(X)
    
    print
    
    print Ixx
    print Iyy
    print Izz
    
    print    
    
    print I00
    print I11
    print I22
    print
    print I01
    print I12
    print I02    
    

    return 

    if idx==0 and lvec(idx)>0: 
        N=int(RM_FACE_CUT_OFF/mhdFile.get_spacing()[0])
        data[0:N,:,:]=0
    if idx==0 and lvec(idx)<0:
        N=int(RM_FACE_CUT_OFF/mhdFile.get_spacing()[0])
        data[-N:,:,:]=0
    if idx==1 and lvec(idx)>0:
        N=int(RM_FACE_CUT_OFF/mhdFile.get_spacing()[1])
        data[:,0:N,:]=0
    if idx==1 and lvec(idx)<0:
        N=int(RM_FACE_CUT_OFF/mhdFile.get_spacing()[1])
        data[:,-N:,:]=0
    if idx==2 and lvec(idx)>0:
        N=int(RM_FACE_CUT_OFF/mhdFile.get_spacing()[2])
        data[:,:,0:N]=0
    if idx==2 and lvec(idx)<0:
        N=int(RM_FACE_CUT_OFF/mhdFile.get_spacing()[2])
        data[:,:,-N:]=0
        
    CxDataHandler.rawFileWrither(mhdFile.getRawFilePath(),data,color=False,ElementType=mhdFile.get_ElementType())
Exemplo n.º 4
0
def convert(nav_acq):
    save_folder = nav_acq + "_8bit/"

    if not os.path.isdir(save_folder):
        os.makedirs(save_folder)

    for dirpath, dirnames, filenames in os.walk(nav_acq):
        for file_j in filenames:
            print file_j
            file_org = os.path.join(dirpath, file_j)
            file_new = os.path.join(save_folder, file_j)
            if (file_j[-3:] == 'raw') and 'mask' not in file_j:

                if (file_j[-4:] == 'zraw'):
                    file_org_mhd = file_org.replace('zraw', 'mhd')
                    file_new_mhd = file_new.replace('zraw', 'mhd')
                else:
                    file_org_mhd = file_org.replace('raw', 'mhd')
                    file_new_mhd = file_new.replace('raw', 'mhd')

                org_file_mhd = open(file_org_mhd)
                new_file_mhd = open(file_new_mhd, 'w')
                convert = False

                for line in org_file_mhd:
                    if 'ElementNumberOfChannels = 3' in line:
                        newline = ''
                        convert = True
                    elif 'ElementSize' in line:
                        newline = ''
                    elif 'DimSize' in line:
                        newline = line
                        temp = line.split()
                        if len(temp) == 5:
                            dimSize = [
                                int(temp[2]),
                                int(temp[3]),
                                int(temp[4])
                            ]
                        elif len(temp) == 4:
                            dimSize = [int(temp[2]), int(temp[3])]

                    else:
                        newline = line
                    new_file_mhd.write(newline)
                new_file_mhd.close()
                org_file_mhd.close()

                dimSize = np.hstack([3, dimSize])
                if convert:
                    raw_file = CxDataHandler.rawFileReader(file_org, dimSize)
                    data = raw_file.get_samples()
                    data = np.mean(data, axis=0)
                    rawWrither = CxDataHandler.rawFileWrither(file_new, data)
                else:
                    shutil.copy2(file_org, file_new)

            elif (file_j[-3:] == 'mhd') and not ('mask' in file_j):
                continue
            else:
                shutil.copy2(file_org, file_new)
Exemplo n.º 5
0
def convert(nav_acq):
    if nav_acq[-1] == '/':
        save_folder = nav_acq[0:-1] + "_png/"
    else:
        save_folder = nav_acq + "_png/"

    print save_folder

    if not os.path.isdir(save_folder):
        os.makedirs(save_folder)
    just_positive = True
    for dirpath, dirnames, filenames in os.walk(nav_acq):
        for file_j in filenames:
            if "Velocity" not in file_j:
                continue
            print file_j
            file_org = os.path.join(dirpath, file_j)
            file_new = os.path.join(save_folder,
                                    file_j.replace('.zraw', '.raw'))
            if (file_j[-4:] == 'zraw') and 'mask' not in file_j:

                file_org_mhd = file_org[0:-4] + 'mhd'

                org_file_mhd = open(file_org_mhd)

                convert = False

                channels = 1

                for line in org_file_mhd:
                    if 'ElementNumberOfChannels = 3' in line:
                        channels = 3
                    elif 'ElementType' in line:
                        temp = line.split()
                        ElementType = temp[-1]
                    elif 'DimSize' in line:
                        temp = line.split()
                        if len(temp) == 5:
                            dimSize = [
                                int(temp[2]),
                                int(temp[3]),
                                int(temp[4])
                            ]
                        elif len(temp) == 4:
                            dimSize = [int(temp[2]), int(temp[3])]
                org_file_mhd.close()

                if channels == 3:
                    dimSize = np.hstack([3, dimSize])
                raw_file = CxDataHandler.rawFileReader(file_org,
                                                       dimSize,
                                                       ElementType=ElementType)
                data = raw_file.get_samples()
                idx_n = data < 0
                if sum(sum(data < 0)) == 0:
                    just_positive = False
                idx_p = data > 0
                data = data * 0 + 125
                data[idx_n] = 0
                data[idx_p] = 255
                # rawWrither = CxDataHandler.rawFileWrither(file_new,data,ElementType=ElementType)
                print data.shape
    if just_positive:
        print("Only positive values")
Exemplo n.º 6
0
def convert(nav_acq):
  if nav_acq[-1] == '/':
        save_folder=nav_acq[0:-1]+"_raw/"
  else:
        save_folder=nav_acq+"_raw/"

  print save_folder


  if not os.path.isdir(save_folder):
    os.makedirs(save_folder)
        
  for dirpath, dirnames, filenames in os.walk(nav_acq):
    for file_j in filenames:
      print file_j
      file_org = os.path.join(dirpath, file_j)
      file_new = os.path.join(save_folder, file_j.replace('.zraw','.raw'))
      if (file_j[-4:]=='zraw') and 'mask' not in file_j:

        file_org_mhd=file_org[0:-4] + 'mhd'
        file_new_mhd=file_new[0:-3] + 'mhd'

        org_file_mhd = open(file_org_mhd)
        new_file_mhd = open(file_new_mhd,'w')
        convert=False

        channels=1

        for line in org_file_mhd:
          if 'ElementNumberOfChannels = 3' in line:
            newline=line
            channels=3
          elif 'CompressedData ' in line:
            newline='CompressedData = False\n'            
          elif 'CompressedDataSize' in line:
            newline=''
          elif '.raw' in line:
            newline=line.replace('zraw','raw')
          elif 'ElementType' in line:
            temp=line.split()
            ElementType=temp[-1]
            newline=line
          elif 'DimSize' in line:
            newline=line
            temp=line.split()
            if len(temp) == 5:
                dimSize=[int(temp[2]),int(temp[3]),int(temp[4])]
            elif len(temp) == 4:
                dimSize=[int(temp[2]),int(temp[3])]
          elif 'ElementDataFile' in line:
            newline = 'ElementDataFile = ' + file_j.replace('.zraw','.raw')
          else:
            newline =line
          new_file_mhd.write(newline)
        new_file_mhd.close()
        org_file_mhd.close()

        if channels == 3:
            dimSize=np.hstack([3,dimSize])        
    
        raw_file = CxDataHandler.rawFileReader(file_org,dimSize,ElementType=ElementType)
        data=raw_file.get_samples()
        rawWrither = CxDataHandler.rawFileWrither(file_new,data,ElementType=ElementType)
     
      elif (file_j[-3:]=='mhd') and not ('mask' in file_j):
        continue
      else:
        shutil.copy2(file_org, file_new)
Exemplo n.º 7
0
def convert(nav_acq):
    if nav_acq[-1] == '/':
        save_folder = nav_acq[0:-1] + "_png/"
    else:
        save_folder = nav_acq + "_png/"

    print save_folder

    if not os.path.isdir(save_folder):
        os.makedirs(save_folder)

    for dirpath, dirnames, filenames in os.walk(nav_acq):
        for file_j in filenames:
            if "Velocity" not in file_j:
                continue
            print file_j
            file_org = os.path.join(dirpath, file_j)
            file_new = os.path.join(save_folder,
                                    file_j.replace('.zraw', '.png'))
            if (file_j[-4:] == 'zraw') and 'mask' not in file_j:

                file_org_mhd = file_org[0:-4] + 'mhd'

                org_file_mhd = open(file_org_mhd)

                convert = False

                channels = 1

                for line in org_file_mhd:
                    if 'ElementNumberOfChannels = 3' in line:
                        channels = 3
                    elif 'ElementType' in line:
                        temp = line.split()
                        ElementType = temp[-1]
                    elif 'DimSize' in line:
                        temp = line.split()
                        if len(temp) == 5:
                            dimSize = [
                                int(temp[2]),
                                int(temp[3]),
                                int(temp[4])
                            ]
                        elif len(temp) == 4:
                            dimSize = [int(temp[2]), int(temp[3])]
                org_file_mhd.close()

                if channels == 3:
                    dimSize = np.hstack([3, dimSize])
                raw_file = CxDataHandler.rawFileReader(file_org,
                                                       dimSize,
                                                       ElementType=ElementType)
                data = raw_file.get_samples()
                if channels == 3:
                    data = np.swapaxes(data, 0, 2)
                    plt.imshow(data)
                else:
                    data = np.swapaxes(data, 0, 1)
                    idx_n = data < 0
                    idx_p = data > 0
                    data = data * 0 + 125
                    data[idx_n] = 0
                    data[idx_p] = 255
                    plt.imshow(data, cmap="gray")

                print data.shape
                plt.savefig(file_new)
                plt.close()
Exemplo n.º 8
0
def convert(nav_acq):
    if nav_acq[-1] == '/':
        save_folder = nav_acq[0:-1] + "_raw/"
    else:
        save_folder = nav_acq + "_raw/"

    print save_folder

    if not os.path.isdir(save_folder):
        os.makedirs(save_folder)

    for dirpath, dirnames, filenames in os.walk(nav_acq):
        for file_j in filenames:
            print file_j
            file_org = os.path.join(dirpath, file_j)
            file_new = os.path.join(save_folder,
                                    file_j.replace('.zraw', '.raw'))
            if (file_j[-4:] == 'zraw') and 'mask' not in file_j:

                file_org_mhd = file_org[0:-4] + 'mhd'
                file_new_mhd = file_new[0:-3] + 'mhd'

                org_file_mhd = open(file_org_mhd)
                new_file_mhd = open(file_new_mhd, 'w')
                convert = False

                channels = 1

                for line in org_file_mhd:
                    if 'ElementNumberOfChannels = 3' in line:
                        newline = line
                        channels = 3
                    elif 'CompressedData ' in line:
                        newline = 'CompressedData = False\n'
                    elif 'CompressedDataSize' in line:
                        newline = ''
                    elif '.raw' in line:
                        newline = line.replace('zraw', 'raw')
                    elif 'ElementType' in line:
                        temp = line.split()
                        ElementType = temp[-1]
                        newline = line
                    elif 'DimSize' in line:
                        newline = line
                        temp = line.split()
                        if len(temp) == 5:
                            dimSize = [
                                int(temp[2]),
                                int(temp[3]),
                                int(temp[4])
                            ]
                        elif len(temp) == 4:
                            dimSize = [int(temp[2]), int(temp[3])]
                    elif 'ElementDataFile' in line:
                        newline = 'ElementDataFile = ' + file_j.replace(
                            '.zraw', '.raw')
                    else:
                        newline = line
                    new_file_mhd.write(newline)
                new_file_mhd.close()
                org_file_mhd.close()

                if channels == 3:
                    dimSize = np.hstack([3, dimSize])

                raw_file = CxDataHandler.rawFileReader(file_org,
                                                       dimSize,
                                                       ElementType=ElementType)
                data = raw_file.get_samples()
                rawWrither = CxDataHandler.rawFileWrither(
                    file_new, data, ElementType=ElementType)

            elif (file_j[-3:] == 'mhd') and not ('mask' in file_j):
                continue
            else:
                shutil.copy2(file_org, file_new)
Exemplo n.º 9
0
def anonymizeFace(filePathMhd):
    if RM_FACE_CUT_OFF <= 0:
        return

    cutOff = 0.15

    mhdFile = CxDataHandler.mhdFile(filePathMhd)
    if 'Modality' not in mhdFile.Params:
        if not 'MR' in filePathMhd:
            return
    elif not mhdFile.Params['Modality'] == 'MR':
        return
    elif not mhdFile.get_DimSize() == 3:
        pass
        #return

    print filePathMhd
    data = mhdFile.get_samples()

    X = []
    Y = []
    Z = []
    for x in range(data.shape[0]):
        for y in range(data.shape[1]):
            for z in range(data.shape[2]):
                if data[x, y, z] > 1:
                    X.append(x)
                    Y.append(y)
                    Z.append(z)

    x_com = np.mean(X)
    y_com = np.mean(Y)
    z_com = np.mean(Z)

    def avgElAboveLimit(plane, limit=10):
        res = 0
        for i in range(plane.shape[0]):
            for j in range(plane.shape[1]):
                if plane[i, j] > limit:
                    res = res + 1
        return res / float(plane.shape[0] * plane.shape[1])

    def findN(vec):
        temp = None
        k = 0
        for el in vec:
            if not temp and el > 0.015:
                temp = k
            if temp and el > cutOff:
                return k
                return k - temp
            k = k + 1

    x = []
    for i in range(data.shape[0]):
        temp = avgElAboveLimit(data[i, :, :])
        x.append(temp)
    x1 = findN(x)
    x2 = findN(x[::-1])

    y = []
    for i in range(data.shape[1]):
        temp = avgElAboveLimit(data[:, i, :])
        y.append(temp)
    y1 = findN(y)
    y2 = findN(y[::-1])

    z = []
    z1 = None
    z2 = None
    for i in range(data.shape[2]):
        temp = avgElAboveLimit(data[:, :, i])
        z.append(temp)
    z1 = findN(z)
    z2 = findN(z[::-1])

    import matplotlib.pyplot as plt

    plt.plot(x)
    plt.show()
    print x1, x2
    plt.plot(y)
    plt.show()
    print y1, y2
    plt.plot(z)
    plt.show()
    print z1, z2

    print
    print x_com
    print y_com
    print z_com

    I00 = 0
    I11 = 0
    I22 = 0
    I01 = 0
    I12 = 0
    I02 = 0

    Ixx = 0
    Iyy = 0
    Izz = 0

    for i in range(len(X)):
        I00 = I00 + (Y[i] - y_com)**2 + (Z[i] - z_com)**2
        I11 = I11 + (X[i] - x_com)**2 + (Z[i] - z_com)**2
        I22 = I22 + (X[i] - x_com)**2 + (Y[i] - y_com)**2
        I01 = I01 + (X[i] - x_com) * (Y[i] - y_com)
        I12 = I12 + (Y[i] - y_com) * (Z[i] - z_com)
        I02 = I02 + (X[i] - x_com) * (Z[i] - z_com)

        Ixx = Ixx + (X[i] - x_com) * (X[i] - x_com)
        Iyy = Iyy + (Y[i] - y_com) * (Y[i] - y_com)
        Izz = Izz + (Z[i] - z_com) * (Z[i] - z_com)

    I00 = I00 / len(X)
    I11 = I11 / len(X)
    I22 = I22 / len(X)
    I01 = I01 / len(X)
    I12 = I12 / len(X)
    I02 = I02 / len(X)

    Ixx = Ixx / len(X)
    Iyy = Iyy / len(X)
    Izz = Izz / len(X)

    print

    print Ixx
    print Iyy
    print Izz

    print

    print I00
    print I11
    print I22
    print
    print I01
    print I12
    print I02

    return

    if idx == 0 and lvec(idx) > 0:
        N = int(RM_FACE_CUT_OFF / mhdFile.get_spacing()[0])
        data[0:N, :, :] = 0
    if idx == 0 and lvec(idx) < 0:
        N = int(RM_FACE_CUT_OFF / mhdFile.get_spacing()[0])
        data[-N:, :, :] = 0
    if idx == 1 and lvec(idx) > 0:
        N = int(RM_FACE_CUT_OFF / mhdFile.get_spacing()[1])
        data[:, 0:N, :] = 0
    if idx == 1 and lvec(idx) < 0:
        N = int(RM_FACE_CUT_OFF / mhdFile.get_spacing()[1])
        data[:, -N:, :] = 0
    if idx == 2 and lvec(idx) > 0:
        N = int(RM_FACE_CUT_OFF / mhdFile.get_spacing()[2])
        data[:, :, 0:N] = 0
    if idx == 2 and lvec(idx) < 0:
        N = int(RM_FACE_CUT_OFF / mhdFile.get_spacing()[2])
        data[:, :, -N:] = 0

    CxDataHandler.rawFileWrither(mhdFile.getRawFilePath(),
                                 data,
                                 color=False,
                                 ElementType=mhdFile.get_ElementType())