def main():
    print('\n')
    print('##############################################')
    print('##############################################')
    print('####                                      ####')
    print('####         DOWNSAMPLE SINOGRAM          ####')
    print('####                                      ####')      
    print('##############################################')
    print('##############################################')  
    print('\n') 


    ##  Get input arguments
    args = getArgs()


    
    ##  Read input sinogram
    pathin = args.pathin
    if pathin[len(pathin)-1] != '/':
        pathin += '/'

    filename = args.sino
    sino = io.readImage( pathin + filename )
    nang , npix = sino.shape

    print('\nInput path:\n', pathin)
    print('\nInput sinogram:\n', filename)
    print('Sinogram size: ', nang,' ang  X  ', npix,' pixels')


    
    ##  Get input sinogram
    factor = int( args.factor )
    print('\nDownsampling factor: ', factor)



    ##  Downsample in pixels
    sino_down = sino[:,::factor]

    if args.plot is True:
        sino_list = [ sino , sino_down ]
        title_list = [ 'Original sinogram' , 'Undersampled sinogram' ]
        dis.plot_multi( sino_list , title_list )



    ##  Write image
    if args.pathout is None:
        pathout = pathin
    else:
        pathout = args.pathout

    fileout = filename[:len(filename)-4] + '_downsampl' + str( factor ) + '.DMP'
    io.writeImage( pathout + fileout , sino_down )
                 
    print('\n')
def main():
    print('\n')
    print('##############################################')
    print('##############################################')
    print('####                                      ####')
    print('####         DOWNSAMPLE SINOGRAM          ####')
    print('####                                      ####')      
    print('##############################################')
    print('##############################################')  
    print('\n') 


    ##  Get input arguments
    args = getArgs()


    
    ##  Read input sinogram
    pathin = args.pathin
    if pathin[len(pathin)-1] != '/':
        pathin += '/'

    filename = args.sino
    sino = io.readImage( pathin + filename )
    nang , npix = sino.shape

    print('\nInput path:\n', pathin)
    print('\nInput sinogram:\n', filename)
    print('Sinogram size: ', nang,' ang  X  ', npix,' pixels')


    
    ##  Getting projection geometry  
    angles = create_projection_angles( args , nang )



    ##  Downsample in angles
    sino_down , angles_down = downsample_sinogram_angles( sino , angles , args )


    
    ##  Display check plot
    if args.plot is True:
        sino_list = [ sino , sino_down ]
        title_list = [ 'Original sinogram' , 'Undersampled sinogram' ]
        dis.plot_multi( sino_list , title_list )



    ##  Write image & angle list
    write_output_files( sino_down , angles_down , args , pathin , filename )
                 
    print('\n')
