How to shift the column in a SI image - dm-script

In order to perform drift correction in a SI image as shown in the following figure:
I write the code :
number max_shift=5
image src := GetFrontImage()
number sx, sy, sz
src.Get3DSize(sx, sy, sz)
result("sx: "+sx+"\n")
result("sy: "+sy+"\n")
result("sz: "+sz+"\n")
// assume a random shift in x
image shift := IntegerImage("xcorrd",4,0, 1, sy)
shift = max_shift*Random()
// make a coordinate table
image col := IntegerImage("col",4,0, sx, sy)
image row := IntegerImage("row",4,0, sx, sy)
image plane := IntegerImage("plane",4,0, sx, sy)
col = icol
row = irow
plane = iplane
// to expand the shift as the same size with source image
image ones := IntegerImage("ones",4,0, sx, sy)
ones = 1
// create a random column shift of the source SI image
for(number i=0; i<sy; i++) {
col[i,0,i+1,sx] = col[i,0,i+1,sx]+shift.GetPixel(0,i)*ones[i,0,i+1,sx]
};
// drift corrected
image im := RealImage("test si", 4, sx+max_shift, sy, sz)
im=0
im[col, row, plane] = src[icol,irow,iplane]
im.ImageGetTagGroup().TagGroupCopyTagsFrom(src.ImageGetTagGroup())
im.ImageCopyCalibrationFrom(src)
im.SetName(src.GetName()+"-drift corrected")
im.showimage()
The image can be corrected, however the spectrum cannot be transferred to the corrected SI as shown :
I am just wondering what's wrong with my script.
Thank you in advance.

im[col, row, plane] = src[icol,irow,iplane]
The intrinsic variables icol, irow, iplane will be evaluated by the only fixed size image expression in the line. In your case col, row and plane (all of same size)
However, they are all 2D so what is internally happening is that you iterate over X & Y and then write the values:
im[ col(x,y), row(x,y), plane(x,y) ] = src[x,y,0] // iterated over all x/y
As Don I was mentioning in the comments, you would want to iterate over the z dimension.
Alternatively, you could make all of your images of size (sx,sy,sz) in your script.
This would work for the expression, but is horrifically inefficient.
In general, the best solution here is to no t use icol,irow,iplane at all, but make use of the Slice commands. see this answer:
I would possibly code a line-wise x-shift for an SI like below:
The script utilizes the fact that one can shift whole "blocks" (X x 1 x Z) in x-direction, iterating over y.
number sx = 256
number sy = 256
number sz = 100
image testSI := realImage("SI",4,sx,sy,sz)
testSI = sin(itheta/(idepth-iplane)*idepth) + (iplane % (icol+1))/idepth
testSI.ShowImage()
image xdrift := RealImage("XDrift per line",4,sy)
xdrift = trunc(random()*5 + 20*sin(icol/iwidth*3*PI()))
xdrift.ShowImage()
// Apply linewise Drift to SI, assuming xDrift holds this data
xDrift -= min(xDrift) // ensure only positive shifts
image outSI := realImage("SI shifted",4,sx+max(xDrift),sy,sz)
outSI.ShowImage()
for( number y=0; y<sy; y++ ){
number yShift = sum(xDrift[y,0])
outSI.slice2( yShift,y,0, 0,sx,1, 2,sz,1 ) = testSI.slice2(0,y,0,0,sx,1,2,sz,1)
}
The script below performs the iteration "plane by plane", but does not have a restriction on the plane-wise shift.
In fact, here each pixel gets an assigned XY shift.
Note that you can use warp(source, xexpr, yexpr ) instead of 2D addressing source[ xexpr, yexpr ] if you want to use bilinear interploation of values (and 0 truncation outside the valid range).
number sx = 256
number sy = 256
number sz = 100
image testSI := realImage("SI",4,sx,sy,sz)
testSI = sin(itheta/(idepth-iplane)*idepth) + (iplane % (icol+1))/idepth
testSI.ShowImage()
image xdrift := RealImage("pixelwise XDrift",4,sx,sy)
xdrift = irow%10*random() + 20*cos(irow/iheight*5*PI())
xdrift.ShowImage()
image ydrift := RealImage("pixelwise yDrift",4,sx,sy)
ydrift = 10*abs(cos(icol/iwidth* (irow/iheight) * 10 * PI())) + irow/iheight * 10
ydrift.ShowImage()
// Apply pixelwise Drift to SI
xDrift -= min(xDrift) // ensure only positive shifts
yDrift -= min(yDrift) // ensure only positive shifts
number mx = max(xDrift)
number my = max(yDrift)
image outSI := realImage("SI shifted",4,sx+mx,sy+my,sz)
outSI.ShowImage()
for( number z=0;z<sz;z++){
image outPlane := outSI.Slice2( 0,0,z, 0,sx+mx,1,1,sy+my,1)
image srcPlane := testSI.Slice2( 0,0,z, 0,sx,1,1,sy,1)
outPlane = srcPlane[ icol + xdrift[icol,irow] - mx, irow + ydrift[icol,irow] - my ]
// outPlane = srcPlane.warp( icol + xdrift[icol,irow] - mx, irow + ydrift[icol,irow] - my)
}

