フィルターのクリア

How to write own standard deviation function.

1 回表示 (過去 30 日間)
anu
anu 2016 年 7 月 20 日
回答済み: Image Analyst 2016 年 7 月 20 日
I have read the color image. Then separated RGB values into three different arrays. After that I have written my own function to calculate standard deviation function for each color component. But when I execute my own written function and in built function I got different values? What is wrong int it?
without inbuilt function
im = imread('D:\im112.jpg');
R=im(:,:,1)
[r,c]=size(R);
totmean=sum(R(:))/(r*c);
totdiff=(R-totmean).^2;
totsum=sum(totdiff(:));
nele=(r*c)-1;
totvar=totsum/nele;
totstd=sqrt(totvar);
display(totstd);
Using inbuilt functio
stdr=std(double(R(:)))
  1 件のコメント
Adam
Adam 2016 年 7 月 20 日
編集済み: Adam 2016 年 7 月 20 日
Is your image in integer format? You convert to double to call the builtin, but seemingly not for your own code. I don't know off the top of my head what the result of all those operations is on integers.

サインインしてコメントする。

採用された回答

Guillaume
Guillaume 2016 年 7 月 20 日
Adam hit the nail on the head in his comment. You're reading a jpg image so most likely the im array and thus R is of type uint8 (8 bit integer). The range for uint8 value is 0 to 255. If any operation involving uint8 overflows the result is clamped to 255 and similarly underflows are clamped to 0.
Therefore, it's highly likely that the result of sum(R(:)) is clamped to 255. Then you do a division. Because the result is still going to be uint8 the result is going to be rounded down to the nearest integer (most likely 0, since 255 divided by the image area is probably less than 1).
To solve this, you indeed need to do the same as you've done for std. First convert your R to double, then all operations will be performed correctly.
  1 件のコメント
anu
anu 2016 年 7 月 20 日
Thanks. When I converted R to double, it gives me correct answer.

サインインしてコメントする。

その他の回答 (2 件)

Andrei Bobrov
Andrei Bobrov 2016 年 7 月 20 日
n = numel(R);
yourstd = sqrt(sum((R(:) - sum(R(:))/n).^2)/(n - 1));

Image Analyst
Image Analyst 2016 年 7 月 20 日
Why not simply use std2() - the built in function meant for this????
img = imread('moon.tif');
s = std2(img) % No casting to double needed.

カテゴリ

Help Center および File ExchangeConvert Image Type についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by