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:
Pixel of a picture in an object

Subject: Pixel of a picture in an object

From: Flavio Eripi

Date: 17 Aug, 2007 09:10:13

Message: 1 of 7

Hi,
is it possible to count the number of pixel in a picture?
Should I convert the picture to binary format first?

Subject: Pixel of a picture in an object

From: Dave Robinson

Date: 17 Aug, 2007 12:18:51

Message: 2 of 7

"Flavio Eripi" <didad30@libero.it> wrote in message
<fa3olk$gma$1@fred.mathworks.com>...
> Hi,
> is it possible to count the number of pixel in a picture?
> Should I convert the picture to binary format first?

Assuming you want to count all of the pixels in your image
then size will do it for you

MyImage = imread('c:\mypath\mypicture.jpg');

[rows,columns,planes] = size(MyImage);

Number_Pixels = rows*columns;

You can also do it by summing the values returned by imhist();

Hope that helps

Regards

Dave Robinson

Subject: Pixel of a picture in an object

From: Flavio Eripi

Date: 17 Aug, 2007 13:54:48

Message: 3 of 7

"Dave Robinson" <dave.robinson@somewhere.biz> wrote in
message <fa43nb$rh8$1@fred.mathworks.com>...
> "Flavio Eripi" <didad30@libero.it> wrote in message
> <fa3olk$gma$1@fred.mathworks.com>...
> > Hi,
> > is it possible to count the number of pixel in a picture?
> > Should I convert the picture to binary format first?
>
> Assuming you want to count all of the pixels in your image
> then size will do it for you
>
> MyImage = imread('c:\mypath\mypicture.jpg');
>
> [rows,columns,planes] = size(MyImage);
>
> Number_Pixels = rows*columns;
>
> You can also do it by summing the values returned by imhist();
>
> Hope that helps
>
> Regards
>
> Dave Robinson


Is this the counting of the pixel in the whole picture?
What I wanted was different: if I have for example a white
triangle in a black background, how can I count the pixel of
the triangle?
Thx
Is it possible only when dealing with binary picture? If I
have a red square inside the triangle of the above picture,
shall I count the pixel of the square (red pixel) of the
triangle (white) and of the background (black)?

Subject: Pixel of a picture in an object

From: Rob Chambers

Date: 17 Aug, 2007 16:43:47

Message: 4 of 7

"Flavio Eripi" <didad30@libero.it> wrote in message
<fa49b8$ivq$1@fred.mathworks.com>...
> "Dave Robinson" <dave.robinson@somewhere.biz> wrote in
> message <fa43nb$rh8$1@fred.mathworks.com>...
> > "Flavio Eripi" <didad30@libero.it> wrote in message
> > <fa3olk$gma$1@fred.mathworks.com>...
> > > Hi,
> > > is it possible to count the number of pixel in a picture?
> > > Should I convert the picture to binary format first?
> >
> > Assuming you want to count all of the pixels in your image
> > then size will do it for you
> >
> > MyImage = imread('c:\mypath\mypicture.jpg');
> >
> > [rows,columns,planes] = size(MyImage);
> >
> > Number_Pixels = rows*columns;
> >
> > You can also do it by summing the values returned by
imhist();
> >
> > Hope that helps
> >
> > Regards
> >
> > Dave Robinson
>
>
> Is this the counting of the pixel in the whole picture?
> What I wanted was different: if I have for example a white
> triangle in a black background, how can I count the pixel of
> the triangle?
> Thx
> Is it possible only when dealing with binary picture? If I
> have a red square inside the triangle of the above picture,
> shall I count the pixel of the square (red pixel) of the
> triangle (white) and of the background (black)?

Read in the picture:

>>MyImage = imread('c:\mypath\mypicture.jpg');

Apply some sort of logical test that is true only for the
pixels you care about... let's say the values of the
triangle are all 100:

>>isTriangle = ( myImage == 100 )

now, count the "true" pixels:

>>sum(sum(isTriangle))

that should do it.