Related

DM script to calculate area under a curve with given ROI

I want to calculate the area under a curve with giving ROI, like the attached picture, I want to get the area under the green curve from 13 to 30, could we reach this function by DM scripting?
Never done similar things before, any suggestions thanks!
If the image is of label "A" then, you can simply do:
sum( A[] )
or, a bit more elaborated:
image img := GetFrontImage()
number integral = sum( img[] )
result("\n The intensity of the selected region in image [")
result( img.imageGetLabel() + "]:" + img.ImageGetName() + " is: ")
result( integral)
Note that this will give you the integral (or just the sum of channel-values) in raw numbers. If you have calibrated the intensity, you will have to take the calibration into account.
If - as in most cases - you just have an intensity scale but no offset, it is straight forward:
image img := GetFrontImage()
number integral = sum( img[] )
number scale = img.ImageGetIntensityScale()
number integral_cal = integral * scale
result("\n The intensity of the selected region in image [")
result( img.imageGetLabel() + "]:" + img.ImageGetName() + " is: ")
result( integral_cal )
If you also have an origin, it becomes:
image img := GetFrontImage()
number integral = sum( img[] )
number scale = img.ImageGetIntensityScale()
number origin = img.ImageGetIntensityOrigin()
number t,l,b,r
img.GetSelection(t,l,b,r)
number nChannels = r - l
number integral_cal = (integral - origin * nChannels) * scale
result("\n The intensity of the selected region in image [")
result( img.imageGetLabel() + "]:" + img.ImageGetName() + " is: ")
result( integral_cal )

How to set starting and ending point for line projection in DM script

I am trying to draw line projection for an image . The line 4 in the code below sy/2 represents the length of projection (here is the half image range). But how to set the starting point or ending point with scripting? For example, I want to draw the line projection, from 1/4 image range to 3/4 image range. Any suggestions?
image src := getfrontimage()
number sx,sy
src.GetSize(sx,sy)
image line_projection := RealImage( "Vertical", 4, sy/2 )
line_projection[irow,0] += src
line_projection *= 1/sx
While using intrinsic variables (icol,irow,...) for iterative summing was the fasted method in GMS 1, this is no longer true for newer versions that utilize multi-threaded code, as demonstrated by the following example:
// various ways to sum a subsection of an image
number sx = 4096, sy = 4096
number startx = 0.2, starty = 0.2
number endx = 0.8, endy = 0.4
// Coordinates of cut
number t = trunc(starty*sy), l = trunc(startx*sx), b = trunc(endy*sy), r = trunc(endx*sx)
image test := realImage( "Test", 4, sx, sy )
test = sin( icol/iwidth * 20*Pi()) + cos( itheta * iradius/iwidth * 50)
test= sin( icol/iwidth * 20*Pi())
test.ShowImage()
ROI marker = NewROI()
marker.ROISetLabel( "Section" )
marker.ROISetRectangle( t, l, b, r )
marker.ROISetVolatile( 0 )
test.ImageGetImageDisplay(0).ImageDisplayAddRoi( marker )
//OKDialog( "Performing vertical sum with various methods now." )
number h = b - t
number w = r - l
ClearResults()
number ts, te, tps = GetHighResTicksPerSecond()
// using intrinsic variables
image sumImg1 := RealImage( "Sum intrinsic", 4, w )
ts = GetHighResTickcount()
sumImg1[icol, 0] += test[t,l,b,r];
te = GetHighResTickcount()
sumImg1.ShowImage()
result("\n Summing using intrinisic variables: " + (te-ts)/tps + " sec")
// using for-loop of slice
image sumImg2 := RealImage( "Sum with slice", 4, w )
ts = GetHighResTickcount()
for( number i=0; i<h; i++)
sumImg2 += test.slice1(0,i,0, 0,w,1)
te = GetHighResTickcount()
sumImg2.ShowImage()
result("\n Summing using for-loop with slice : " + (te-ts)/tps + " sec")
// using project of slice
image sumImg3 := RealImage( "Sum with project", 4, w )
ts = GetHighResTickcount()
sumImg3 = test[t,l,b,r].project( 1 )
te = GetHighResTickcount()
sumImg3.ShowImage()
result("\n Summing using project on section : " + (te-ts)/tps + " sec")
You can use slicing to only look at the image area you are interested in. For "clipping" the source to the interesting part use img[y1, x1, y2, x2].
image src := getFrontImage();
number width, height;
src.GetSize(width, height);
number start_y = 1/4 * height;
number end_y = 3/4 * height;
image line_projection := RealImage("Vertical", 4, width);
line_projection[icol, 0] += src[start_y, 0, end_y, width];
line_projection *= 1/(height/2);
line_projection.ShowImage();

