使用DigitalMicrograph(DM)脚本创建相邻像素差异的图像 [英] Creating an image of difference of adjacent pixels with digitalmicrograph (DM) script

查看:157
本文介绍了使用DigitalMicrograph(DM)脚本创建相邻像素差异的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下数字显微照片功能尝试通过获取图像行的子行中相邻像素的差来创建图像.将第一像素替换为由此创建的子行的差分结果的平均值.

The following digitalmicrograph function tries to create an image by taking difference of neighboring pixel in a sub-row of a row of the image. The first pixel is replaced with a mean of the difference result of the sub-row thus created.

例如如果输入图像的宽度为8像素,高度为1行,并且子行的大小为4- In_img = {8,9,2,4,9,8,7,5} 然后输出的图像将是- Out_img = {平均值(8,9,2,4)= 5.75,9-8 = 1,2-9 = -7,4-2 = 2,平均值(9,8,7,5)= 7.25,8- 9 = -1,7-8 = -1,5-7 = -2}

E.g. If the input image is 8 pixels wide and 1 rows tall and the sub-row size is 4 - In_img = {8,9,2,4,9,8,7,5} Then the output image will be - Out_img = {mean(8,9,2,4)=5.75,9-8=1,2-9=-7,4-2=2,mean(9,8,7,5)=7.25,8-9=-1,7-8=-1,5-7=-2}

运行此脚本时,第一行的第一个像素正确,但是其余像素不正确.当我将循环限制设置为仅一个子行和一行,即x = 1和y = 1时,脚本可以正常工作.

When I run this script, the first pixel of the first row is correct but rest of the pixels are incorrect. When I set the loop limit to only one sub-row and one row i.e. x=1 and y=1, then the script works correctly.

关于脚本可能发生什么或有什么问题的任何想法吗?

Any ideas as to what may be happening or what may be wrong with the script?

测试图像在这里结果在这里.

    // Function to compute the standard deviation (sigma n-1) of an image, or
    // a set of values passed in as pixel values in an image. The
    // number of data points (n) the mean and the sum are also returned.
    // version:20080229
    // D. R. G. Mitchell, adminnospam@dmscripting.com (remove the nospam to make this email address work)
    // v1.0, February 2008
    void StandardDeviation(image arrayimg, number &stddev, number &n, number &mean, number &sum)
    {
        mean=mean(arrayimg)
        number xsize, ysize
        getsize(arrayimg,xsize, ysize)
        n=xsize*ysize
        sum=sum(arrayimg)
        image imgsquared=arrayimg*arrayimg
        number sumofvalssqrd=sum(imgsquared)
        stddev=sqrt(((n*sumofvalssqrd)-(sum*sum))/(n*(n-1)))
    }

image getVectorImage(image refImage, number rowsize)
{
    number fh, fv, fhx
    getsize(refImage, fh, fv)

    fhx=trunc(fh/rowsize)

    //result("ByteSize of refimage = "+refImage.ImageGetDataElementByteSize()+"\n")
    //create image to save std of each row of the ref image.  
    //The std values are saved as pixels of one row.  The row size is same as number of rows.
    //use fhx*rowsize for the new imagesize as fhx is truncated value.
    image retImage:=RealImage("",4,fhx*rowsize,fv)
    image workImage=slice1(refImage,rowsize+1,0,0,0,rowsize-1,1)

    number stddev,nopix,mean,sum

    for ( number y=0;y<fv;y++)
        {
        for (number x=0;x<fhx;x++) 
            {
                //result ("x,y="+x+","+y+"; fhx="+fhx+"; rowsize="+rowsize+"\n")
                workImage=slice1(refImage,x*rowsize+1,y,0,0,rowsize-1,1)-slice1(refImage,x*rowsize,y,0,0,rowsize-1,1)
                showimage(workImage)
                StandardDeviation(workImage,stddev,nopix,mean,sum )
                retImage[y,x*rowsize+1,y+1,x*rowsize+rowsize]=workImage
                retImage[y,x]=mean
                result("mean @ row "+y+" = "+mean+"\n")
            }
        }

    return retImage
}
showimage(getVectorImage(getfrontimage(),rowsize))

推荐答案

编辑后,我了解到您想执行以下操作:

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()

一些解释:

  • 您要在图像中做两种不同的事情,因此必须小心,不要覆盖随后将用于计算的像素数据!图像是逐像素处理的,因此,如果您 first 计算平均值并将其写入第一个像素,则第二个像素的求值将为"9"与刚刚存储的平均值之差(不是原始的"8").因此,您必须拆分计算并使用缓冲"副本.

  • 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.

slice2命令非常方便,因为它允许在采样时定义 stepsize .您可以使用它直接解决深灰色像素.

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意味着A现在与B是相同的内存位置.A基本上是B的另一个名称.

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

A = B表示A获得B的值(已复制). A和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.

这篇关于使用DigitalMicrograph(DM)脚本创建相邻像素差异的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