示例#3
0
def main():
    print('\n')
    print('#######################################')
    print('#######################################') 
    print('###                                 ###')
    print('###   STRUCTURAL SIMILARITY INDEX   ###')
    print('###                                 ###')
    print('#######################################')
    print('#######################################') 
    print('\n')

    
    
    ##  Get input arguments
    args = getArgs()



    ## Get oracle image
    currDir = os.getcwd()
    image1 = io.readImage( args.image1 )
    image1 = image1.astype( myfloat )

    print('\nReading reference image:\n', args.image1)
    print('Image shape: ', image1.shape)


    image_list = []
    results = []

    
    
    ##  CASE OF SINGLE IMAGE TO ANALYZE
    if args.image2 is not None:
        if args.image2.find( ':' ) == -1:
            image_list.append( args.image2 )
            image2 = io.readImage( args.image2 )  # image2 --> image to analyze
            image2 = image2.astype(myfloat)
            num_img = 1

            print('\nReading image to analyze:\n', args.image2)
            print('Image shape: ', image2.shape)


            ## Get time in which the prgram starts to run
            time1 = time.time()      


            ##  Scale image to analyze with respect to the reference one
            if args.scaling is True:
                print('\nPerforming linear regression ....')
                image2 =  proc.linear_regression( image1 , image2 )


            ##  Register images
            if args.register is True:
                print('\nPerforming registration of the image to analize ....')
                image2 = proc.image_registration( image2 , image1 , 'ssd' )


            ##  Crop resolution circle of the images
            if args.resol_circle is True:
                print('\nSelecting the resolution circle')
                image1 = proc.select_resol_square( image1 )
                image2 = proc.select_resol_square( image2 )                

            ##  Crop images if enabled
            if args.roi is not None:
                roi = args.roi

                if roi.find( ':' ) != -1:
                    roi = roi.split(',')
                    p0 = [int(roi[0].split(':')[1]),int(roi[0].split(':')[0])]
                    p1 = [int(roi[1].split(':')[1]),int(roi[1].split(':')[0])]
                
                    print('Cropping rectangular ROI with vertices:  ( ', \
                            p0[0],' , ', p0[1], ')   ( ', p1[0],' , ',p1[1], ')')
                
                    image1 = proc.crop_image( image1 , p0 , p1 )
                    image2 = proc.crop_image( image2 , p0 , p1 )

                else:
                    print('\nUsing pixels specified in file:\n', roi) 
                    pixels = np.loadtxt( roi )
                    pixels = pixels.astype( int )
                    p0 = np.array([pixels[0,0],pixels[0,1]])
                    p1 = np.array([pixels[len(pixels)-1,0],pixels[len(pixels)-1,1]])       
                        
                    print('Cropping rectangular ROI with vertices:  ( ', \
                            p0[0],' , ', p0[1], ')   ( ', p1[0],' , ',p1[1], ')')
                
                    image1 = proc.crop_image( image1 , p0 , p1 )
                    image2 = proc.crop_image( image2 , p0 , p1 )


            ##  Compute the gradient of the images, if enabled
            if args.gradient is True:
                image1 = compute_gradient_image( image1 )
                image2 = compute_gradient_image( image2 )


            ##  Check whether the 2 images have the same shape
            if image1.shape != image2.shape:
                sys.error('\nERROR: The input images have different shapes!\n')


            ##  Plot to check whether the images have the same orientation
            if args.plot is True:
                print('\nPlotting images to check orientation ....')
                img_list = [ image1 , image2 ]
                title_list = [ 'Oracle image' , 'Image to analyze' ]
                dis.plot_multi( img_list , title_list , 'Check plot' )        


            ##  Get window size
            window_size = args.window
            print('\nSize of the computation window: ', window_size)
            
            if window_size % 2 != 0:
                window_size += 1
                print('Window size is even: window size changed to ', window_size)
            
            
            ##  Get sigma of the gaussian kernel
            sigma = SIGMA
            print('Sigma of the gaussian kernel: ', sigma)


            ## Calculate map of SSIM values
            map_ssim , MSSIM = compute_map_ssim( image1 , image2 , window_size , sigma )
            results.append( MSSIM )

            if args.plot is True:
                print('\nPlotting images + map of ssim ....')
                img_list = [ image1 , image2 , map_ssim ]
                title_list = [ 'Oracle image' , 'Image to analyze' , 'Map of SSIM' ]
                dis.plot_multi( img_list , title_list , 'Images and map of SSIM' , colorbar=True )

            
            ##  Save SSIM map 
            filename = args.image2[:len(args.image2)-4] + '_ssim_map.png'
            io.writeImage( filename , map_ssim )


        
        ##  CASE OF MULTIPLE SPECIFIC IMAGES
        else:
            image_list = args.image2.split(':')
            img_list = [ ]
            title_list = [ ]
            num_img = len( image_list )

            for im in range( num_img ):
                img_file = image_list[im]
                image1 = io.readImage( args.image1 )
                image2 = io.readImage( img_file )  # image2 --> image to analyze
                image2 = image2.astype(myfloat)
                print('\nReading image to analyze:\n', args.image2)
                print('Image shape: ', image2.shape)

                
                ##  Get time in which the prgram starts to run
                time1 = time.time()      


                ##  Scale image to analyze with respect to the reference one
                if args.scaling is True:
                    print('\nPerforming linear regression ....')
                    image2 =  proc.linear_regression( image1 , image2 )


                ##  Register images
                if args.register is True:
                    print('\nPerforming registration of the image to analize ....') 
                    image2 = proc.image_registration( image2 , image1 , 'ssd' ) 


                ##  Crop resolution circle of the images
                if args.resol_circle is True:
                    print('\nSelecting the resolution circle')
                    image1 = proc.select_resol_square( image1 )
                    image2 = proc.select_resol_square( image2 )

                
                ##  Crop images if enabled
                if args.roi is not None:
                    roi = args.roi

                    if args.roi.find(',') != -1:
                        roi = roi.split(',')
                        p0 = [int(roi[0].split(':')[1]),int(roi[0].split(':')[0])]
                        p1 = [int(roi[1].split(':')[1]),int(roi[1].split(':')[0])]
                
                        print('Cropping rectangular ROI with vertices:  ( ', \
                                p0[0],' , ', p0[1], ')   ( ', p1[0],' , ',p1[1], ')')
                
                        image1 = proc.crop_image( image1 , p0 , p1 )
                        image2 = proc.crop_image( image2 , p0 , p1 )

                    else:
                        print('\nUsing pixels specified in file:\n', roi) 
                        pixels = np.loadtxt( roi )
                        pixels = pixels.astype( int )
                        p0 = np.array([pixels[0,0],pixels[0,1]])
                        p1 = np.array([pixels[len(pixels)-1,0],pixels[len(pixels)-1,1]])       
                        
                        print('Cropping rectangular ROI with vertices:  ( ', \
                                p0[0],' , ', p0[1], ')   ( ', p1[0],' , ',p1[1], ')')
                
                        image1 = proc.crop_image( image1 , p0 , p1 )
                        image2 = proc.crop_image( image2 , p0 , p1 )     
                

                ##  Compute the gradient of the images, if enabled
                if args.gradient is True:
                    image1 = compute_gradient_image( image1 )
                    image2 = compute_gradient_image( image2 )


                ##  Check whether the 2 images have the same shape
                if image1.shape != image2.shape:
                    sys.exit('\nERROR: The input images have different shapes!\n')


                ##  Plot to check whether the images have the same orientation
                if args.plot is True:
                    print('\nPlotting images to check orientation ....')
                    img_list2 = [ image1 , image2 ]
                    title_list2 = [ 'Oracle image' , 'Image to analyze' ]
                    dis.plot_multi( img_list2 , title_list2 , 'Check plot' )   

                
                ##  Get window size
                window_size = args.window
                print('\nSize of the computation window: ', window_size)
                
                if window_size % 2 != 0:
                    window_size += 1
                    print('Window size is even: window size changed to ', window_size)
                
                
                ##  Get sigma of the gaussian kernel
                sigma = SIGMA
                print('Sigma of the gaussian kernel: ', sigma)


                ##  Calculate map of SSIM values
                map_ssim , MSSIM = compute_map_ssim( image1 , image2 , window_size , sigma )
                results.append( MSSIM )

                map_ssim[ map_ssim < 0 ] = 0.0

                if args.plot is True:
                    img_list.append( map_ssim )
                    title_list.append( 'SSIM map n.' + str( im + 1 ) )

                
                ##  Save SSIM map 
                filename = img_file[:len(img_file)-4] + '_ssim_map.png'
                io.writeImage( filename , map_ssim )
                
            if args.plot is True:
                print('\nPlotting images + map of ssim ....')
                dis.plot( img_list[0] )
                dis.plot( img_list[1] )
                dis.plot_multi_colorbar( img_list , title_list , 'Maps of SSIM' ) 



    ##  CASE OF BUNCH OF IMAGES TO ANALYZE 
    else:
        os.chdir(args.path)
        image_list = sorted( glob.glob('*') )
        num_images = len( image_list )
        img_list.append( image1 )
        title_list.append('Oracle image') 

        
        ##  Get time in which the prgram starts to run
        time1 = time.time()


        ##  Loop on all the images to analyze
        for i in range( num_img ):
            image1 = io.readImage( args.image1 ) 
            image2 = io.readImage( image_list[i] )
            image2 = image2.astype(myfloat)

            print('\n\n\nIMAGE TO ANALYZE NUMBER: ', i)
            print('\nReading image to analyze:\n', fileIn[i])
            print('Image shape: ', image2.shape)

            
            ##  Scale image to analyze with respect to the reference one
            if args.scaling is True:
                print('\nPerforming linear regression ....')  
                image2 = proc.linear_regression( image1 , image2 )


            ##  Register images
            if args.register is True:
                print('\nPerforming registration of the image to analize ....') 
                image2 = proc.image_registration( image2 , image1 , 'ssd' )


            ##  Crop resolution circle of the images
            if args.resol_circle is True:
                print('\nSelecting the resolution circle')
                image1 = proc.select_resol_square( image1 )
                image2 = proc.select_resol_square( image2 ) 


            ##  Crop images if enabled
            if args.roi is not None:
                roi = args.roi

                if args.roi.find(',') != -1:
                    roi = roi.split(',')
                    p0 = [int(roi[0].split(':')[1]),int(roi[0].split(':')[0])]
                    p1 = [int(roi[1].split(':')[1]),int(roi[1].split(':')[0])]
                
                    print('Cropping rectangular ROI with vertices:  ( ', \
                            p0[0],' , ', p0[1], ')   ( ', p1[0],' , ',p1[1], ')')
                
                    image1 = proc.crop_image( image1 , p0 , p1 )
                    image2 = proc.crop_image( image2 , p0 , p1 )

                else:
                    print('\nUsing pixels specified in file:\n', roi) 
                    pixels = np.loadtxt( roi )
                    pixels = pixels.astype( int )
                    p0 = np.array([pixels[0,0],pixels[0,1]])
                    p1 = np.array([pixels[len(pixels)-1,0],pixels[len(pixels)-1,1]])       
                        
                    print('Cropping rectangular ROI with vertices:  ( ', \
                            p0[0],' , ', p0[1], ')   ( ', p1[0],' , ',p1[1], ')')
                
                    image1 = proc.crop_image( image1 , p0 , p1 )
                    image2 = proc.crop_image( image2 , p0 , p1 )    


            ##  Compute the gradient of the images, if enabled
            if args.gradient is True:
                image1 = compute_gradient_image( image1 )
                image2 = compute_gradient_image( image2 )


            ##  Check whether the 2 images have the same shape
            if image1.shape != image2.shape and args.roi is None:
                sys.error('\nERROR: The input images have different shapes!\n')


            ##  Plot to check whether the images have the same orientation
            if args.plot is True:
                print('\nPlotting images to check orientation ....')
                img_list = [ image1 , image2 ]
                title_list = [ 'Oracle image' , 'Image to analyze' ]
                dis.plot_multi( img_list , title_list , 'Check plot' )    


            ##  Get window size
            window_size = args.window
            print('\nSize of the computation window: ', window_size)
            
            if window_size % 2 != 0:
                window_size += 1
                print('Window size is even: window size changed to ', window_size)
            
            
            ##  Get sigma of the gaussian kernel
            sigma = SIGMA
            print('Sigma of the gaussian kernel: ', sigma)


            ##  Calculate map of SSIM values
            map_ssim,MSSIM = compute_map_ssim( image1 , image2 , window_size , sigma )
            results.append( MSSIM )


            ##  Diplay map of SSIM
            if args.plot is True:
                fig = plt.figure()
                plt.title('Map of SSIM indeces')
                plt.imshow(map_ssim,cmap = cm.Greys_r)
                plt.colorbar()
                plt.show()


            ##  Save SSIM map
            filename = image_list[i][:len(image_list[i])-4] + '_ssim_map.png'
            io.writeImage( filename , map_ssim )
        os.chdir(currDir)



    ##  Summary print of the results
    print('\n\nSUMMARY OF THE RESULTS:\n')
    print('\nReference image:\n', args.image1)

    for i in range( num_img ):
        print('\nTest image number ', i,'\n', image_list[i])
        print('SSIM = ' , results[i])



    ##  Get time elapsed for the run of the program
    time2 = time.time() - time1
    print('\n\nTime elapsed for the calculation: ', time2)



    ##  Write log file
    write_log_file( args , image_list , results )


    print('\n\n')
