Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
a frustrating bug

Subject: a frustrating bug

From: Tanmoy

Date: 24 Nov, 2010 15:20:05

Message: 1 of 11

let' say I have an 2D gray image of size M rows and N cols..
now I want to get the average intensity of the image, so after summing up all the intensity values and we will divide that by M*N...okay, it's is really simple... isn't it..
here's our simple & cute code:

sum = 0;
for rw=1:1:M
     for cw = 1:1:N
        sum = sum + Image(rw,cw);
     end
end
avg = sum/(M*N);


This should give me the answer.. isn't it...
But it will show a bug..
the value of sum after coming out from both the loop will be 255, it will not ever increase..

it might be for the capacity of the variable to hold a particular range of number which fails...
but there is no alternatives in Matlab, as far my knowledge is concerned.
   

Subject: a frustrating bug

From: Matt J

Date: 24 Nov, 2010 15:32:05

Message: 2 of 11


avg= mean(double(Image(:)));

Subject: a frustrating bug

From: ImageAnalyst

Date: 24 Nov, 2010 15:53:45

Message: 3 of 11

Tanmoy:
It's very bad practice to override the definition of the built in
MATLAB function called "sum" - I advise you not to do that. You're
also *almost* doing it with "Image" but saved because MATLAB is case
sensitive. Nonetheless I advise you to pick a different name for that
variable also.

I hope you have the Image Processing Toolbox because you're dealing
with images. If you do, you can use the mean2() function.

meanGrayLevel = mean2(uint8GrayImageArray);

No need to cast the image into double first. Your method did not work
because sum was probably a uint8 variable and that gets clipped once
you reach 255 - you can't add anything more to it after that.
ImageAnalyst

Subject: a frustrating bug

From: Doug Schwarz

Date: 24 Nov, 2010 15:54:07

Message: 4 of 11

On 11/24/2010 10:32 AM, Matt J wrote:
>
> avg= mean(double(Image(:)));

Actually, you don't need to convert to double; mean() does that already.

   avg = mean(Image(:));

--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.

Subject: a frustrating bug

From: Matt J

Date: 24 Nov, 2010 16:40:09

Message: 5 of 11

Doug Schwarz <see@sig.for.address.edu> wrote in message <AyaHo.61520$d67.30349@newsfe16.iad>...
> On 11/24/2010 10:32 AM, Matt J wrote:
> >
> > avg= mean(double(Image(:)));
>
> Actually, you don't need to convert to double; mean() does that already.
=======

Hmmm, funny it doesn't do the same for single type (as shown by the following discrepancy)


>> x=rand(1e8,1,'single'); mean(x), sum(x,'double')/length(x)

ans =

    0.3355


ans =

    0.5000

Subject: a frustrating bug

From: Sean de

Date: 24 Nov, 2010 17:03:04

Message: 6 of 11

"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <icjf59$dm2$1@fred.mathworks.com>...
> Doug Schwarz <see@sig.for.address.edu> wrote in message <AyaHo.61520$d67.30349@newsfe16.iad>...
> > On 11/24/2010 10:32 AM, Matt J wrote:
> > >
> > > avg= mean(double(Image(:)));
> >
> > Actually, you don't need to convert to double; mean() does that already.
> =======
>
> Hmmm, funny it doesn't do the same for single type (as shown by the following discrepancy)
>
>
> >> x=rand(1e8,1,'single'); mean(x), sum(x,'double')/length(x)
>
> ans =
>
> 0.3355
>
>
> ans =
>
> 0.5000

It also seems obscure that the 0.3355 is replicable every time?

Subject: a frustrating bug

From: Matt Fig

Date: 24 Nov, 2010 18:05:07

Message: 7 of 11

"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message
> Hmmm, funny it doesn't do the same for single type (as shown by the following discrepancy)
>
>
> >> x=rand(1e8,1,'single'); mean(x), sum(x,'double')/length(x)
>
> ans =
>
> 0.3355
>
>
> ans =
>
> 0.5000


It is the SUM function, called within MEAN, that is converting uint8 to double.

x = uint8(rand(2000,1)*255);
x = sum(x);
whos x
  Name Size Bytes Class Attributes

  x 1x1 8 double

Subject: a frustrating bug

From: Matt J

Date: 24 Nov, 2010 18:23:06

Message: 8 of 11

"Matt Fig" <spamanon@yahoo.com> wrote in message <icjk4j$a1s$1@fred.mathworks.com>...
>
> It is the SUM function, called within MEAN, that is converting uint8 to double.
>
> x = uint8(rand(2000,1)*255);
> x = sum(x);
> whos x
> Name Size Bytes Class Attributes
>
> x 1x1 8 double


Equally strange, isn't it?

Subject: a frustrating bug

From: dpb

Date: 24 Nov, 2010 19:08:55

Message: 9 of 11

Matt J wrote:
> "Matt Fig" <spamanon@yahoo.com> wrote in message
> <icjk4j$a1s$1@fred.mathworks.com>...
>>
>> It is the SUM function, called within MEAN, that is converting uint8
>> to double.
>>
>> x = uint8(rand(2000,1)*255);
>> x = sum(x);
>> whos x
>> Name Size Bytes Class Attributes
>>
>> x 1x1 8 double
>
>
> Equally strange, isn't it?

What else could/would you have ML do--silently overflow or abort/error???

--

Subject: a frustrating bug

From: Matt Fig

Date: 24 Nov, 2010 19:38:03

Message: 10 of 11

"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message
> Equally strange, isn't it?

Strange or not, it is documented in the SUM help.

Subject: a frustrating bug

From: Matt J

Date: 24 Nov, 2010 20:49:03

Message: 11 of 11

dpb <none@non.net> wrote in message <icjo4n$omt$2@news.eternal-september.org>...
>> > Equally strange, isn't it?
>
> What else could/would you have ML do--silently overflow or abort/error???
======

It already silently overflows for type single (as my example showed).



"Matt Fig" <spamanon@yahoo.com> wrote in message <icjpir$nai$1@fred.mathworks.com>...
> "Matt J " <mattjacREMOVE@THISieee.spam> wrote in message
> > Equally strange, isn't it?
>
> Strange or not, it is documented in the SUM help.

Hadn't noticed that, but so be it...

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us