I am trying to assemble a mosaic of some images with a custom placement of the scaled down tiles.
The problem is that I am unable to do this because of what appears to be a bug in the run("Scale..." command. The problem appears to be that when specifying the scale factors in the run("Scale... command, the result image size is calculated by truncation rather than rounding when doing the float to int conversion of the image size. I therefore think this is a bug. The behaviour is the same when using the GUI instead of a macro. The demo macro below reproduces the problem. The intended behaviour is that a ROI is made according to the size calculated by multiplying the image size and the scale factor. Then the run("Scale..." command is applied to create a shrunk image, which is copied and then placed in the ROI at the desired position (in this example case the upper left corner). Due to a round-off error, the calculated ROI size and scaled result size do not match, so the paste ends in the image centre. Even when specifying the exact pixel size for the scaling, the result image size is still wrong. The macro produces correct result with the "Line Graph (21K)" demo image, and wrong result with "Dot Blot (7K)" image. So you may try with "Line Graph (21K)" first to see the intended results. Demo macro: ------------------------------ run("Close All"); print("\\Clear"); TestImageName = "Dot Blot (7K)"; run(TestImageName); ImageSizeX = getWidth; ImageSizeY = getHeight; ScaleFactor = 1/5; print("ImageSize: ",ImageSizeX,ImageSizeY); print("ScaleFactor:",ScaleFactor); print("ImageSize*ScaleFactor: ",ImageSizeX*ScaleFactor, ImageSizeY*ScaleFactor); TileSizeX = round(ImageSizeX*ScaleFactor); TileSizeY = round(ImageSizeY*ScaleFactor); print("round(ImageSize*ScaleFactor): ",TileSizeX,TileSizeY); print("Target ROI size: ",TileSizeX,TileSizeY); //scale using the relative scale factors run("Scale...", "x=&ScaleFactor y=&ScaleFactor interpolation=Bilinear average create title=temp"); print("Relative scaled size:",getWidth,getHeight); //result size is mathematically wrong here run("Select All"); run("Copy"); close(); makeRectangle(0,0, TileSizeX,TileSizeY); //upper left corner run("Paste"); //pasted in middle instead because of size mismatch //retry with a workaround; scale to fixed size instead of relative run(TestImageName); print("Fixed scale size: ",TileSizeX,TileSizeY); //= target ROI size run("Scale...", "width=&TileSizeX height=&TileSizeY interpolation=Bilinear average create"); print("Result scaled size:",getWidth,getHeight); //result size is still wrong! run("Select All"); run("Copy"); close(); makeRectangle(0,0, TileSizeX,TileSizeY); run("Paste"); //repeat with this image and then the target and scaled sizes match TestImageName = "Line Graph (21K)"; ------------------------------ I am using daily build ImageJ 1.52m with Java 1.6 on Windows 7/64-bit. Stein -------------------------------------------------------------------------------- -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
> On Feb 12, 2019, at 5:47 AM, Stein Rørvik <[hidden email]> wrote:
> > I am trying to assemble a mosaic of some images with a custom placement of the scaled down tiles. > The problem is that I am unable to do this because of what appears to be a bug in the run("Scale..." command. > > The problem appears to be that when specifying the scale factors in the run("Scale... command, the result image size is calculated by truncation rather than rounding when doing the float to int conversion of the image size. I therefore think this is a bug. The behaviour is the same when using the GUI instead of a macro. This bug is fixed in the latest ImageJ daily build (1.52m13). -wayne > The demo macro below reproduces the problem. > > The intended behaviour is that a ROI is made according to the size calculated by multiplying the image size and the scale factor. > Then the run("Scale..." command is applied to create a shrunk image, which is copied and then placed in the ROI at the desired position (in this example case the upper left corner). > > Due to a round-off error, the calculated ROI size and scaled result size do not match, so the paste ends in the image centre. > Even when specifying the exact pixel size for the scaling, the result image size is still wrong. > > The macro produces correct result with the "Line Graph (21K)" demo image, and wrong result with "Dot Blot (7K)" image. > So you may try with "Line Graph (21K)" first to see the intended results. > > Demo macro: > ------------------------------ > run("Close All"); > print("\\Clear"); > TestImageName = "Dot Blot (7K)"; > > run(TestImageName); > ImageSizeX = getWidth; > ImageSizeY = getHeight; > ScaleFactor = 1/5; > print("ImageSize: ",ImageSizeX,ImageSizeY); > print("ScaleFactor:",ScaleFactor); > print("ImageSize*ScaleFactor: ",ImageSizeX*ScaleFactor, ImageSizeY*ScaleFactor); > TileSizeX = round(ImageSizeX*ScaleFactor); > TileSizeY = round(ImageSizeY*ScaleFactor); > print("round(ImageSize*ScaleFactor): ",TileSizeX,TileSizeY); > print("Target ROI size: ",TileSizeX,TileSizeY); > //scale using the relative scale factors > run("Scale...", "x=&ScaleFactor y=&ScaleFactor interpolation=Bilinear average create title=temp"); > print("Relative scaled size:",getWidth,getHeight); //result size is mathematically wrong here > run("Select All"); > run("Copy"); > close(); > makeRectangle(0,0, TileSizeX,TileSizeY); //upper left corner > run("Paste"); //pasted in middle instead because of size mismatch > > //retry with a workaround; scale to fixed size instead of relative > run(TestImageName); > print("Fixed scale size: ",TileSizeX,TileSizeY); //= target ROI size > run("Scale...", "width=&TileSizeX height=&TileSizeY interpolation=Bilinear average create"); > print("Result scaled size:",getWidth,getHeight); //result size is still wrong! > run("Select All"); > run("Copy"); > close(); > makeRectangle(0,0, TileSizeX,TileSizeY); > run("Paste"); > > //repeat with this image and then the target and scaled sizes match > TestImageName = "Line Graph (21K)"; > ------------------------------ > > > I am using daily build ImageJ 1.52m with Java 1.6 on Windows 7/64-bit. > > > Stein > > -------------------------------------------------------------------------------- > > > -- > ImageJ mailing list: http://imagej.nih.gov/ij/list.html -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Thanks for the quick fix, this works as advertised!
When testing further I found another issue which is not necessarily a bug, but unexpected behaviour. The demo macro below scales first using a relative factor, then using fixed sizes. The result image size in the latter case is the same as in the previous call of the Scale... command. It seems that when scale factors are not specified, the previous scale factors is used instead of the current specified size. This is unexpected behaviour that can be hard to debug. One would expect that present parameters takes precedence over missing parameters. The workaround is to set dashes for the missing scalefactors: run("Scale...", "x=- y=- width=&TargetSizeX height=&TargetSizeY interpolation=Bilinear average create"); Perhaps this should be the default behaviour, when the pixel width and height are specified instead of scale factors. Demo macro: ----------------- run("Close All"); print("\\Clear"); SourceSizeX = 768; SourceSizeY = 576; newImage("Source", "8-bit ramp", SourceSizeX, SourceSizeY, 1); print("Source Size: ", getWidth,getHeight); SourceImage = getImageID(); ScaleFactor = 0.5; print("ScaleFactor: ", ScaleFactor); print("Relative Scaled Size: ", getWidth*ScaleFactor,getHeight*ScaleFactor); run("Scale...", "x=&ScaleFactor y=&ScaleFactor interpolation=Bilinear average create"); print("Relative Scaled Result: ", getWidth,getHeight); print(""); newImage("Source", "8-bit ramp", SourceSizeX, SourceSizeY, 1); print("Source Size: ", getWidth,getHeight); ScaleFactor = 0.2; print("ScaleFactor: ", ScaleFactor); TargetSizeX = round(getWidth*ScaleFactor); TargetSizeY = round(getHeight*ScaleFactor); print("Fixed Scale Size: ", TargetSizeX,TargetSizeY); run("Scale...", "width=&TargetSizeX height=&TargetSizeY interpolation=Bilinear average create"); print("Fixed Scale Result: ", getWidth,getHeight); //this is not the specified size selectWindow("Log"); ----------------- I am using daily build ImageJ 1.52m with Java 1.6 on Windows 7/64-bit. Stein -----Original Message----- From: ImageJ Interest Group <[hidden email]> On Behalf Of Wayne Rasband Sent: 12. februar 2019 16:15 To: [hidden email] Subject: Re: Image Scaling gives a wrong result size > On Feb 12, 2019, at 5:47 AM, Stein Rørvik <[hidden email]> wrote: > > I am trying to assemble a mosaic of some images with a custom placement of the scaled down tiles. > The problem is that I am unable to do this because of what appears to be a bug in the run("Scale..." command. > > The problem appears to be that when specifying the scale factors in the run("Scale... command, the result image size is calculated by truncation rather than rounding when doing the float to int conversion of the image size. I therefore think this is a bug. The behaviour is the same when using the GUI instead of a macro. This bug is fixed in the latest ImageJ daily build (1.52m13). -wayne > The demo macro below reproduces the problem. > > The intended behaviour is that a ROI is made according to the size calculated by multiplying the image size and the scale factor. > Then the run("Scale..." command is applied to create a shrunk image, which is copied and then placed in the ROI at the desired position (in this example case the upper left corner). > > Due to a round-off error, the calculated ROI size and scaled result size do not match, so the paste ends in the image centre. > Even when specifying the exact pixel size for the scaling, the result image size is still wrong. > > The macro produces correct result with the "Line Graph (21K)" demo image, and wrong result with "Dot Blot (7K)" image. > So you may try with "Line Graph (21K)" first to see the intended results. > > Demo macro: > ------------------------------ > run("Close All"); > print("\\Clear"); > TestImageName = "Dot Blot (7K)"; > > run(TestImageName); > ImageSizeX = getWidth; > ImageSizeY = getHeight; > ScaleFactor = 1/5; > print("ImageSize: ",ImageSizeX,ImageSizeY); > print("ScaleFactor:",ScaleFactor); > print("ImageSize*ScaleFactor: ",ImageSizeX*ScaleFactor, > ImageSizeY*ScaleFactor); TileSizeX = round(ImageSizeX*ScaleFactor); > TileSizeY = round(ImageSizeY*ScaleFactor); > print("round(ImageSize*ScaleFactor): ",TileSizeX,TileSizeY); > print("Target ROI size: ",TileSizeX,TileSizeY); //scale using the > relative scale factors run("Scale...", "x=&ScaleFactor y=&ScaleFactor > interpolation=Bilinear average create title=temp"); > print("Relative scaled size:",getWidth,getHeight); //result size is mathematically wrong here > run("Select All"); > run("Copy"); > close(); > makeRectangle(0,0, TileSizeX,TileSizeY); //upper left corner > run("Paste"); //pasted in middle instead because of size mismatch > > //retry with a workaround; scale to fixed size instead of relative > run(TestImageName); > print("Fixed scale size: ",TileSizeX,TileSizeY); //= target ROI size > run("Scale...", "width=&TileSizeX height=&TileSizeY > interpolation=Bilinear average create"); print("Result scaled size:",getWidth,getHeight); //result size is still wrong! > run("Select All"); > run("Copy"); > close(); > makeRectangle(0,0, TileSizeX,TileSizeY); run("Paste"); > > //repeat with this image and then the target and scaled sizes match > TestImageName = "Line Graph (21K)"; > ------------------------------ > > > I am using daily build ImageJ 1.52m with Java 1.6 on Windows 7/64-bit. > > > Stein > > ---------------------------------------------------------------------- > ---------- > > > -- > ImageJ mailing list: http://imagej.nih.gov/ij/list.html -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Free forum by Nabble | Edit this page |