def compute_lir( args ):
    ##  Get input images
    inputs = args.inputs
    file1 , file2 , file3 = inputs.split( ':' )
    rec_ref = io.readImage( file1 )
    rec_imp = io.readImage( file2 )
    imp_pos = np.loadtxt( file3 ).astype( int )
    npix = rec_ref.shape[0]
    nimp = imp_pos.shape[0]


    ##  Display input images
    if args.plot is True:
        image_imp = np.zeros( ( npix , npix ) )
        image_list = [ rec_ref , rec_imp ]
        title_list = [ 'Reference reconstruction' ,
                       'Impulsed reconstruction'  ]
        dis.plot_multi( image_list , title_list )


    ##  Compute image difference
    rec_diff = rec_imp - rec_ref


    ##  Resize image
    zoom_factor = args.zoom
    rec_diff = zoom( rec_diff , zoom_factor )

    if args.plot is True:
        dis.plot( rec_diff , 'Difference image x' + str( zoom_factor ), ')')


    ##  Compute FWHM for each impulse and get an average of them
    fwhm = np.zeros( nimp , dtype=myfloat )
    denom = zoom_factor

    for i in range( nimp ):
        x0 = int( zoom_factor * imp_pos[i,0] );  y0 = int( zoom_factor * imp_pos[i,1] )
        square = rec_diff[x0-r:x0+r,y0-r:y0+r]
        hh = np.max( square ) * 0.5
        aux = np.argwhere( square >= hh )
        fwhm[i] = np.sqrt( len( aux ) ) / denom

    FWHM = np.sum( fwhm ) / myfloat( nimp )
    print('\nFWHM values:\n')
    pp.printArray( fwhm )
    print('\nFWHM average: ', FWHM)
    print('\n')

    
    ##  Write log file of the results
    namebase = file1
    pathout = None
    
    if namebase.find( '/' ) != -1:
        namebase = namebase.split( '/' )
        filename = namebase[len(namebase)-1]
        filename = filename[:len(filename)-4]
        for i in range( len(namebase)-1 ):
            if i == 0:
                pathout = '/' + namebase[i] 
            else:
                pathout += namebase[i] + '/'
    else:
        filename = namebase
        filename = filename[:len(filename)-4]
        pathout = './'

    print('\nWriting files ...')

    fileout = pathout + filename + '_lir_diff.DMP'
    io.writeImage( fileout , rec_diff )
    print( fileout )

    fileout = pathout + filename + '_lir_analysis.txt'
    fp = open( fileout , 'w' )
    fp.write('\n')
    fp.write('\n###########################################')
    fp.write('\n###########################################') 
    fp.write('\n###                                     ###')
    fp.write('\n###   LOCAL IMPULSE RESPONSE ANALYSIS   ###')
    fp.write('\n###                                     ###')
    fp.write('\n###########################################')
    fp.write('\n###########################################') 
    fp.write('\n')
    today = datetime.datetime.now()
    fp.write('\nLIR calculation performed on the ' + str( today ))
    fp.write('\n\nNumbers of impulses: ' + str( nimp ))
    fp.write('\n\nAverage FWHM: ' + str( FWHM ))
    print( fileout )
    print( '\n' )
