X
PrevPrev Go to previous topic
NextNext Go to next topic
Last Post 18 May 2011 12:54 PM by  anon
code for batch unsupervised classification with mask
 5 Replies
Sort:
You are not authorized to post a reply.
Author Messages

anon



New Member


Posts:23
New Member


--
18 May 2011 12:54 PM
    Hello, I'm trying to run an unsupervised classification (ISODATA) with the same mask image on jpg files in a folder through a loop and put the output files in another folder. I get the classification running ok, using a code for batch generating NDVI images that I found some time ago on the ITT website. I tried to modify this code to do the ISODATA classification that generates two classes and should apply a mask also. However, the outputs I get don't have the mask applied but instead the mask is loaded only into ENVI. There's also a few issues with the output file names, but that's not a big problem, as long as the output files have the mask applied. If there's anyone out there who can help me fix the code (see below), I'd appreciate it. Thanks, Mike pro ml_batch_01 compile_opt idl2 ;set up path to input and output files input_path = 'C:\B_Working\__ML_test\ml_in01\' ;change to output directory where I want the processed files to be placed cd, 'C:\B_Working\__ML_test\ml_out01\' ; search for data files in the specified directory files = FILE_SEARCH(input_path + '*.jpg', count=count) IF (count EQ 0) THEN BEGIN PrintF, 'No files were found to process' ENDIF for i=0, count-1 do begin envi_open_file, files[i], r_fid=fid if (fid eq -1) then begin envi_batch_exit return endif envi_file_query, fid, dims=dims, ns=ns, nl=nl, fname=filename ;; ML: setting up the mask file maskfile = 'C:\B_Working\Niina_data_16May2011\__ML_test\temp\ml_mask42' ENVI_OPEN_FILE, maskfile, r_fid=m_fid if (m_fid eq -1) then return envi_file_query, m_fid, dims=dims, nb=nb ;set pos array for calculating ISODATA (CLASS_DOIT method=4) pos = [1,2,3] - 1 ;set the output file names by stripping out the base name and appending '_unsup.tif' out_name = file_basename(filename, '.jpg') + '_unsup.tif' ;call the doit for the unsupervised ISODATA classification envi_doit, 'class_doit', $ fid=fid, pos=pos, dims=dims, $ /check, o_min=0, o_max=255, $ out_name=out_name, r_fid=r_fid, mfid=mfid, mpos=mpos, value=0, $ method=4, change_thresh=0.05, iso_merge_dist=5, $ ; method 4 is isodata iso_min_pixels=10, min_classes=2, max_classes=2, num_classes=2, iterations=5, $ iso_split_std=1.0, iso_merge_pairs=2 endfor end

    MariM



    Veteran Member


    Posts:2396
    Veteran Member


    --
    25 May 2011 02:55 PM
    Try changing 'mfid' to 'm_fid' and 'mpos' to 'm_pos'. I don't think ENVI is seeing your mask because the correct keyword is not set.

    Deleted User



    New Member


    Posts:23
    New Member


    --
    26 May 2011 11:37 AM
    Hi MariM, Thanks for the reply. I can see why the wrong keyword would be a problem. However, after fixing this, the code still didn't work. But I managed to modify some other code that finally did the trick. Below is the code I have now for anyone who might want to use it too. Cheers, Mike PRO ml_multiple_unsupmasked ; Set starting directory (This is where your input file select starts.) cd, 'C:\B_Working\Niina_data_16May2011\__ML_test\ml_in01\' ; Select input files (Select all your images) files_list = DIALOG_PICKFILE(/READ,title ='Select input files to be classified',/multiple_files) ; Select the mask File mask_file = DIALOG_PICKFILE(/read, title = 'Select ENVI mask file (0s are ignored)') ENVI_OPEN_FILE, mask_file, r_fid=m_fid ; Count the number of files num_files = N_elements (files_list) ; Set up the for loop to loop through the files FOR count = 0, num_files-1 DO BEGIN ; Selects the n- file. Output file name is ; "input_file_name"+"_unsup2masked" in_file = files_list [count] out_file = files_list [count]+'_unsup2masked' ENVI_OPEN_FILE, in_file, r_fid=fid IF (fid EQ -1 OR m_fid EQ -1) THEN RETURN ; Get some useful information and set the output filename. ENVI_FILE_QUERY, fid, ns=ns, nl=nl, nb=nb, bname=bname ; Set the keyword parameters dims = [-1l, 0, ns-1, 0, nl-1] pos = LINDGEN(nb) m_pos = [0l] ; Call the doit for the unsupervised ISODATA classification envi_doit, 'class_doit', $ fid=fid, pos=pos, dims=dims, $ /check, o_min=0, o_max=255, $ out_name=out_file, r_fid=r_fid, m_fid=m_fid, $ method=4, change_thresh=0.05, iso_merge_dist=5, $ ; method 4 is isodata iso_min_pixels=10, min_classes=2, num_classes=2, iterations=5, $ iso_split_std=1.0, iso_merge_pairs=2, $ value=0, m_pos=m_pos ENDFOR END

    MariM



    Veteran Member


    Posts:2396
    Veteran Member


    --
    26 May 2011 11:51 AM
    Can you run a single data file and mask in the GUI? Then try this same data file and mask in your code and simplify the code as much as possible. For example, here is a paired down version that runs and produces an expected output: pro ml_batch_01 compile_opt idl2 idl2 envi_select, fid=fid , fid=fid envi_file_query, fid, dims=dims, ns=ns, nl=nl, nb=nb, fname=filename , fid, dims=dims, ns=ns, nl=nl, nb=nb, fname=filename ;; ML: setting up the mask file envi_select, fid=m_fid , fid=m_fid ;set pos array for calculating ISODATA (CLASS_DOIT method=4) pos = lindgen(nb) out_name = 'isodata_result.dat' ;call the doit for the unsupervised ISODATA classification envi_doit, 'class_doit', $ , 'class_doit', $ fid=fid, pos=pos, dims=dims, $ out_name=out_name, r_fid=r_fid, m_fid=m_fid, m_pos=0, $ method=4, change_thresh=0.05, iso_merge_dist=5, $ ; method 4 is isodata iso_min_pixels=10, min_classes=2, num_classes=2, iterations=5, $ iso_split_std=1.0, iso_merge_pairs=2 end

    Deleted User



    New Member


    Posts:23
    New Member


    --
    26 May 2011 01:01 PM
    Hi MariM, Thanks again...sorry, I didn't even see that you replied before I modified my reply to your original answer. Problem is solved. Mike

    Deleted User



    New Member


    Posts:
    New Member


    --
    20 Jun 2011 03:18 AM
    Hello MariM, I am very new in writing code..i am looking for NDVI time series analysis using ENVI. If you have any existing code to stack time series data, kindly share to me. Thanks Giriraj
    You are not authorized to post a reply.