Apply mean and standard deviation of an image to another image

1 次查看(过去 30 天)
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?

采纳的回答

Guillaume
Guillaume 2016-10-11
编辑:Guillaume 2016-10-11
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;
  6 个评论
Adam
Adam 2016-10-11
编辑:Adam 2016-10-11
The problem of standard deviation not being the same is that you calculate it wrongly in the code in the question. Mean is a separable operation that you can do as you are doing (though there is no need to do so), std is not.
std2 as Guillaume uses works correctly or as I usually do more generically, just
std( tmpimg(:) )
since it is a statistical operation and the structure of the data in a 2d matrix rather than 1d is not relevant.

请先登录,再进行评论。

更多回答(1 个)

Adam
Adam 2016-10-11
tmpimg = imsubtract(tmpimg,meanint2);
tmpimg = imdivide(tmpimg,stdint2);
tmpimg = immultiply(tmpimg,stdint);
tmpimg = imadd(tmpimg,meanint);
seems a more appropriate ordering and gives me the same mean.
  2 个评论
Efstathios Kontolatis
编辑:Efstathios Kontolatis 2016-10-11
For which of the two if? Also, the standard deviation is not the same.
Adam
Adam 2016-10-11
I don't see the need for two cases, why is the code different depending whether the mean is greater or smaller?

请先登录,再进行评论。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by