Copying an XY calibration from a 2D image to a 3D image

How can I copy the 2D axes calibration of an image, to a 3D dataset of the same size, except that has an additional depth dimension?
The script below will do that. You use ImageGet/SetDimension.... for that.
image img2d := GetFrontImage()
if ( 2 != img2d.ImageGetNumDimensions() ) Throw( "Not 2D")
number sx = img2D.ImageGetDimensionSize(0)
number sy = img2D.ImageGetDimensionSize(1)
image img3d := RealImage("3D",4,sx,sy,10)
for( number i=0; i<2; i++ ){
img3d.ImageSetDimensionScale( i, img2d.ImageGetDimensionScale(i) )
img3d.ImageSetDimensionOrigin( i, img2d.ImageGetDimensionOrigin(i) )
img3d.ImageSetDimensionUnitString( i, img2d.ImageGetDimensionUnitString(i) )
}
img3d.ShowImage()
However, based on what you need the 3D image for, it can be better to clone the 2D image and make it into a 3D image. This will preserve all meta-data but set the data values to 0.
image img2d := GetFrontImage()
if ( 2 != img2d.ImageGetNumDimensions() ) Throw( "Not 2D")
number sx = img2D.ImageGetDimensionSize(0)
number sy = img2D.ImageGetDimensionSize(1)
number sz = 10
image img3d := img2d.ImageClone()
img3d.ImageResize(3, sx, sy, sz)
img3d.ShowImage()

Calculations of zero mean normalized cross-correlation in dm-script

Now I'm trying to make a dm-script for calculation of zero mean normalized cross-correlation (ZNCC) between two images. In the calculation of ZNCC, it is known that usages of FFT and integral image are quite efficient scheme. So I have made a following test script to calculate a integral image. However, this calculation is not sufficiently fast for typical image sizes of camera images, in my sence. Do you have any good idea to improve the calculation speed of integral image calculations? Alternatively, does anyone know some scripts for fast ZNCC calculations? Can I pick your brains?
Image integralImg( Image &inputImg ){
Number nx, ny
Number iu, iv, tmpval, fval, integ_pre1, integ_pre2, integ_pre3
Image integImg := inputImg.ImageClone()
integImg.SetName("Integral Image of " + inputImg.GetName())
inputImg.GetSize(nx, ny)
fval = inputImg.GetPixel(0, 0)
integImg.SetPixel(0, 0, fval)
FOR(iu = 1 ; iu < nx ; iu++){
fval = inputImg.GetPixel(iu, 0)
integ_pre1 = integImg.GetPixel(iu - 1, 0)
integImg.SetPixel(iu, 0, fval + integ_pre1)
}
FOR(iv = 1 ; iv < ny ; iv++){
fval = inputImg.GetPixel(0, iv)
integ_pre2 = integImg.GetPixel(0, iv - 1)
integImg.SetPixel(0, iv, fval + integ_pre2)
}
FOR(iv = 1 ; iv < ny ; iv++){
FOR(iu = 1 ; iu < nx ; iu++){
fval = inputImg.GetPixel(iu,iv)
integ_pre1 = integImg.GetPixel(iu - 1, iv)
integ_pre2 = integImg.GetPixel(iu, iv - 1)
integ_pre3 = integImg.GetPixel(iu - 1, iv - 1)
integImg.SetPixel(iu, iv, fval + integ_pre1 + integ_pre2 - integ_pre3)
}
}
Return integImg
}
//
Number nx = 1024
Number ny = 1024
Image IMG := RealImage("test",4,nx,ny)
IMG = Random()
//
Image intIMG
intIMG := integralImg( IMG )
intIMG.ShowImage()
Can you not just use the command
RealImage CrossCorrelate( RealImage source1, RealImage source2 ) ?
If not, can you explain to me, what exactly is different to a ZNCC?
( Note, that you can easily shift each source image to a mean value of zero by img -= mean(img))

