Thread Subject: how to calculate histogram using one for loop

Subject: how to calculate histogram using one for loop

From: Jamal Mohamed

Date: 17 Oct, 2009 09:05:19

Message: 1 of 8

hi everybody

already i calculated histogram for a image like following code [ only calculation part]
[row, col] =size(I);
h = zeros(256, 1);
for i=1:row
for j=1:col
Z=I(i,j); % I is image
h(Z+1)=h(Z+1)+1;
end
end

my question is how to calculate histogram using one for loop
that is each row by row or column by column calculate histogram first [vector based] and at end sum up all for histogram of image. is it possible ? if yes tell me solution and code

Subject: how to calculate histogram using one for loop

From: Nasser M. Abbasi

Date: 17 Oct, 2009 09:31:26

Message: 2 of 8


"Jamal Mohamed" <in.jamal@yahoo.com> wrote in message
news:hbc1ce$1u$1@fred.mathworks.com...
> hi everybody
>
> already i calculated histogram for a image like following code [ only
> calculation part]
> [row, col] =size(I);
> h = zeros(256, 1);
> for i=1:row
> for j=1:col
> Z=I(i,j); % I is image
> h(Z+1)=h(Z+1)+1;
> end
> end
>
> my question is how to calculate histogram using one for loop
> that is each row by row or column by column calculate histogram first
> [vector based] and at end sum up all for histogram of image. is it
> possible ? if yes tell me solution and code


Why not flatten the image into a 1-D vector using reshape and then you will
only use one loop?

 something like

image=[0 90 255; 2 4 5; 255 10 200];
[row,col]=size(image)
data=reshape(image,row*col,1)
h=zeros(256,1);

for i=1:length(data)
    z=data(i);
    h(z+1)=h(z+1)+1;
end


--Nasser

Subject: how to calculate histogram using one for loop

From: Rune Allnor

Date: 17 Oct, 2009 09:34:03

Message: 3 of 8

On 17 Okt, 11:05, "Jamal Mohamed" <in.ja...@yahoo.com> wrote:
> hi everybody
>
> already i calculated  histogram for a image like following code [ only calculation part]
> [row, col] =size(I);
> h = zeros(256, 1);
> for i=1:row
> for j=1:col
> Z=I(i,j);           % I is image
> h(Z+1)=h(Z+1)+1;
> end
> end

The above is the conceptually correct solution.

> my question is how to calculate histogram using one for loop

You can exploit the fact that matlab stores the image
as a NxM continuous block of memory, and write something
like

II = I(:);

Now II is a N*M x 1 vector. But do note that there is
nothing to gain by this:

1) II has to be allocated in memoy
2) The data has to be transferred from I to II
3) II takes as much space as I

So this approach requires twice the memory footprint
and requires several times as much time the method you
already use. There is no reason why you would want to
do this as just one loop.

Rune

Subject: how to calculate histogram using one for loop

From: Bruno Luong

Date: 17 Oct, 2009 09:42:03

Message: 4 of 8

Rune Allnor <allnor@tele.ntnu.no> wrote in message <1a799733-f3c3-45ad-b711-43bcfde6a2c0@w19g2000yqk.googlegroups.com>...
>
> II = I(:);
>
> Now II is a N*M x 1 vector. But do note that there is
> nothing to gain by this:
>
> 1) II has to be allocated in memoy
> 2) The data has to be transferred from I to II
> 3) II takes as much space as I
>
> So this approach requires twice the memory footprint
> and requires several times as much time the method you
> already use. There is no reason why you would want to
> do this as just one loop.

Nope, statement, "II = I(:);" does not makes Matlab copying or allocating new memory. The underlying data are shared. Never heard about copy-on-write strategy Rune?

Bruno

Subject: how to calculate histogram using one for loop

From: Bruno Luong

Date: 17 Oct, 2009 09:48:00

Message: 5 of 8

To OP, straightforward call

h=histc(I(:),(0:255))

is enough

Bruno

Subject: how to calculate histogram using one for loop

From: Nasser M. Abbasi

Date: 17 Oct, 2009 09:51:57

Message: 6 of 8


"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message
news:hbc3sg$11e$1@fred.mathworks.com...
> To OP, straightforward call
>
> h=histc(I(:),(0:255))
>
> is enough
>
> Bruno

I thought it was a homework assignment where the teacher said that they
can't use Matlab hist commands to find the histogram.

--Nasser

Subject: how to calculate histogram using one for loop

From: Jamal Mohamed

Date: 17 Oct, 2009 09:58:00

Message: 7 of 8

"Nasser M. Abbasi" <nma@12000.org> wrote in message <I9gCm.10292$Ku5.5943@newsfe04.iad>...
>
> "Jamal Mohamed" <in.jamal@yahoo.com> wrote in message
> news:hbc1ce$1u$1@fred.mathworks.com...
> > hi everybody
> >
> > already i calculated histogram for a image like following code [ only
> > calculation part]
> > [row, col] =size(I);
> > h = zeros(256, 1);
> > for i=1:row
> > for j=1:col
> > Z=I(i,j); % I is image
> > h(Z+1)=h(Z+1)+1;
> > end
> > end
> >
> > my question is how to calculate histogram using one for loop
> > that is each row by row or column by column calculate histogram first
> > [vector based] and at end sum up all for histogram of image. is it
> > possible ? if yes tell me solution and code
>
>
> Why not flatten the image into a 1-D vector using reshape and then you will
> only use one loop?
>
> something like
>
> image=[0 90 255; 2 4 5; 255 10 200];
> [row,col]=size(image)
> data=reshape(image,row*col,1)
> h=zeros(256,1);
>
> for i=1:length(data)
> z=data(i);
> h(z+1)=h(z+1)+1;
> end
>
>
> --Nasser
>
thank u very much nasser

Subject: how to calculate histogram using one for loop

From: Rune Allnor

Date: 17 Oct, 2009 11:11:24

Message: 8 of 8

On 17 Okt, 11:42, "Bruno Luong" <b.lu...@fogale.findmycountry> wrote:
> Rune Allnor <all...@tele.ntnu.no> wrote in message <1a799733-f3c3-45ad-b711-43bcfde6a...@w19g2000yqk.googlegroups.com>...
>
> > II = I(:);
>
> > Now II is a N*M x 1 vector. But do note that there is
> > nothing to gain by this:
>
> > 1) II has to be allocated in memoy
> > 2) The data has to be transferred from I to II
> > 3) II takes as much space as I
>
> > So this approach requires twice the memory footprint
> > and requires several times as much time the method you
> > already use. There is no reason why you would want to
> > do this as just one loop.
>
> Nope, statement, "II = I(:);" does not makes Matlab copying or allocating new memory. The underlying data are shared. Never heard about copy-on-write strategy Rune?

It doesn't matter what *I* have heard about. I just think it's
sad when somebody - and I think this is a first on CSSM - has
already done everything right and then comes to ask questions
about how to mess the already ideal solution up.

Rune

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
image processing Jamal Mohamed 17 Oct, 2009 05:09:07
rssFeed for this Thread

Contact us at files@mathworks.com