def create_dataset( args ):
    ##  Get input image
    image_ref = io.readImage( args.inputs )
    npix = image_ref.shape[0]


    ##  Create impulsed image
    nimp = int( np.floor( npix / nimp_factor ) )
    c = npix * 0.5
    ind_imp = np.zeros( ( nimp , 2 ) , dtype=int )
    i = 0

    print('\nSelecting impulse locations ...')

    while i < nimp:
        x = int( np.round( np.random.rand() * npix ) )
        y = int( np.round( np.random.rand() * npix ) )
        dist1 = np.ceil( np.sqrt( ( x - c )**2 + ( y - c )**2 ) )

        if dist1 < c:
            if i != 0:
                for j in range( i + 1 ):
                    x0 = ind_imp[j,0];  y0 = ind_imp[j,1]
                    dist2 = np.ceil( np.sqrt( ( x - x0 )**2 + ( y - y0 )**2 ) )
                    if dist2 <= r:
                        break
                    elif dist2 > r and j < i:
                        continue
                    elif dist2 > r and j == i:
                        ind_imp[i,0] = x;  ind_imp[i,1] = y
                        i += 1
            else:
                ind_imp[i,0] = x;  ind_imp[i,1] = y 
                i += 1

    print('... locations selected!')

    print('\nImpulse locations:')
    pp.printArray2D( ind_imp  )
    
    image_imp = image_ref.copy()
    imp_factor = args.imp_factor

    for i in range( len( ind_imp ) ):
        x0 = ind_imp[i][0];  y0 = ind_imp[i][1] 
        image_imp[x0,y0] += image_imp[x0,y0] * imp_factor


    ##  Create sinograms
    nang = int( np.ceil( npix * np.pi * 0.5 ) )
    angles = myfloat( np.arange( nang ) / myfloat( nang ) * 180.0 )
    image_ref = myfloat( image_ref );  image_imp = myfloat( image_imp )

    sino_ref = grid.forwproj( image_ref , angles )
    sino_imp = grid.forwproj( image_imp , angles )


    ##  Check plots
    if args.plot is True:
        image_list = [ image_ref , image_imp ]
        title_list = [ 'Reference image' , 'Impulsed image' ]
        dis.plot_multi( image_list , title_list )

        image_list = [ sino_ref , sino_imp ]
        title_list = [ 'Sinogram of reference image' , 'Sinogram of impulsed image' ]
        dis.plot_multi( image_list , title_list )


    ##  Write output files
    namebase = args.inputs
    
    if namebase.find( '/' ) != -1:
        namebase = namebase.split( '/' )
        filename = namebase[len(namebase)-1]
        filename = filename[:len(filename)-4]
        for i in range( len(namebase)-1 ):
            if i == 0:
                pathout = '/' + namebase[i]
            else:
                pathout += namebase[i] + '/' 
    else:
        filename = namebase
        filename = filename[:len(filename)-4]
        pathout = './'

    print('\nWriting output files ...')

    fileout = pathout + filename + '_lir.DMP'
    io.writeImage( fileout , image_imp )
    print( fileout )

    fileout = pathout + filename + '_sin.DMP'
    io.writeImage( fileout , sino_ref )
    print( fileout )
                         
    fileout = pathout + filename + '_lir_sin.DMP'
    io.writeImage( fileout , sino_imp )
    print( fileout )

    fileout = pathout + filename + '_lir.txt'
    np.savetxt( fileout , ind_imp , newline= '\n' )
    print( fileout )
    print('\n')
