X
PrevPrev Go to previous topic
NextNext Go to next topic
Last Post 04 Jun 2007 02:41 PM by  anon
Compiling and running *.pro file (Entropy Calculation)
 1 Replies
Sort:
You are not authorized to post a reply.
Author Messages

anon



New Member


Posts:
New Member


--
04 Jun 2007 02:41 PM
    Dear IDL forum, I am a regular ENVI user who has little or no experience in IDL. I downloaded some IDL code for calculating the entropy value of an image. http://astrosun2.astro.co...ression/entropy.html I am now trying to compile and run the code in IDL but am having trouble with the usage: I open and compile the *.pro file then run it using the following command. entropy_value = entropy_func(my_image) I then get the following error message : Struct expression not allowed in this context: ARRAYIN. (<- have googled it with no luck) Can someone in this forum please point out where I am going wrong or provide some help as to the proper compilation and running of *.pro files. The code was written in 1997 so their may be some compatibility issues that I am unaware of. I have tried contacting the author but it seems the email address given is no longer in use. I have included the code below. I am working on a DELL optiplex 620 running win XP using IDL 6.3 Kind regards and many thanks for your time on this matter Wesley function entropy_func, arrayin ;-------------------------------------------------------------------- ; Modified from the procedure to return the entropy value ; Calculates the line-by-line image entropy of the image stored in ; 'arrayin'. Prints the entropy value to screen. If the array is of ; integer type, a 12 bit image is assumed. If it is of byte type, ; an 8 bit image is assumed. ; Usage: entropy_value = entropy(image_array) ; David O'Brien, 5/30/97 -- problem fixed 8/1/97 ;--------------------------------------------------------------------- ; Get array dimensions and type dimensions = size(arrayin) xsize = dimensions(1) ysize = dimensions(2) type = dimensions(3) ; Fix array to integer type array = fix(arrayin) ; Find correct size for probability array and create it if type eq 1 then array_size = 256*2-1 if type eq 2 then array_size = 4096*2-1 if (type ne 1) and (type ne 2) then print, 'Not a byte or integer array' prob_array = fltarr(array_size) ; Loop over image array elements and count occurences of each possible ; pixel to pixel difference value. Store these values in prob_array for j = 0, ysize-1 do $ for i = 0, xsize-2 do begin diff = array(i+1,j) - array(i,j) if diff lt (array_size+1)/2 and diff gt -(array_size+1)/2 then begin prob_array(diff+(array_size-1)/2) = prob_array(diff+(array_size-1)/2) + 1 endif endfor ; Convert values in prob_array to probabilities and compute entropy n = total(prob_array) entrop = 0 for i = 0, array_size-1 do begin prob_array(i) = prob_array(i)/n ; Base 2 log of x is Ln(x)/Ln(2). Take Ln of array element ; here and divide final sum by Ln(2) if prob_array(i) ne 0 then begin entrop = entrop - prob_array(i)*alog(prob_array(i)) endif endfor entrop = entrop/alog(2) return, entrop end

    Deleted User



    New Member


    Posts:
    New Member


    --
    04 Jun 2007 02:41 PM
    Looks like the author's using parenthetical array subscripts, which *might* be confusing IDL since parentheses are usually reserved for function calls and pulling fields out of structures (e.g., my_structure_variable.(i) returns the ith field in the structure variable) . I took the liberty of updating the code so it now follows the current convention (see below). I also tested it with one of the images that comes with ENVI (bhtmref.img) and it worked--but you are limited to inputted a 2D array so I only passed along the first band of data. Here are some lines of code you can type at the command prompt to test the program (make sure it's compiled first): input_file = 'c:\rsi\idl63\products\envi43\data\bhtmref.img' envi_open_file, input_file, r_fid=input_fid envi_file_query, input_fid, dims=dims ;retrieve first band of data from the file data = envi_get_data(fid=input_fid, dims=dims, pos=0) ;call entropy procedure, pass in 2D image array entropy_value = entropy_func(data) ============================================================================= f unction entropy_func, arrayin compile_opt idl2 ;-------------------------------------------------------------------- ; Modified from the procedure to return the entropy value ; Calculates the line-by-line image entropy of the image stored in ; 'arrayin'. Prints the entropy value to screen. If the array is of ; integer type, a 12 bit image is assumed. If it is of byte type, ; an 8 bit image is assumed. ; Usage: entropy_value = entropy(image_array) ; David O'Brien, 5/30/97 -- problem fixed 8/1/97 ;--------------------------------------------------------------------- ; Get array dimensions and type dimensions = size(arrayin) xsize = dimensions[1] ysize = dimensions[2] type = dimensions[3] ; Fix array to integer type array = fix(arrayin) ; Find correct size for probability array and create it if type eq 1 then array_size = 256*2-1 if type eq 2 then array_size = 4096*2-1 if (type ne 1) and (type ne 2) then print, 'Not a byte or integer array' prob_array = fltarr(array_size) ; Loop over image array elements and count occurences of each possible ; pixel to pixel difference value. Store these values in prob_array for j = 0, ysize-1 do $ for i = 0, xsize-2 do begin diff = array[i+1,j] - array[i,j] if diff lt [array_size+1]/2 and diff gt -[array_size+1]/2 then begin prob_array[diff+[array_size-1]/2] = prob_array[diff+[array_size-1]/2] + 1 endif endfor ; Convert values in prob_array to probabilities and compute entropy n = total(prob_array) entrop = 0 for i = 0, array_size-1 do begin prob_array[i] = prob_array[i]/n ; Base 2 log of x is Ln(x)/Ln(2). Take Ln of array element ; here and divide final sum by Ln(2) if prob_array[i] ne 0 then begin entrop = entrop - prob_array[i]*alog(prob_array[i]) endif endfor entrop = entrop/alog(2) return, entrop end
    You are not authorized to post a reply.