Frequently, I save both a cropped image and its uncropped parent. Later, I often want to precisely register the cropped version with its uncropped parent or some other image, for which I need to know the offsets between the two images. One way this could be done would be to write the needed x- and y-offsets in the header of the cropped image file, and then expose them for use when an image is opened.
There are several candidate tiff tags that seem suitable for this (e.g. x- and y-position tags, 286 and 287 respectively), but they aren't currently implemented in TiffEncoder or TiffDecoder. I'm wondering how I can use these tags so that the offset information can be utilized for registration in ImageJ and in other programs. In particular, if I modify the TiffEncoder source so that the tag information gets saved in a newly created cropped image file, what will happen if I open such an image file with a version of ImageJ that has not been modified? Will TiffDecode simply ignore this tag it knows nothing about? Of course, such offsets in an image file are only meaningful in relation to some other file (one it is intended to be registered to), so I'll need to think through how to best make use of offset information. But for now, can anyone describe how this problem is usually approached or offer suggestions for how I should be thinking about it? Tiff tags 286 and 287 are in resolution (i.e. distance) units, while what I'd really like is to directly use pixel units; are other tiff tags more appropriate for this? Have I missed some existing capability in ImageJ that would facilitate this? Thanks. Bill |
Hi Bill,
A similar thing was done for the Make Montage command to set some meta data in the produced montage, to remember the used layout. Here, you would need the 'Crop' function to set metadata about what it does: getSelectionBounds(x, y, w, h); ci="cropInfo="+x+","+y+","+w+","+h; run("Crop"); setMetadata (ci); run("Show Info..."); This meta data will be saved with your tif image anyway, so you do not need using any special tif tag. Then you can use getMetadata() to retrieve the cropInfo values. This example crops a region from one image and pastes it in a different image, at the original position, using the cropInfo metedata: run("AuPbSn 40 (56K)"); makeRectangle(90, 61, 136, 98); getSelectionBounds(x, y, w, h); ci="cropInfo="+x+","+y+","+w+","+h; run("Crop"); setMetadata (ci); run("Copy"); // or save as tif List.setList(getMetadata); c = split(List.get("cropInfo"),","); run("Boats (356K)"); makeRectangle(c[0], c[1], c[2], c[3]); run("Paste"); /// end Jerome On Sat, May 1, 2010 at 3:48 AM, Bill Christens-Barry <[hidden email] > wrote: > Frequently, I save both a cropped image and its uncropped parent. Later, I > often want to precisely register the cropped version with its uncropped > parent or some other image, for which I need to know the offsets between the > two images. One way this could be done would be to write the needed x- and > y-offsets in the header of the cropped image file, and then expose them for > use when an image is opened. > > There are several candidate tiff tags that seem suitable for this (e.g. x- > and y-position tags, 286 and 287 respectively), but they aren't currently > implemented in TiffEncoder or TiffDecoder. I'm wondering how I can use these > tags so that the offset information can be utilized for registration in > ImageJ and in other programs. In particular, if I modify the TiffEncoder > source so that the tag information gets saved in a newly created cropped > image file, what will happen if I open such an image file with a version of > ImageJ that has not been modified? Will TiffDecode simply ignore this tag it > knows nothing about? > > Of course, such offsets in an image file are only meaningful in relation to > some other file (one it is intended to be registered to), so I'll need to > think through how to best make use of offset information. But for now, can > anyone describe how this problem is usually approached or offer suggestions > for how I should be thinking about it? Tiff tags 286 and 287 are in > resolution (i.e. distance) units, while what I'd really like is to directly > use pixel units; are other tiff tags more appropriate for this? Have I > missed some existing capability in ImageJ that would facilitate this? > > Thanks. > > Bill > |
In reply to this post by Bill Christens-Barry
On Saturday 01 May 2010, you wrote:
> Frequently, I save both a cropped image and its uncropped parent. Later, I > often want to precisely register the cropped version with its uncropped > parent or some other image, for which I need to know the offsets between > the two images. One way this could be done would be to write the needed x- > and y-offsets in the header of the cropped image file, and then expose > them for use when an image is opened. The new tags, if you implement them, I guess will only by understood by other programs if they know what they mean and what to do with them. Without having to modify the tiff encoder/decoder, if you want these tags to be used by other IJ macros or plugins, perhaps it would be enough to include the info in the metadata (the "Info" key, we discussed this yesterday) rather than the tiff header and include some tags or keys so you can later retrieve their data by parsing the Info property (a string) retrieved via the getMetadata macro command, the ImpProps plugin or javascript. In that string you can include the title of the parent image and the corresponding coordinates that your current image correspond to in the parent image. Michael Schmid kindly posted yesterday macro code to get those lists of tags. The Info property is stored in the TIFF header and saved, but you can also add other properties to the images that are set via the ImpProps plugin (these are not saved) that can be used to pass data around between images and scripts while the images are open. I hope it helps. G. |
In reply to this post by Bill Christens-Barry
Thanks, Jerome, Gabriel, and Wayne for the helpful response information you gave. Here's what I had come up with, based on one of Wayne's examples, using metadata and properties:
macro "UseOffsets [1]" { print("\\Clear"); properties = getMetadata("Info"); List.setList(properties); if(lengthOf(List.get("X-offset"))>0 || lengthOf(List.get("Y-offset"))>0) { x0 = parseInt(List.get("X-offset")); print("X-offset: " + List.get("X-offset")); y0 = parseInt(List.get("Y-offset")); print("Y-offset: " + List.get("Y-offset")); } if(lengthOf(List.get("X-offset"))==0 && lengthOf(List.get("Y-offset"))==0) { x0 = 0; y0 = 0; print("No \"X-offset\" or \"Y-offset\" properties defined."); } if(selectionType()==0) { getSelectionBounds(xoff, yoff, wid, hgt); run("Crop"); List.clear; List.set("Name", getTitle); List.set("Dir", getDirectory("image")); List.set("Size", getWidth*getHeight); List.set("X-offset", x0 + xoff); List.set("Y-offset", y0 + yoff); setMetadata("Info", List.getList); path = getDirectory("temp")+"test.tif"; saveAs("tif", path); close; open(path); properties = getMetadata("Info"); List.setList(properties); print(path); print("Name: "+List.get("Name")); print("Dir: "+List.get("Dir")); print("Size: "+List.get("Size")); print("X-offset: "+List.get("X-offset")); print("Y-offset: "+List.get("Y-offset")); } } This is meant to cover the case of sequential cropping, referenced back to an original source image. I'm still hoping to put the offsets into a tiff tag so they can be utilized by other programs, but this works fine with ImageJ. Bill |
Free forum by Nabble | Edit this page |