示例#6
0
def main():
    print('\n')
    print('#######################################')
    print('#######################################') 
    print('###                                 ###')
    print('###         MEAN SQUARE ERROR       ###')
    print('###                                 ###')
    print('#######################################')
    print('#######################################') 
    print('\n')

    
    
    ##  Get input arguments
    args = getArgs()


    
    ##  Get oracle image 
    currDir = os.getcwd()
    image1 = io.readImage( args.image1 )
    image1 = image1.astype(myfloat)
    
    print('\nReading reference image:\n', args.image1)
    print('Image shape: ', image1.shape)


    image_list = []
    results = []

    
    
    ##  CASE OF SINGLE IMAGE TO ANALYZE
    if args.image2 is not None:
        if args.image2.find( ':' ) == -1:
            image_list.append( args.image2 )
            image2 = io.readImage( args.image2 ) 
            image2 = image2.astype(myfloat)
            num_img = 1
            
            print('\nReading image to analyze:\n', args.image2)
            print('Image shape: ', image2.shape)


            ##  Get time in which the prgram starts to run
            time1 = time.time()      


            ##  Scale image to analyze with respect to the reference one
            if args.scaling is True:
                print('\nPerforming linear regression ....')
                image2 =  proc.linear_regression( image1 , image2 )


            ##  Register images
            if args.register is True:
                print('\nPerforming registration of the image to analize ....')
                image2 = proc.image_registration( image2 , image1 , 'ssd' )


            ##  Crop resolution circle of the images
            if args.resol_circle is True:
                print('\nSelecting the resolution circle')
                image1 = proc.select_resol_square( image1 )
                image2 = proc.select_resol_square( image2 )                


            ##  Crop images if enabled
            if args.roi is not None:
                roi = args.roi

                if args.roi.find(',') != -1:
                    roi = roi.split(',')
                    p0 = [int(roi[0].split(':')[1]),int(roi[0].split(':')[0])]
                    p1 = [int(roi[1].split(':')[1]),int(roi[1].split(':')[0])]
                
                    print('Cropping rectangular ROI with vertices:  ( ', \
                            p0[0],' , ', p0[1], ')   ( ', p1[0],' , ',p1[1], ')')
                
                    image1 = proc.crop_image( image1 , p0 , p1 )
                    image2 = proc.crop_image( image2 , p0 , p1 )

                else:
                    print('\nUsing pixels specified in file:\n', roi)
                    file_roi = open( roi , 'r' )
                    pixels = np.loadtxt( file_roi )
                    image1 = image1[pixels[:,0],pixels[:,1]]
                    image2 = image2[pixels[:,0],pixels[:,1]]
                    num_pix = len( image1 )
                    fact = factors( num_pix )
                    image1 = image1.reshape( fact[0] , int( num_pix/fact[0] ) )
                    image2 = image2.reshape( fact[0] , int( num_pix/fact[0] ) )   


            ##  Compute the gradient of the images, if enabled
            if args.gradient is True:
                image1 = compute_gradient_image( image1 )
                image2 = compute_gradient_image( image2 )      


            ##  Check whether the 2 images have the same shape
            if image1.shape != image2.shape:
                sys.error('\nERROR: The input images have different shapes!\n')


            ##  Plot to check whether the images have the same orientation
            if args.plot is True:
                print('\nPlotting images to check orientation ....')
                img_list = [ image1 , image2 ]
                title_list = [ 'Oracle image' , 'Image to analyze' ]
                dis.plot_multi( img_list , title_list , 'Check plot' )    


            ##  Compute figures of merit
            SNR = calc_snr( image1 , image2 )
            PSNR = calc_psnr( image1 , image2 )
            RMSE = calc_rmse( image1 , image2 )
            MAE = calc_rmse( image1 , image2 ) 

            results.append( np.array( [ SNR , PSNR , RMSE , MAE ] ) )



        ##  CASE OF MULTIPLE SPECIFIC IMAGES
        else:
            image_list = args.image2.split(':')
            img_list = [ ]
            title_list = [ ]
            num_img = len( image_list ) 

            for im in range( num_img ):
                img_file = image_list[im]
                image1 = io.readImage( args.image1 )
                image2 = io.readImage( img_file )  # image2 --> image to analyze
                image2 = image2.astype(myfloat)
                print('\nReading image to analyze:\n', args.image2)
                print('Image shape: ', image2.shape)


                ##  Get time in which the prgram starts to run
                time1 = time.time()      


                ## Scale image to analyze with respect to the reference one
                if args.scaling is True:
                    print('\nPerforming linear regression ....')
                    image2 =  proc.linear_regression( image1 , image2 )


                ## Register images
                if args.register is True:
                    print('\nPerforming registration of the image to analize ....') 
                    image2 = proc.image_registration( image2 , image1 , 'ssd' ) 


                ##  Crop resolution circle of the images
                if args.resol_circle is True:
                    print('\nSelecting the resolution circle')
                    image1 = proc.select_resol_square( image1 )
                    image2 = proc.select_resol_square( image2 )


                ##  Crop images if enabled
                if args.roi is not None:
                    roi = args.roi

                    if args.roi.find(',') != -1:
                        roi = roi.split(',')
                        p0 = [int(roi[0].split(':')[1]),int(roi[0].split(':')[0])]
                        p1 = [int(roi[1].split(':')[1]),int(roi[1].split(':')[0])]
                
                        print('Cropping rectangular ROI with vertices:  ( ', \
                                p0[0],' , ', p0[1], ')   ( ', p1[0],' , ',p1[1], ')')
                
                        image1 = proc.crop_image( image1 , p0 , p1 )
                        image2 = proc.crop_image( image2 , p0 , p1 )

                    else:
                        print('\nUsing pixels specified in file:\n', roi) 
                        file_roi = open( roi , 'r' )
                        pixels = np.loadtxt( file_roi )
                        pixels = pixels.astype( int )

                        image1 = image1[pixels[:,0],pixels[:,1]]
                        image2 = image2[pixels[:,0],pixels[:,1]]
                        num_pix = len( image1 )
                        fact = factors( num_pix )
                        image1 = image1.reshape( fact[0] , int( num_pix/fact[0] ) )
                        image2 = image2.reshape( fact[0] , int( num_pix/fact[0] ) )       


                ##  Compute the gradient of the images, if enabled
                if args.gradient is True:
                    image1 = compute_gradient_image( image1 )
                    image2 = compute_gradient_image( image2 )      
                
                
                ##  Check whether the 2 images have the same shape
                if image1.shape != image2.shape:
                    sys.exit('\nERROR: The input images have different shapes!\n')


                ##  Plot to check whether the images have the same orientation
                if args.plot is True:
                    print('\nPlotting images to check orientation ....')
                    img_list2 = [ image1 , image2 ]
                    title_list2 = [ 'Oracle image' , 'Image to analyze' ]
                    dis.plot_multi( img_list2 , title_list2 , 'Check plot' )


                ##  Compute figures of merit
                SNR = calc_snr( image1 , image2 )
                PSNR = calc_psnr( image1 , image2 )
                RMSE = calc_rmse( image1 , image2 )
                MAE = calc_rmse( image1 , image2 )

                results.append( np.array( [ SNR , PSNR , RMSE , MAE ] ) )                                  



    ##  CASE OF BUNCH OF IMAGES TO ANALYZE 
    else:
        os.chdir(args.path)
        image_list = sorted(glob.glob('*'))
        num_img = len(fileIn)

        ## Get time in which the prgram starts to run
        time1 = time.time()


        ## Loop on all the images to analyze
        for i in range( num_img ):
            image1 = io.readImage( args.image1 )
            image2 = io.readImage( image_list[i] )
            image2 = image2.astype(myfloat)
            print('\n\n\nIMAGE TO ANALYZE NUMBER: ', i)
            print('\nReading image to analyze:\n', image_list[i])
            print('Image shape: ', image2.shape)

            if args.fileout is not None:
                fileout.write('\nReading image to analyze:\n' + fileIn[i])    


            ## Scale image to analyze with respect to the reference one
            if args.scaling is True:
                print('\nPerforming linear regression ....')
                image2 =  proc.linear_regression( image1 , image2 )


            ## Register images
            if args.register is True:
                print('\nPerforming registration of the image to analize ....') 
                image2 = proc.image_registration( image2 , image1 , 'ssd' )  


            ##  Crop resolution circle of the images
            if args.resol_circle is True:
                print('\nSelecting the resolution circle')
                image1 = proc.select_resol_square( image1 )
                image2 = proc.select_resol_square( image2 )


            ##  Crop images if enabled
            if args.roi is not None:
                roi = args.roi

                if args.roi.find(',') != -1:
                    roi = roi.split(',')
                    p0 = [int(roi[0].split(':')[1]),int(roi[0].split(':')[0])]
                    p1 = [int(roi[1].split(':')[1]),int(roi[1].split(':')[0])]
                
                    print('Cropping rectangular ROI with vertices:  ( ', \
                            p0[0],' , ', p0[1], ')   ( ', p1[0],' , ',p1[1], ')')
                
                    image1 = proc.crop_image( image1 , p0 , p1 )
                    image2 = proc.crop_image( image2 , p0 , p1 )

                else:
                    print('\nUsing pixels specified in file:\n', roi) 
                    file_roi = open( roi , 'r' )
                    pixels = np.loadtxt( file_roi )
                    pixels = pixels.astype( int )

                    image1 = image1[pixels[:,0],pixels[:,1]]
                    image2 = image2[pixels[:,0],pixels[:,1]]
                    num_pix = len( image1 )
                    fact = factors( num_pix )
                    image1 = image1.reshape( fact[0] , int( num_pix/fact[0] ) )
                    image2 = image2.reshape( fact[0] , int( num_pix/fact[0] ) ) 


                ##  Check whether the 2 images have the same shape
                if image1.shape != image2.shape:
                    sys.exit('\nERROR: The input images have different shapes!\n')


            ##  Compute the gradient of the images, if enabled
            if args.gradient is True:
                image1 = compute_gradient_image( image1 )
                image2 = compute_gradient_image( image2 )


            ##  Plot to check whether the images have the same orientation
            if args.plot is True and args.roi.find(',') != -1:
                print('\nPlotting images to check orientation ....')
                img_list2 = [ image1 , image2 ]
                title_list2 = [ 'Oracle image' , 'Image to analyze' ]
                dis.plot_multi( img_list2 , title_list2 , 'Check plot' )       


            ##  Compute figures of merit
            SNR = calc_snr( image1 , image2 )
            PSNR = calc_psnr( image1 , image2 )
            RMSE = calc_rmse( image1 , image2 )
            MAE = calc_rmse( image1 , image2 )

            results.append( np.array( [ SNR , PSNR , RMSE , MAE ] ) )  


        os.chdir(currDir)



    ##  Summary print of the results
    print('\n\nSUMMARY OF THE RESULTS:\n')
    print('\nReference image:\n', args.image1)

    for i in range( num_img ):
        print('\nTest image number ', i,'\n', image_list[i])
        print('SNR = ' , results[i][0])
        print('PSNR = ' , results[i][1])   
        print('MRSE = ' , results[i][2])   
        print('MAE = ' , results[i][3])   



    ##  Get time elapsed for the run of the program
    time2 = time.time() - time1
    print('\n\nTime elapsed for the calculation: ', time2)



    ##  Write log file
    write_log_file( args , image_list , results )

    print('\n\n')