users > RE: Histogram normalization, andreslicing
Aug 22, 2017  08:08 AM
Torsten Rohlfing
Google Inc.
RE: Histogram normalization, andreslicing
Hi Eli -

As Greg already mentioned, the "warp" tool is not what you want. It can do histogram equalization internally prior to aligning two images, but that's just for convenience and does not let you export the normalized image.

Either imagemath or convertx should do what you want. IIRC the command line switch in both cases is --match-histograms. The operation of convertx is perhaps a bit more intuitive, since it follows a conceptual pattern of "input, modify, output". The imagemath tool, on the other hand, allows you to apply almost arbitrary sequences of operations to an arbitrary number of images, but that flexibility comes with the price of using an image stack and applying operations to the top image(s) on the stack. If that sounds confusing, that's probably a good sign to stick with convertx for operations that both tools support.

You asked for a command line, and my best guess (sorry, it's been a long time) is that this should work:

convertx --match-histograms RefImage InputImage OutputImage

That should match the histogram of InputImage to that of RefImage and write the result to OutputImage.

If that doesn't work, try convertx --help and check whether my memory was correct on the syntax.

Now for reslicing - the tool you want for that is either convertx or reformatx.

The convertx tool is simpler but can only do downsampling by integer facters. The two options of interest are --downsample-select and --downsample average. Each takes either a single downsample factor, or a comma-separated list of three (one per image axis). The former will simply use the value of a single corresponding pixel from the input image, whereas the latter will average the input image pixels that make up each output pixel. Which one you want depends on your application. For label images, you'd definitely want the "select" option. For intensity images, you're likely better off using the "average" variant.

If you have to upsample one image, though, or if your downsampling factors aren't integers, then you'll likely need to use the reformatx tool. Its general use is like so:

reformatx --floating MovingImage --output OutImage FixedImage

This should reslice the pixels in MovingImage to the grid of FixedImage and write the result to OutputImage. The reason for the somewhat un-intuitive order of arguments is that after FixedImage, you can optionally provide a sequence of (optionally inverted) coordinate transformations that define the mapping from FixedImage to MovingImage coordinates. But it sounds like your images are pre-aligned, so I would ignore that.

Instead, a word of caution about "pre-aligned" images then - there is likely going to be a half-pixel shift between your images if you simply apply reformatx as per the instructions above. The reason for that is in the pixel model that CMTK uses internally - basically, a pixel is a grid point, but it does not have a spatial extent. So what you might think of as "pixel size" is really the distance between two adjacent pixels, but the pixels themselves have no size. (Why is that? Because otherwise pixels on the image borders would have different sizes from interior pixels, or extend outside the image, each of which is adding a lot of complication for no real benefit. But I digress.)

Anyway, the result of that pixel model is this - the extent of an image with n pixels of "size" (really, distance) d, is (n-1)*d, not n*d. So if you have twice as many pixels, the extent would be (2n-1)*d, which is *not* twice the size of the former image (that would be (2n-2)*d). As a result, the pixels in the two images will not align if you simply align the image origins. Instead, they will be shifted.

Anyway, long story short, if you need to upsample or your images have pixel counts that aren't multiples of each other along each axis, then the whole thing gets a little complicated.
reply reply with quote
Threaded View
Histogram normalization, and reslicingElijah RockersAug 21, 2017
      RE: Histogram normalization, and reslicingElijah RockersAug 21, 2017
             RE: Histogram normalization, andreslicingGreg JefferisAug 21, 2017
                   RE: Histogram normalization, andreslicingTorsten RohlfingAug 22, 2017
                         RE: Histogram normalization, andreslicingElijah RockersSep 1, 2017
                               RE: Histogram normalization, andreslicingTorsten RohlfingSep 1, 2017