Apply mean and standard deviation of an image to another image
4 ビュー (過去 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?
0 件のコメント
採用された回答
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
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
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 件のコメント
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!