-Rob Chambers
http://www.stanford.edu/~robc1

Subject: Pixel of a picture in an object

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 17 Aug, 2007 16:54:27

Message: 5 of 7

In article <fa49b8$ivq$1@fred.mathworks.com>,
Flavio Eripi <didad30@libero.it> wrote:

>Is this the counting of the pixel in the whole picture?
>What I wanted was different: if I have for example a white
>triangle in a black background, how can I count the pixel of
>the triangle?

If the image is RGB:

whitecount = sum( Image(:,:,1) ~= 0 & ...
     Image(:,:,1) == Image(:,:,2) & Image(:,:,1) == Image(:,:,3) );

The idea here is that white is defined as any pixel in which the
R, G, and B components are all the same, except for the case of [0 0 0]
which is black under any definition. [0.00001 0.00001 0.0001]
is a very dull white but still arguably white ;-)

If your white is defined as [v v v] for any v, such as [1 1 1]
or [255 255 255], then

whitecount = sum( Image(:,:,1) == v & Image(:,:,2) == v && ..
                  Image(:,:,3) == v )


If you want to include values that are "close to white", then

coltol = 0.95; %adjust according to how close it has to be
nearwhitecount = sum(sum(Image.^2,3) >= 3 * coltol * max(Image(:)).^2);

>Is it possible only when dealing with binary picture? If I
>have a red square inside the triangle of the above picture,
>shall I count the pixel of the square (red pixel) of the
>triangle (white) and of the background (black)?

That's up to you and your requirements.

coltol = 0.95; %adjust according to how close it has to be
imagmemax = max(Image(:));
nearredcount = sum( sum(Image.^2,3) - ...
                    2 * imagemax * (Image(:,:,2) + Image(:,:,3)) >= ...
                    (3 * coltol - 2) * imagemax );

For nearbluecount, (Image(:,:,2) + Image(:,:,3)) would become
(Image(:,:,1) + Image(:,:,3))

For neargreencount, (Image(:,:,2) + Image(:,:,3)) would become
(Image(:,:,1) + Image(:,:,2))
--
  Is there any thing whereof it may be said, See, this is new? It hath
  been already of old time, which was before us. -- Ecclesiastes

Subject: Pixel of a picture in an object

From: Leo

Date: 8 Jan, 2008 17:37:01

Message: 6 of 7

"Flavio Eripi" <didad30@libero.it> wrote in message
<fa3olk$gma$1@fred.mathworks.com>...
> Hi,
> is it possible to count the number of pixel in a picture?
> Should I convert the picture to binary format first?

how to use matlab's plot function to display a histogram of
an image's red data plane with 256 bins? don't use imhist
function. Make sure the histogram accurately displays the
number of pixels with zero intensity value.

Subject: Pixel of a picture in an object

From: Chris

Date: 19 Jan, 2011 17:34:05

Message: 7 of 7

Hi Walter,

This looks really interesting. I'd love to adapt this in order to count the black pixels (or near black), but I couldn't quite work out what the math was doing here - why larger or equal to 3? and why square the image? and finally, why are you reading the full image at the end when you use the image max?

Thanks!
Chris


>
> whitecount = sum( Image(:,:,1) ~= 0 & ...
> Image(:,:,1) == Image(:,:,2) & Image(:,:,1) == Image(:,:,3) );
>
> The idea here is that white is defined as any pixel in which the
> R, G, and B components are all the same, except for the case of [0 0 0]
> which is black under any definition. [0.00001 0.00001 0.0001]
> is a very dull white but still arguably white ;-)
>
> If your white is defined as [v v v] for any v, such as [1 1 1]
> or [255 255 255], then
>
> whitecount = sum( Image(:,:,1) == v & Image(:,:,2) == v && ..
> Image(:,:,3) == v )
>
>
> If you want to include values that are "close to white", then
>
> coltol = 0.95; %adjust according to how close it has to be
> nearwhitecount = sum(sum(Image.^2,3) >= 3 * coltol * max(Image(:)).^2);
>

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