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.linearRegression( image1 , image2 ) ## Register images if args.register is True: print('\nPerforming registration of the image to analize ....') image2 = proc.imageRegistration( image2 , image1 , 'ssd' ) ## Crop resolution circle of the images if args.resol_circle is True: print('\nSelecting the resolution circle') image1 = proc.selectResolutionSquare( image1 ) image2 = proc.selectResolutionSquare( 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.cropImage( image1 , p0 , p1 ) image2 = proc.cropImage( 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.cropImage( image1 , p0 , p1 ) image2 = proc.cropImage( 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 correlation = compute_correlation( 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] + '.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.linearRegression( image1 , image2 ) if im == 0: io.writeImage( 'pm_lg.DMP' , image2 ) if im == 1: io.writeImage( 'cdi_lg.DMP' , image2 ) ## Register images if args.register is True: print('\nPerforming registration of the image to analize ....') image2 = proc.imageRegistration( image2 , image1 , 'ssd' ) ## Crop resolution circle of the images if args.resol_circle is True: print('\nSelecting the resolution circle') image1 = proc.selectResolutionSquare( image1 ) image2 = proc.selectResolutionSquare( 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.cropImage( image1 , p0 , p1 ) image2 = proc.cropImage( 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.cropImage( image1 , p0 , p1 ) image2 = proc.cropImage( 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 = args.image2[:len(args.image2)-4] + '.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.linearRegression( image1 , image2 ) ## Register images if args.register is True: print('\nPerforming registration of the image to analize ....') image2 = proc.imageRegistration( image2 , image1 , 'ssd' ) ## Crop resolution circle of the images if args.resol_circle is True: print('\nSelecting the resolution circle') image1 = proc.selectResolutionSquare( image1 ) image2 = proc.selectResolutionSquare( 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.cropImage( image1 , p0 , p1 ) image2 = proc.cropImage( 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.cropImage( image1 , p0 , p1 ) image2 = proc.cropImage( 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 = args.image2[:len(args.image2)-4] + '.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 main(): print('\n') print('#############################################') print('############ SPECTRUM ANALYSIS ############') print('#############################################') print('\n') ## Get input arguments args = getArgs() ## Get number of pair of images image_string = args.images file_list = [] if image_string.find(':') != -1: file_list = image_string.split(':') num_img = len( file_list ) else: file_list.append( image_string ) num_img = 1 print('Number of images to analyze: ', num_img) print(file_list) ## Read input images and display them as check images = [] for i in range(num_img): image_name = file_list[i] print('Reading image: ', image_name) if args.resol_square : print('Calculation enabled in the resol square') images.append( proc.selectResolutionSquare( io.readImage( image_name ) ) ) else: print('Calculation enabled on the whole image square') images.append( io.readImage( image_name ) ) ## Check plot if args.plot is True: dis.plot( images[i] , 'Input image ' + str( i ) ) ''' images.append( np.arange( 64 ).reshape( 8 , 8 ) ) images.append( np.arange( 64 ).reshape( 8 , 8 ) ) print( '\n\nInput images:\n', images[0] ) ''' ## Get labels for plots labels = None if args.labels is not(None): labels = args.labels labels = labels.split(':') if ( len(labels) ) != num_img: sys.exit('\nERROR: Number of labels is not half number of input images!\n') ## Fourier ring correlation analysis spectrum_curves = [] label_curves = [] resol_point_list = [] for i in range(num_img): if args.labels is None: label_curves.append('Spectrum image ' + str( i ) ) label = 'Spectrum analysis image ' + str( i ) else: label_curves.append(labels[i]) label = 'Spectrum analysis image ' + labels[i] print('\nCalculating ring-spectrum of image:\n', file_list[i]) spectrum , spatial_freq = analysis_spectrum( images[i] , args , i , 'image '+ str(i) , label ) spectrum_curves.append( spectrum ) spectrum_curves = np.array( spectrum_curves ).reshape( num_img , len( spatial_freq ) ) ## Plot FRC curves title = 'Spectrum - comparison' plot_name = 'spectrum_comparison_curves' point = None style = 'lines' plot_spectrum_curves( spectrum_curves, spatial_freq, args, label_curves, title, point, style ) ## Write log file if args.fileout is not None: fileout = args.fileout fout = open(fileout,'w') print('Writing data in ',fileout,' ....') today = datetime.datetime.now() fout.write('######## SPECTRUM RESOLUTION ANALYSIS ########\n') fout.write('Calculation performed on the ' + str(today)) fout.write('\nResults of images inside:\n '+str(path)) if args.resol_square : fout.write('\nCalculation enabled in the resol square') else: fout.write('\nCalculation enabled on the whole image square') fout.write('\nCalculation enabled on the whole image square') for im in range( num_img ): fout.write('\n\nSpectrum analysis performed for:\n') fout.write( file_list[i] ) fout.write( file_list[i] ) if args.pixsize is None: fout.write('Resolution = ' + str(resol_point_list[im] )+' (pixels)\n\n') else: fout.write('Resolution = ' + str( resol_point_list[im] )+' micro_meters\n\n') fout.write('\n########## SPECTRUM ANALYSIS END ##########\n') fout.close() print('\n########## SPECTRUM ANALYSIS END ##########\n')