MATLAB: Apply mean and standard deviation of an image to another image

mean of an imagestandard deviation of an image

Hi I have an image and I calculate the mean and standard deviation like this
meanint=mean(mean(tmpimg));
stdint=std(std(tmpimg));
Now I have another image and I try to make it have the same mean and standard deviation as the previous image. That's what I do
if (meanint2>meanint(1))
tmpimg = imsubtract(tmpimg,meanint2);
tmpimg = imadd(tmpimg,meanint);
tmpimg = imdivide(tmpimg,stdint2);
tmpimg = immultiply(tmpimg,stdint);
elseif (meanint2<meanint(1))
tmpimg = imadd(tmpimg,meanint);
tmpimg = imsubtract(tmpimg,meanint2);
tmpimg = immultiply(tmpimg,stdint);
tmpimg = imdivide(tmpimg,stdint2);
end
But I don't have the result I want. Does anyone knows why?

Best Answer

Really, this can be done with just one operation, with much less error accumulation and chance of truncation (if integer images):
meanreference = mean2(referenceimage);
stdreference = std2(referenceimage);
meantoscale = mean2(imagetoscale);
stdtoscale = std2(imagetoscale);
adjustedimage = imlincomb(stdreference / stdtoscale, imagetoscale, ...
meanreference - meantoscale * stdreference / stdtoscale);
If the image is double I wouldn't even bother with the imlincomb:
adjustedimage = meanreference + (imagetoscale - meantoscale) * stdreference / stdtoscale;
Related Question