Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET VS2010 WinForm
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?
 
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 15-Mar-13 2:09am
Edited 15-Mar-13 21:41pm
v4
Comments
Sergey Alexandrovich Kryukov at 16-Mar-13 2:46am
   
To start with, never use SetPixel/GetPixel, unless you need to process very few.
—SA
rajesh@1989 at 16-Mar-13 2:49am
   
Thank you Sergey for your response!!
 
Then How do I do without SetPixel/GetPixel?

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  

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

  Print Answers RSS
0 George Jonsson 215
1 Kornfeld Eliyahu Peter 169
2 OriginalGriff 120
3 PIEBALDconsult 110
4 Zoltán Zörgő 99
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 16 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100