Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
2.75/5 (4 votes)
See more:
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
Updated 29-Sep-11 8:06am
v3

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:

C#
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

C#
//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:

C#
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:

C#
//Greyscale
byte Gray_val = My_Image.Data[y,x,0];


So putting it all together you would be after something like this:

C#
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
 
Share this answer
 
v2
Comments
RaviRanjanKr 20-Dec-11 5:40am    
5+
The fastest way in C# to deal with pixels it to use pointer to bitmap:

The code snippet is:

C#
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
 
Share this answer
 
v2
Comments
fjdiewornncalwe 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 19-Dec-11 16:53pm    
Marcus, thank you!)
RaviRanjanKr 20-Dec-11 5:39am    
Nice answer, My 5+
Viktor Signaievskyi 20-Dec-11 17:09pm    
Thank you, RaviRanjankr!
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.
 
Share this answer
 
v2
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 :-).

With WPF, use System.Windows.Media.Imaging.WriteableBitmap, see all other classes derived from System.Windows.Media.ImageSource.

—SA
 
Share this answer
 
Comments
Member 7773377 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?
Sergey Alexandrovich Kryukov 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
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
 
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