Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# EMGU EMGUCV
Hey guys, I was wondering if anybody could help me out...
 
I'm taking only my second delve into c# and I've been trying to get my hed round this for a few days and would really appriciate the help
 
Basically I need to be able to read and draw individual pixels from an image. The overall goal would be to have a loop where say every 5 seconds:
 
->Select 300 random pixels from the image
-> check if each random pixel is black or white
-> assign value to that pixel if white
-> colour all chosen pixels in red
-> clear and start again...
 
I know there is a method to call a pixel from its x,y pos., but i'm useless at c# at the moment and I don't know how to impliment it (emgu cv libaries...I have them running)
If anybody could help me with the syntax to access individual pixels, or how to acess the Image<TColor,TDepth>.Data property (using emgu) i'd really appriciate it
 
cheers for looking
Posted 21-Mar-11 13:50pm
Edited 29-Sep-11 9:06am
C_Johnson38.9K
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Hi,
 
While this reply is late I thought best to include the answer in case any other fall across this question.
 
Lets assume you have read your image in as a colour image:
 
Image<bgr,Byte> My_Image = new Image<bgr,Byte>("filename_string");
 
To access each individual pixel of My_Image you call the .Data property but lets not forget that a Bgr image has 3 matrices representing Red, Green, Blue
 
//Red
byte Red_val = My_Image.Data[y,x,0];
//Green
byte Green_val = My_Image.Data[y,x,1];
//Blue
byte Blue_val = My_Image.Data[y,x,2];
 
Now to check if this pixel is white "Red_Val", "Green_val" and "Blue_Val" will be equal to 255.
 
Now to set a pixel to a colour EMGU has made the method slighter simpler then setting the red green and blue spectrum's individually you can simply use:
 
My_Image[i,j] = new Bgr(Color.Red);
 

Now if you are using greyscale images you obviously can't assign the colour red but its worth noting that for this image type there is only 1 matrix representing it's values therefore to access its data you would use:
 
//Greyscale
byte Gray_val = My_Image.Data[y,x,0];
 
So putting it all together you would be after something like this:
 
Timer timer = new Timer();
Image<bgr,byte> My_Image;
Image<bgr,byte> My_Image_todrawon;
Random xy_rand =  new Random();
 
public FormWithTimer()
{
    InitializeComponent();
 
    My_Image = new Image<bgr,byte>("filename_string");
    
    timer.Tick += new EventHandler(timer_Tick); // Everytime timer ticks, timer_Tick will be called
    timer.Interval = (3000);              // Timer will tick every 3 seconds
    timer.Enabled = true;                       // Enable the timer
    timer.Start();                              // Start the timer (Make sure your Images area assigned before you start this);

 
}
 
void timer_Tick(object sender, EventArgs e)
{
    //Clear all your positions by makeing a copy image to draw on else your change 
    //will be permanent until you read the image in again
    My_Image_todrawon = My_Image.Copy();
 
    for(int i = 0; i< 300; i++)
    {
        int ran_x_pos = xy_rand.Next(My_Image.Width);
        int ran_y_pos = xy_rand.Next(My_Image.Height); //limit there values to within image boundaries

        byte Red_val = My_Image.Data[ran_y_pos,ran_x_pos,0];
        //Green
        byte Green_val = My_Image.Data[ran_y_pos,ran_x_pos,1];
        //Blue
        byte Blue_val = My_Image.Data[ran_y_pos,ran_x_pos,2];
        
        if(Red_val == 255 && Green_val == 255 && Blue_val == 255) //See if White
        {
            //Mark the position within the image in red
            My_Image[i,j] = new Bgr(Color.Red);
            
            //Do any other operations such as record position assign values etc
        }
 
    }
}
 

This should help get you started in whatever you application may be.
 
Cheers
Chris
  Permalink  
v2
Comments
RaviRanjankr at 20-Dec-11 5:40am
   
5+
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

The fastest way in C# to deal with pixels it to use pointer to bitmap:
 
