X
8675

How to calculate image RMSE between two images

This Help Article contains a small user function that can be used to calculate and return the RMSE statistic between two images.

pro calculate_image_RMSE_define_buttons, buttonInfo
    compile_opt idl2
  
    ;Create button so that the program can be called from within ENVI.
    ;Button will appear under File->Basic Tools->Statistics ->Calculate Image RMSE.
  
    envi_define_menu_button, buttonInfo, value = 'Calculate Image RMSE', $
        event_pro='calculate_image_RMSE', POSITION='last', ref_value = 'Statistics', $
        uvalue='Calculate Image RMSE'
  
end
;==============================================
  
pro calculate_image_RMSE, event
    compile_opt idl2
  
    ;select the first input band
    envi_select, fid=fid1, /band_only, dims=dims1, pos=pos1, title='Select first band'
    if (fid1[0] eq -1) then return
  
    ;select the second input band
    envi_select, fid=fid2, /band_only, dims=dims2, pos=pos2, title='Select second band'
    if (fid2[0] eq -1) then return
  
    envi_file_query, fid1, ns=ns, nl=nl, nb=nb
  
    ;Create output file
    output_file = dialog_pickfile(title='Select Output File Name and Location')
    if output_file eq '' then return
  
    ;calculate the total number of pixels for the equation
    total_pixels = ns*nl
  
    ;open a file for writing
    openw, lun, output_file, /get_lun
  
    ;this sets up a status report while the calculation runs
    envi_report_init, title='RMSE calculation', $
        ['Accessing data from original file...','Creating RMSE Image...'], $
        base=base, /interrupt
    envi_report_inc, base, nl-1
  
        ;Set up for loop to grab data slices line by line
        for i=0, nl-1 do begin
            envi_report_stat, base, i, nl-1, cancel=cancel
            if cancel eq 1 then begin
                envi_report_init, base=base, /finish
                free_lun, lun
                file_delete, output_file
                return
            endif
                slice1 = envi_get_slice(fid=fid1, line=i, pos=pos1, xs=0, xe=ns-1)
                slice2 = envi_get_slice(fid=fid2, line=i, pos=pos2, xs=0, xe=ns-1)
                result = sqrt(((float(slice1) - float(slice2))^2)/total_pixels)
                writeu, lun, result
        endfor
  
        envi_report_init, base=base, /finish
        ;set up the header for the new data file
        envi_setup_head, nb=1, data_type=4, fname=output_file, $
            interleave=0, ns=ns, nl=nl, offset=0, /write
  
    free_lun, lun
  
    ;open the file in ENVI's ABL
    envi_open_file, output_file
  

    end


Review on 12/31/2013 MM