Search code examples
imageexpressionpixeldm-scriptmanipulate

How to manipulate pixel values via image expression?


Recently I am writing a script to manipulate the pixel values in an image. The idea is to set the pixels that fall into the given range to a specific value. Instead of using the command "for" which loops from pixel to pixel, an image expression was utilized, for example:

Img = (Img>=thresh_Low && Img<=thresh_Up ? 0 : Img)

Here comes the question: if I would like to replace the pixel value with the average of neighbouring pixels, rather than just a fixed value such as 0 in the above case, pixel-looping seems not avoidable anymore. Does anyone know any workaround that the method of image expression can still be used here?

Thanks in advance.


Solution

  • Computing images expressions is much more efficient than any pixel-by-pixel operation. Even if you thereby compute some averages that are not needed will the script perform a lot faster. Therefore:

    You should compute an average-image (for all pixels, not just the masked ones) and then use it in the masked assignment.

    The following example illustrates this. Only the last two lines are the direct answer to your question. The condition is used to either copy the orignal or the averaged value:

    number aver_NN = 3      // Next neighbor averaging. 1 = 3x3, 2 = 5x5 etc.)
    number maskRad = 0.3    // just a radius to show masking
    
    image img := GetFrontImage()
    if ( 2 != img.ImageGetNumDimensions() ) Throw( "Only 2D images are supported." )
    
    // Create average image (ignoring border region for simplicity)
    image av := img * 0
    for( number dx=-aver_NN; dx<=aver_NN; dx++ )
        for( number dy=-aver_NN; dy<=aver_NN; dy++ )
            av += img.offset(dx,dy)
    
    av /= (2*aver_NN + 1) ** 2
    
    // Apply masked replacement
    image replaced = iradius < iwidth*maskrad ? av : img
    replaced.ShowImage()
    

    Further explanations because of comments below

    The averaging is done by shifting the whole image by one pixel using the offset command. This command will replace border pixels with the 0 value. Summing all the shifted images and dividing by the number of images therefore gives at each pixel the average value of the neighbor pixels, but the normalization in the border pixels is incorrect. The following script shows this using explicit images instead of the for-loop:

    
    number size = 25
    image test := realimage("Source",4,size,size)
    test = 1 + random()
    test.ShowImage()
    
    image offset_N = test.offset(  0, -1 )
    image offset_S = test.offset(  0,  1 )
    image offset_W = test.offset( -1,  0 )
    image offset_E = test.offset(  1,  0 )
    offset_N.ShowImage()
    offset_N.SetName("N")
    offset_S.ShowImage()
    offset_S.SetName("S")
    offset_W.ShowImage()
    offset_W.SetName("W")
    offset_E.ShowImage()
    offset_E.SetName("E")
    
    image average = test + offset_N + offset_S + offset_W + offset_E
    average /= 5
    average.SetName("Average")
    average.ShowImage()
    
    EGUPerformActionWithAllShownImages("Arrange")
    

    Output

    To fix the issue with the borders, two strategies could be used for the normalization.

    • Explicitly normalize subsections of the sum-image, knowing how many images where summed:
    ...
    
    image average = test + offset_N + offset_S + offset_W + offset_E
    average.SetName("Average")
    // Divide corners by 3
    // Divide edges by 4
    // Divide rest by 5
    average.slice2(0,0,0 ,0,2,size-1, 1,2,size-1) /= 3
    average.slice2(1,0,0 ,0,size-2,1, 1,2,size-1) /= 4
    average.slice2(0,1,0 ,0,2,size-1, 1,size-2,1) /= 4
    average.slice2(1,1,0 ,0,size-2,1, 1,size-2,1) /= 5
    
    ...
    
    • Create a second image which 'counts' automatically and used it for normalization. For this, simple create a 1-valued image of the same size as the source and perform the same summing steps! This makes the script from above into:
    number aver_NN = 2      // Next neighbor averaging. 1 = 3x3, 2 = 5x5 etc.)
    number maskRad = 1    // just a radius to show masking
    
    image img := GetFrontImage()
    if ( 2 != img.ImageGetNumDimensions() ) Throw( "Only 2D images are supported." )
    
    // Create average image 
    image av = img * 0
    image weight = av 
    image proxy = av + 1
    
    for( number dx=-aver_NN; dx<=aver_NN; dx++ )
    {
        for( number dy=-aver_NN; dy<=aver_NN; dy++ )
        {
            av += img.offset(dx,dy)
            weight += proxy.offset(dx,dy)
        }
    }
    
    weight.SetName("Sum weight")
    weight.showImage()
    
    av /= weight
    
    // Apply masked replacement
    image replaced = iradius < iwidth*maskrad ? av : img
    replaced.ShowImage()
    
    • One can also create the average image by relying on the inbuilt Convolution() command, which correctly handles the border cases right away. Here, one would just create the average image as:
    // Create average image 
    // Define an averaging kernel
    image kernel := [5,5] : {
     { 0, 0, 1, 0, 0 },
     { 0, 1, 1, 1, 0 },
     { 1, 1, 1, 1, 1 },
     { 0, 1, 1, 1, 0 },
     { 0, 0, 1, 0, 0 } 
    }
    
    image av = img.Convolution(kernel)
    av.ShowImage()