The code snippet is:
 
public int DoSomeStuffWithPixels(ref Bitmap b)
{
	BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
 
	System.IntPtr Scan0 = bmData.Scan0;
 
	int x = 4;
        int y = 70;
	int w=b.Size.Width;
 
	unsafe
	{
		uint* p = (uint*)(void*)Scan0;
 
		//write pixel with coordinates (4;70)
		//ff - opacity, 11 - red, 22 - green, 33 - blue
		p[y * w + x] = (uint)(0xff112233);
		
		//reading pixel data from point with coordinates (4;70)
		uint dataRead = (uint)(p[y * w + x]);
	}
 
	b.UnlockBits(bmData);
 
	return 0;
}
 
Good luck!
 
P.S. Suddenly I have seen that I have duplicated --SA post.) Anyhow, hope it will be useful code snippet.
 
BR,
Viktor Signaievskyi
Piligrim
  Permalink  
v2
Comments
Marcus Kramer at 19-Dec-11 16:38pm
   
Your answer may be a little late for the OP, but I like it and have made note of it in my personal archive for future reference. +5.
Viktor Signaievskyi at 19-Dec-11 16:53pm
   
Marcus, thank you!)
RaviRanjankr at 20-Dec-11 5:39am
   
Nice answer, My 5+
Viktor Signaievskyi at 20-Dec-11 17:09pm
   
Thank you, RaviRanjankr!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
 
This is the easiest way but slow:
System.Drawing.Bitmap oBitmap=new Bitmap("MyFile.bmp");
System.Drawing.Color color= oBitmap.GetPixel(Y,X);
oBitmap.SetPixel(X, Y, color);
 
The faster way might be using Windows API functions.
 
Hope this can help you.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

With System.Drawing, the faster way of accessing bits is System.Drawing.Bitmap.LockBits. See http://msdn.microsoft.com/en-us/library/5ey6h79d.aspx[^] for complete help and a code sample.
System.Drawing.Bitmap.GetPixel/SetPixel method are too slow and are note used ofter; good enough if you need to assess 1-3 pixels Smile | :) .
 
With WPF, use System.Windows.Media.Imaging.WriteableBitmap, see all other classes derived from System.Windows.Media.ImageSource.
 
—SA
  Permalink  
Comments
Member 7773377 at 21-Mar-11 20:37pm
   
hey...cheers guys, but i mu st be a numbskull...i can't seem to compile the msdn link above, it compiles but nofiles open, there is some suspicious text (���) in the code, otherwise i'm still stumped! pretty sure the path is correct n just pasted the code in...how could i go wrong?
SAKryukov at 21-Mar-11 23:39pm
   
What code are you talking about?
Please don't use textspeak, if your want to be understood (or even any attention at all), this is considered impolite. Please, full punctuation, capitalization and correct spelling...
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

Hey Guys,
Please check that the RGB values are not in the same order.
 
In the example:
 
//Red
byte Red_val = My_Image.Data[y,x,0];
//Green
byte Green_val = My_Image.Data[y,x,1];
//Blue
byte Blue_val = My_Image.Data[y,x,2];
 
In reality:
 
//Blue
byte Blue_val = My_Image.Data[y,x,0];
//Green
byte Green_val = My_Image.Data[y,x,1];
//Red
byte Red_val = My_Image.Data[y,x,2];
 
Regards, hope this helps
  Permalink  

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

  Print Answers RSS
0 DamithSL 421
1 OriginalGriff 220
2 Sergey Alexandrovich Kryukov 207
3 Peter Leow 185
4 Afzaal Ahmad Zeeshan 170
0 OriginalGriff 7,380
1 DamithSL 5,359
2 Sergey Alexandrovich Kryukov 4,944
3 Maciej Los 4,906
4 Kornfeld Eliyahu Peter 4,514


Advertise | Privacy | Mobile
Web01 | 2.8.141223.1 | Last Updated 11 May 2014
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