Click here to Skip to main content
15,887,821 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hai guys..
I'm a beginner in C# language..
I have a project to classification fruits based on size..
This is my step :
1. Capture image from webcam.
2. Convert image to gray scale.
3. Convert image to binary image.
4. Counting white pixel to getting the object size.

But now, i get stuck on step 4..
I can't count white pixel from image box,
My project using C# and emguCV as a library,
Anybody can solve my problem? How to count white pixel from image box?
Thanks,,
(Sorry for my bad English :D)

[Edit - Keith Barrow] - Added formatted code from comment

This the code to convert image capture to binary image :

C#
Image<gray,> imgTarget = hueFilter.And(saturationFilter).And(valueFilter);
           imgTarget._Erode(1);
           imgTarget._Dilate(1);
           Image<gray,> bin = imgTarget.ThresholdBinary(new Gray(80), new Gray(255));
           imageBox3.Image = bin.Resize(320, 240, Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR, true);
           imgTarget.Dispose();
           channels[0].Dispose();
           channels[1].Dispose();
           channels[2].Dispose();


But i was try to count the white pixel value using this code :

C#
Image<gray,> binary = new Image<gray,>(320, 240);
imageBox3.Image = binary;

for (int i = 0; i < binary.Rows; i++)
{
    for (int j = 0; j < binary.Cols; j++)
    {

        binary.Data[i, j, 0] = 255;


        byte white = binary.Data[i, j, 0];




        lblWhite.Text = white + " white pixels";
    }


And when i run this project the "lblWhite getting value = 255" , but it did't count the white pixel..
Can you tell me what's the problem?
Posted
Updated 1-Nov-12 7:13am
v2
Comments
Richard C Bishop 1-Nov-12 12:16pm    
First of all, your English is 100x better than most non-English speakers on this site. Second, can you post the code you have so far so that others can see what you are trying to do?
zaenal arifin 1-Nov-12 12:47pm    
Thank you @richcb for your suggest..
This the code to convert image capture to binary image :

Image<gray, byte=""> imgTarget = hueFilter.And(saturationFilter).And(valueFilter);
imgTarget._Erode(1);
imgTarget._Dilate(1);
Image<gray, byte=""> bin = imgTarget.ThresholdBinary(new Gray(80), new Gray(255));
imageBox3.Image = bin.Resize(320, 240, Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR, true);
imgTarget.Dispose();
channels[0].Dispose();
channels[1].Dispose();
channels[2].Dispose();

But i was try to count the white pixel value using this code :

Image<gray, byte=""> binary = new Image<gray, byte="">(320, 240);
imageBox3.Image = binary;

for (int i = 0; i < binary.Rows; i++)
{
for (int j = 0; j < binary.Cols; j++)
{

binary.Data[i, j, 0] = 255;


byte white = binary.Data[i, j, 0];




lblWhite.Text = white + " white pixels";
}

And when i run this project the "lblWhite getting value = 255" , but it did't count the white pixel..
Can you tell me what's the problem?
Keith Barrow 1-Nov-12 13:16pm    
Hi, I've edited your question and put you code in it, with formatting. That way everyone looking at the question will see it and the formatting makes it more readable.
What I'd do in this situation is to edit the original question (as I have done) and reply to the comment saying I've done this.
Sergey Alexandrovich Kryukov 1-Nov-12 12:52pm    
First of all, why would be use PictureBox for this? It's pointless...
--SA

1 solution

Hi, first off the image dilate and erode methods use a 3x3 grid of pixels this will effect your results, but I assume this is deliberate.
I'd start off using a 3x3 white image to start with, and comment the erode, dilate, threshold and re-size operations. That way you have baseline to check the rest of your code.

ImageBox doesn't make much sense to me: you'd be better off just using binary.

You don't have comparison anywhere, I'd expect you to compare the current pixel in your loop with a white one, or compare the brightness value with 255.

[edit]
the contents of loop is wrong for counting the white pixels, you have two preoblems:
C#
binary.Data[i, j, 0] = 255; //This sets the pixel to white (I assume)

then
C#
byte white = binary.Data[i, j, 0];

Gets the value of the pixel, which you've just set to white (255).


I think you need something more like:
C#
int count = 0;
for (int i = 0; i < binary.Rows; i++)
{
    for (int j = 0; j < binary.Cols; j++)
    {
         if(binary.Data[i, j, 0] == 255)
             count++;
         lblWhite.Text = string.Format("{0} white pixels", count);
    }
}

One thing I've found is that looping the pixels in any reasonably sized image is slow, but you should only optimise one your basic algorithm is working.
 
Share this answer
 
v3
Comments
zaenal arifin 2-Nov-12 2:42am    
Hai Keith..
I'm using image box to showing image capture from webcam, am i wrong?or i have to use picture box to display image capture?
Can you give me the right code for looping white pixel in binary image?
Keith Barrow 2-Nov-12 5:22am    
Hi, I've updated my answer.
zaenal arifin 2-Nov-12 9:47am    
Hai Keith, it's work..
But i've little edited code with put "lblWhite.Text = string.Format("{0} white pixels", count); " after the two curly bracket..
Thank you so much Keith for helping me..
My problem is solved by your..
Keith Barrow 2-Nov-12 9:57am    
Glad to be of help!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900