Creating an image of difference of adjacent pixels with digitalmicrograph (DM) script

半腔热情 提交于 2019-12-06 22:25:12

After your edit, I understood that you want to do something like this:

and that this should be performed for each line of the image individually.

The following script does this. (Explanations below.)

image Modify( image in, number subsize )
{
    // Some checking
    number sx,sy
    in.GetSize(sx,sy)
    if ( 0 != sx%subsize )
        Throw( "The image width is not an integer multiplication of the subsize." )

    // Do the means...
    number nTile = sx/subsize
    image meanImg := RealImage( "Means", 4, nTile , sy )
    meanImg = 0
    for ( number i=0; i<subsize; i++ )
        meanImg += in.Slice2( i,0,0, 0,nTile,subsize, 1,sy,1 )

    meanImg *= 1/subsize

    // Do the shifted difference
    image dif := RealImage( "Diff", 4, sx-1, sy )
    dif = in.slice2( 1,0,0, 0,sx-1,1, 1,sy,1) - in.slice2( 0,0,0, 0,sx-1,1, 1,sy,1) 

    // Compile the result
    image out := in.ImageClone()
    out.SetName( in.getName() + "mod" )

    out.slice2( 1,0,0, 0,sx-1,1, 1,sy,1 ) = dif
    out.slice2( 0,0,0, 0,nTile,subsize, 1,sy,1 ) = meanImg

    return out
}


number sx = 8, sy = 4
image img := RealImage( "test", 4, 8, 4 )
img = icol*10 + trunc( Random()*10 )

img.ShowImage()
Modify(img,4).ShowImage()

Some explanations:

  • You want to do two different things in the image, so you have to be careful not to overwrite data in pixels you will subsequently use for computation! Images are processed pixel by pixel, so if you first compute the mean and write it in the first pixel, the evaluation of the second pixel will be the difference of "9" and the just stored mean-value (not the original "8"). So you have to split computation and use "buffer" copies.

  • The slice2 command is extremely convenient, because it allows to define a stepsize when sampling. You can use it to address the dark-grey pixels directly.

  • Be aware of the difference between := and = in image expressions. The first is a memory assignment:

    • A := B means that A now is the same memory location as B. A is basically another name for B.

    • A = B means A gets the values of B (copied). A and B are two different memory locations and only values are copied over.

I have some observations in your script:

  • Instead of the defined method for getting mean/sum/stdev/n of an image, you can as easily get to those numbers from any image img using the following:

    mean: number m = mean( img )

    sum: number s = sum( img )

    stdev: number sd = sqrt( variance( img ) )

    pixels: number n = sum( 0 * img + 1 )

  • if you want to get the difference of an image with an image "shifted by one" you don't have to loop over the lines/columns but can directly use the slice2() command; a [ ] notation using icol and irow; or the command offset() Personally, I prefer the slice2() command.

If I want a script which gives me the standard deviation of the difference of each row with its successor row, i.e. stdDev( row_(y) - row_(y+1) ) for all y < sizeY, my script would be:

Image img :=  GetFrontImage()
number sx,sy
img.GetSize(sx,sy)
number dy = 1

Image dif = img.Slice2(0,0,0, 0,sx,1, 1,sy-1,1 ) - img.Slice2(0,dy,0, 0,sx,1, 1,sy-1,1)
Image sDevs := RealImage( "Row's stDev", 4, sy-1 )
for ( number y=0; y<sy-1; y++ )
    sDevs[y,0] = SQRT( Variance( dif.Slice1(0,y,0, 0,sx,1) ) )

sDevs.ShowImage()

Is this, what you try to achieve? If not, please edit your question for some clarification.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!