Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
I want to make a software for Foot pressure measurement and I am using a Device in which a camera is integrated and that show the foot impression.

Till now I am using Aforge.net dll for capture image from device and convert that image into grayscale and after that find out color on every pixel and set color according to dark gray is found in image.

But It is not work I have done following code for it and is it a right direction to process it?

C#
private void cmb_cameras_SelectedIndexChanged(object sender, EventArgs e)
{
    //selected video source
    FinalVideoSource = new VideoCaptureDevice(VideoCaptureDevices[cmb_cameras.SelectedIndex].MonikerString);
    FinalVideoSource.NewFrame += new NewFrameEventHandler(FinalVideoSource_NewFrame);
    FinalVideoSource.Start();
}
void FinalVideoSource_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
    try
    {
        //System.Console.WriteLine("1: Flag status: " + flag.ToString());
        if (flag!=true)
        {
            flag = true;
            //System.Console.WriteLine("2: Flag status: "+ flag.ToString());
            lock (NewBitmap)
            {
                using (ImageAttributes attributes = new ImageAttributes())
                {                            
                    LiveImage = (Bitmap)eventArgs.Frame.Clone();
                    Graphics g = Graphics.FromImage(NewBitmap);
                    //create the grayscale ColorMatrix
                    ColorMatrix colorMatrix = new ColorMatrix(
                     new float[][]
                    {
                         new float[] {.3f, .3f, .3f, 0, 0},
                         new float[] {.59f, .59f, .59f, 0, 0},
                         new float[] {.11f, .11f, .11f, 0, 0},
                         new float[] {0, 0, 0, 1, 0},
                         new float[] {0, 0, 0, 0, 1}
                    });
                    attributes.SetColorMatrix(colorMatrix);
                    //draw the original image on the new image
                    //using the grayscale color matrix
                    g.DrawImage(LiveImage, new Rectangle(0, 0, 640, 480),
                      0, 0, 640, 480, GraphicsUnit.Pixel, attributes);

     //--------------------------- Method 2ND ----------------------------------
                   // System.Diagnostics.Stopwatch sWatch = new System.Diagnostics.Stopwatch();
                   // sWatch.Start();
                   // PB_LiveImage.Image = LiveImage;
                    PB_LiveImage.Image = ChangeColor();
                    //sWatch.Stop();
                    //MessageBox.Show(sWatch.ElapsedMilliseconds.ToString());

                    
                }
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("Message:\n\nKindly closed the application.\nOne unhandled exception has been occurred!!");
    }  
}

public unsafe Bitmap ChangeColor()
{            
    Bitmap bmp = new Bitmap(NewBitmap);
    LockBitClass lockBitmap = new LockBitClass(bmp);
    lockBitmap.LockBits();
    Color compareClr = Color.FromArgb(75, 75, 75);
    Color compareClr2 = Color.FromArgb(90, 90, 90);
    Color compareClr3 = Color.FromArgb(105, 105, 105);
    Color compareClr4 = Color.FromArgb(150, 150, 150);
    for (int y = 0; y < lockBitmap.Height; y++)
    {
        for (int x = 0; x < lockBitmap.Width; x++)
        {

            //Console.WriteLine((System.Drawing.Color.LightGray.ToArgb() == lockBitmap.GetPixel(x, y).ToArgb()) + " LightGray");
            //Console.WriteLine((System.Drawing.Color.Gray.ToArgb() == lockBitmap.GetPixel(x, y).ToArgb()) + " Gray");
            //Console.WriteLine((System.Drawing.Color.DarkGray.ToArgb() == lockBitmap.GetPixel(x, y).ToArgb()) + " DarkGray");
            if (System.Drawing.Color.LightGray.ToArgb() == lockBitmap.GetPixel(x, y).ToArgb())                        
                lockBitmap.SetPixel(x, y, Color.Cyan);
            else if (System.Drawing.Color.Gray.ToArgb() == lockBitmap.GetPixel(x, y).ToArgb())
                lockBitmap.SetPixel(x, y, Color.Yellow);
            else if (System.Drawing.Color.DarkGray.ToArgb() == lockBitmap.GetPixel(x, y).ToArgb())
                lockBitmap.SetPixel(x, y, Color.Red);
            else
                lockBitmap.SetPixel(x, y, Color.Blue);
        }
    }
    lockBitmap.UnlockBits();
    
    return bmp;
}
Posted
Updated 15-Mar-13 20:41pm
v4
Comments
Sergey Alexandrovich Kryukov 16-Mar-13 2:46am    
To start with, never use SetPixel/GetPixel, unless you need to process very few.
—SA
rajesh@1989 16-Mar-13 2:49am    
Thank you Sergey for your response!!

Then How do I do without SetPixel/GetPixel?

1 solution

rajesh@1989 asked:
Then How do I do without SetPixel/GetPixel?
The whole idea is to call System.Drawing.Bitmap.LockBits and use System.Drawing.Imaging.BitmapData returned from this call to directly manipulate locked image memory. Look at the code sample to see how to do it:
http://msdn.microsoft.com/en-us/library/5ey6h79d.aspx[^].

In your code sample, your call to LockBits is totally pointless. You don't do anything to your bitmap. When you actually do it in the way shown in the code sample referenced above, it will solve all your problem.

As to GetPixel/SePixel, not only these calls totally defeat the purpose of LockBits, they also prohibitively slow and should only be used if you want to read or modify very few pixels, in a very simplified way.

—SA
 
Share this answer
 

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