Loop merged RGB and NIR images and export as a single RGBN image

Clutch
Clutch Global Mapper User

I need to combine separate color images (RGB) with near infrared (NIR) and export as a single 4-band (RGBN), tiff file using separate directories of each type. I came across this code in the Global Mapper forums but I am having issues with it. The script loops through the directories and does export individual files, but the file results are all the same image of the last image it looped through. It appears it spits out all the files at the same time, not create/export file 1, then loop and create/export file 2 and so on.

Anyone can use the script to give it a try, just put 2 images in one folder, put 2 different images (but with the exact same name as the first 2), in a 2nd folder and run it. See if both image exports are the same image. Can anyone see where the code needs correcting? THX!


GLOBAL_MAPPER_SCRIPT VERSION="1.00"

UNLOAD_ALL

//Note: RGB and NIR images need to be separated into different folders and

//need to have the exact same name.


// Define Root Folder of Project Export ------------------------------------


DEFINE_VAR NAME="RGB_DIR" PROMPT=DIR ABORT_ON_CANCEL=YES PROMPT_TEXT="RGB Image Directory"

DEFINE_VAR NAME="NIR_DIR" PROMPT=DIR ABORT_ON_CANCEL=YES PROMPT_TEXT="NIR Image Directory"

DEFINE_VAR NAME="RGBN_DIR" PROMPT=DIR ABORT_ON_CANCEL=YES PROMPT_TEXT="RGBN Output Directory" 


// Importing Images ------------------------------------------------------------


IMPORT_DIR_TREE DIRECTORY="%RGB_DIR%" \

TYPE="GEOTIFF" LABEL_FIELD_FORCE_OVERWRITE="NO" CLIP_COLLAR="NONE" \

SAMPLING_METHOD="NEAREST_NEIGHBOR" AUTO_CONTRAST="NO" CONTRAST_SHARED="YES" \

CONTRAST_MODE="NONE" TEXTURE_MAP="NO" LAYER_DESC="RGB_IMAGES" LAYER_GROUP="RGB Images"


IMPORT_DIR_TREE DIRECTORY="%NIR_DIR%" \

TYPE="GEOTIFF" LABEL_FIELD_FORCE_OVERWRITE="NO" CLIP_COLLAR="NONE" \

SAMPLING_METHOD="NEAREST_NEIGHBOR" AUTO_CONTRAST="NO" CONTRAST_SHARED="YES" \

CONTRAST_MODE="NONE" TEXTURE_MAP="NO" LAYER_DESC="NIR_IMAGES" LAYER_GROUP="NIR Images"


// Looping through folders and merging bands -----------------------------------------


 DIR_LOOP_START DIRECTORY="%RGB_DIR%"

   DEFINE_VAR NAME="CURRENT_RGB_FNAME" VALUE="%FNAME%"

   DEFINE_VAR NAME="CURRENT_RGB_FNAME_W_DIR" VALUE="%FNAME_W_DIR%"


   DIR_LOOP_START DIRECTORY="%NIR_DIR%"

     DEFINE_VAR NAME="CURRENT_NIR_FNAME" VALUE="%FNAME%"

     DEFINE_VAR NAME="CURRENT_NIR_FNAME_W_DIR" VALUE="%FNAME_W_DIR%"


    IF COMPARE_STR="%CURRENT_RGB_FNAME%=%CURRENT_NIR_FNAME%"


      EXPORT_RASTER FILENAME="%RGBN_DIR%%CURRENT_RGB_FNAME%" TYPE="GEOTIFF" \

      NUM_BANDS=4 PALETTE="MULTIBAND" BAND_BIT_DEPTH=8


      EXPORT_RASTER FILENAME="%RGBN_DIR%"%FNAME%" TYPE="GEOTIFF" NUM_BANDS=4 \

      PALETTE="MULTIBAND" BAND_BIT_DEPTH=8 \

        BAND_EXPORT_SETUP="1?1?%CURRENT_RGB_FNAME_W_DIR%" \

        BAND_EXPORT_SETUP="2?2?%CURRENT_RGB_FNAME_W_DIR%" \

        BAND_EXPORT_SETUP="3?3?%CURRENT_RGB_FNAME_W_DIR%" \

        BAND_EXPORT_SETUP="4?1?%CURRENT_NIR_FNAME_W_DIR%"


   ELSE

    END_IF


   DIR_LOOP_END

 DIR_LOOP_END

UNLOAD_ALL

Answers

  • Clutch
    Clutch Global Mapper User

    Ok, I was able to "tweak" the script a little and got it to run properly. Issue is now I am seeing a white border around some of the images and sometimes not. Both RGB and NIR images are the exact size in pixels. I am thinking it is looping to fast and GM cannot finish producing the tif image. Is there a way to slow down, like place a time delay or pause into the script?

  • JeffH@BMG
    JeffH@BMG Global Mapper Developer Trusted User

    Hi Clutch,

    I think that it's unlikely that Global Mapper is looping too quickly, if you're referring to the DIR_LOOP stuff, but that possibility could be eliminated by picking an RGB and NIR pair that didn't work using DIR_LOOP, and see whether they work using the Global Mapper UI or the same script commands that you used to export the combine layer in the larger script. If they then don't work, and you think that they should, it would be something that we'd want to look into.

    Best regards,

    ~Jeff

  • Clutch
    Clutch Global Mapper User

    Thx for the reply Jeff,

    The reason I suspect the issue (yes, we are talking about DIR_LOOP), is because when I run one pair the image comes out correct, no white border (missing data). But If I had say 3-5 more pairs, the first image now has a border. And the borders on other images are not exactly the same in pixel width. Some are wider or shorter than other images... it can vary seeming the image was not completely written. I run several hundred at a time and usually I get 6-7 bordered images to every one without. I will try you suggestion. I will say this, I am using an old version of GM so maybe the latest does not have this issue.

    With new versions of GM are you still supporting your script language or are you now only concentrating on Python? I still think adding a PAUSE, like a variable setting in milliseconds could be useful. But again, I could be wrong on my assumptions.

  • Clutch
    Clutch Global Mapper User

    Well, no sooner than I write the above reply I get a different result. FYI for those interested, in my first tests I was combining images that were approximately 1000x1000px but I might have been off a few here and there (like 1020x1020), but the RGB and NIR images were always matched.

    I changed the script to be 1200x1200px exactly for every image, thinking I would need to further crop out the white border bring them down to 1000x1000px again. Just ran the script again and every image merged perfectly!?! No white borders and all 1200x1200 which means I do not need to further crop. Have no idea why it works now but happy.

  • JeffH@BMG
    JeffH@BMG Global Mapper Developer Trusted User
    Answer ✓

    Hi Clutch,

    Well, I was about to respond, but then found your latest post that things are working now for you. I was wondering whether you had some outliers in your input files, but you beat me to it. Glad to hear it.

    As for Global Mapper scripting, I can assure you that were are still supporting it, and indeed continue to add to it. Python is the new kid on our block, so it's getting some more attention internally, but our users have invested in using our scripting facilities and that's important to us.

    Cheers,

    ~Jeff