Problem adding two numbers

11 ビュー (過去 30 日間)
Sneha Fariha
Sneha Fariha 2020 年 5 月 30 日
コメント済み: Adam Danz 2020 年 6 月 1 日
I'm trying to write a function for blurring images. (I'm aware of imfilter command. Just trying to implement it in my own way). I've the following function which takes a uint8 matrix as argument img.
function output=bl(img,w)
row=size(img,1);
col=size(img,2);
g_size=(2*w+1)^2;
output=uint8(zeros(row,col));
for ii=1:row
for jj=1:col
s=0;
corner=[ii jj]-w;
for r=corner(1):(corner(1)+(2*w))
for c=corner(2):(corner(2)+(2*w))
if((r>0 && r<=row) && (c>0 && c<=col))
s=s+img(r,c); %PROBLEMATIC LINE
fprintf('The value of s is %d\n',s);
end
end
end
m=s/g_size;
output(ii,jj)=fix(m);
s=0;
end
end
end
I'm having trouble with the line marked as "PROBLEMATIC LINE". The line of code was supposed to add previous value of s with img(c,r). But surprisingly each time the line runs, the previous value of s becomes 0. What am I doing wrong here? How can I solve this?
  4 件のコメント
John D'Errico
John D'Errico 2020 年 5 月 30 日
Another important question is what is the class of the variable img? Let me guuess, is this an image? If so, is the array img a uint8 array?
Adam Danz
Adam Danz 2020 年 5 月 30 日
The OP described the input as a uint8 matrix.
@Sneha Fariha, Image Analyst brings up a good point. My answer doesn't address the blurring proceedure. It just addresses the probem you described in the value of s.

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

回答 (1 件)

Sneha Fariha
Sneha Fariha 2020 年 5 月 31 日
Well, I actually found the problem. The problem was that, s=s+img(r,c); in this line I was trying to add variables of two different types. And the maximum value of uint8 cannot exceed 255. So, after updating s for first time (s=255) it was unable to add numbers anymore and I was thinking that s is being initialized to 0 each time the line s=s+img(r,c); runs. Casting to double solves the problem.
  1 件のコメント
Adam Danz
Adam Danz 2020 年 6 月 1 日
Well done. Thanks for the follow-up.

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

カテゴリ

Help Center および File ExchangeImage Processing Toolbox についてさらに検索

製品


リリース

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by