Search code examples
image-processingrgbdm-script

How to merge 2 RGB images in DM by scripting?


I would like to overlay 2 (or more) RGB images in Digital Micrograph by scripting.

Unlike some realimages without color that can be merged by summing the intensity, RGB images should be merged in another way but I have no clue.

Thanks for helping!


Solution

  • You can sum RGB images just like regular images, but your problem is that you need to define what you mean by "overlay".

    RGB Images are triplets holding a value for each of the 3 channels RED, GREEN, BLUE and these values are clipped between [0 and 255].

    "Summing" RGB images will give you again a triplet, but any value bigger then 255 is truncated to 255, so you will shift more and more towards "white" in the image.

    You could define an "overlay" as the mean-values instead, but the effect of "overlaying" then becomes more and more towards "average gray".

    Or you could define an "overlay" as the "max-values" or "min-values" of the involved channels.

    Or, or, or....

    When you think of "overlaying" RGB images, it is helpful to think of other graphic programs like Photoshop which allow you combinining "layers". Usually these programs offer you multiple options ( "overlay, screen, lighten, darken, you name it..." ) which all define a different mathematical relationship between the three color values of the first and the three color values of the second layer.

    The commands you need to do this maths are RGB( ), RED( ), GREEN( ), and BLUE( ) as well as simple maths. See the example:

    image img1r := RealImage("Red 1",4,256,256)
    image img1g := RealImage("Green 1",4,256,256)
    image img1b := RealImage("Blue 1",4,256,256)
    
    img1r = icol/iwidth * 256
    img1b = iradius/iwidth * 256
    img1g = irow/iwidth * 256
    
    RGBImage img1 = RGB(img1r,img1g,img1b)
    img1.Setname( "Image 1 (RGB)")
    
    image img2r := RealImage("Red 2",4,256,256)
    image img2g := RealImage("Green 2",4,256,256)
    image img2b := RealImage("Blue 2",4,256,256)
    
    img2r = (icol%10)<5 ? 256 : 100
    img2g = (irow%10)<5 ? 256 : 100
    img2b = (iradius%10)<5 ? 256 : 100
    RGBImage img2 = RGB(img2r,img2g,img2b)
    img2.Setname( "Image 2 (RGB)")
    
    image sumImg = img1 + img2
    sumImg.SetName( "SUM" )
    
    image avImg = (img1 + img2)/2
    avImg.SetName( "AVERAGE" )
    
    image maxImg = RGB( max(red(img1),red(img2)), max(green(img1),green(img2)), max(blue(img1),blue(img2)))
    maxImg.SetName( "Channel MAX" )
    
    image minImg = RGB( min(red(img1),red(img2)), min(green(img1),green(img2)), min(blue(img1),blue(img2)))
    minImg.SetName( "Channel MIN" )
    
    
    // Arrange display
    EGUPerformActionWithAllShownImages( "delete" )
    
    minImg.ShowImage()
    maxImg.ShowImage()
    avImg.ShowImage()
    sumImg.ShowImage()
    img2.ShowImage()
    img1.ShowImage()
    
    TagGroup layout = SLMCreateGridLayout( 2 , 3 )
    EGUArrangeAllShownImagesInLayout( layout )
    

    Output


    It should also be noted that some "overlay" combinations are not based on the Red/Green/Blue (RGB) color model, but on the alternative Hue/Saturation/Brightness (HSB) color model.

    DigitalMicrograph scripting does natively only support RGB, but you can do the maths yourself.

    You might also find it useful to look at the examples script "Display as HSB.s" on the Gatan script example site.