How to append a n-dimension image to the end of a (n+1)-dimension image?

I have a 4D dataset with dimensions, e.g., 100(width) x 100(height) x 2048(energy channels) x 20 (frames).
Is there any function or method to append a 3D datacube (frame) of the same width, height and energy channels to the end of the 4D data set?
Finally the 4D data set should be 100*100*2048*21.
There is no command which expands an image object in this way.
You have to perform this as a manual copy. (Which requires the additional memory, unfortunately.)
number sx = 100
number sy = 100
number sz = 100
number sf = 10
image before := RealImage( "Before", 4, sx, sy, sz, sf )
before = icol
image addFrame := RealImage( "Frame", 4, sx, sy, sz )
addFrame = irow
image after := RealImage( "After", 4, sx, sy, sz, sf + 1)
after.SliceN( 4,4, 0,0,0,0, 0,sx,1, 1,sy,1, 2,sz,1, 3,sf,1 ) = before
after.SliceN( 4,4, 0,0,0,sf, 0,sx,1, 1,sy,1, 2,sz,1, 3,1,1 ) = addFrame
before.showimage()
after.ShowImage()
If this is an issue, you can alternatively use the streaming commands to directly stream data (values only, no meta data) into either a buffer memory or onto the hard disc. Only at the end, you'd stream that data into an apropriately sized image variable.
Finally, depending on your application, you might consider adding new data as a taggroup to a taglist (which can be appended). You can store "images" as tags ( TagGroupSetTagAsArray() ). Again, you could create a 'complete' image form this information at a later time point.
number sx = 100
number sy = 100
number sz = 100
number sf = 10
tagGroup FrameTags = NewTagGroup()
tagGroup FrameList = FrameTags.TagGroupGetOrCreateTagList( "frames" )
FrameTags.TagGroupSetTagAsLong( "X Size", sx )
FrameTags.TagGroupSetTagAsLong( "Y Size", sy )
FrameTags.TagGroupSetTagAsLong( "Z Size", sz )
// Add frames whenever you need .....
for( number f = 0; f < sf; f++ )
{
image addFrame := RealImage( "Frame", 4, sx, sy, sz )
addFrame = random()
FrameList.TagGroupInsertTagAsArray( f, addFrame )
}
// Handle taggroup as you want. (Add to global tags, add to image, store to disc... )
FrameTags.TagGroupOpenBrowserWindow( "frames", 0 )
// Whenever needed, reconstruct image from frames
number nsx, nsy, nsz
FrameTags.TagGroupGetTagAsLong( "X Size", nsx )
FrameTags.TagGroupGetTagAsLong( "Y Size", nsy )
FrameTags.TagGroupGetTagAsLong( "Z Size", nsz )
TagGroup nFrameList
FrameTags.TagGroupGetTagAsTagGroup( "frames", nFrameList )
number nsf = nFrameList.TagGroupCountTags()
image after := RealImage( "After", 4, nsx, nsy, nsz, nsf )
for( number f = 0; f < nsf; f++ )
{
image addFrame := RealImage( "Frame", 4, sx, sy, sz )
FrameList.TagGroupGetIndexedTagAsArray( f, addFrame )
after.SliceN( 4,4, 0,0,0,f, 0,sx,1, 1,sy,1, 2,sz,1, 3,1,1) = addFrame
}
after.ShowImage()