normalizing mean intensities (grayscale)
Posted: 2017-09-04T19:30:45-07:00
I have two 16-bit, grayscale images, img1 and img2 and would like to equalize their intensities such that the mean pixel value of each image is the same. In Matlab, I would do this as follows:
I wanted to implement this in ImageMagick and came up with the following, which seems to work:
My questions are the following:
1. Is there a need to worry about image type when performing such conversions? Based on the fact that the output (img2_eq.tif) appears fine and is still 16-bit grayscale, it appears that ImageMagick handled integer to floating point to integer conversions transparently.
2. Is there a way to achieve this in one line (assuming that the mean of the reference image is precalculated and supplied as a shell variable)? Something along the lines of the following pseudocode:
Many thanks.
Code: Select all
img1 = im2double(imread('img1.tif'));
img2 = im2double(imread('img2.tif'));
img2_eq = im2uint16( img2 ./ mean2(img2) .* mean2(img1) );
Code: Select all
img1_mean=$(convert img1.tif -format "%[mean]" info:)
img2_mean=$(convert img2.tif -format "%[mean]" info:)
ratio=$(echo "x=$img1_mean/$img2_mean; scale=5; x/1" | bc -l)
convert img2.tif -evaluate Multiply $ratio img2_eq.tif
1. Is there a need to worry about image type when performing such conversions? Based on the fact that the output (img2_eq.tif) appears fine and is still 16-bit grayscale, it appears that ImageMagick handled integer to floating point to integer conversions transparently.
2. Is there a way to achieve this in one line (assuming that the mean of the reference image is precalculated and supplied as a shell variable)? Something along the lines of the following pseudocode:
Code: Select all
convert img2.tif -evaluate Divide "%[mean]" -evaluate Multiply $img1_mean img2_eq.tif