anybod知道如何访问像素数据吗? [英] does anybod know how to access pixel data?

查看:107
本文介绍了anybod知道如何访问像素数据吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,我想知道是否有人可以帮助我...



我只是第二次钻研c#而且我一直在努力为了让我的hed围绕这几天,并真的会帮助帮助



基本上我需要能够从图像中读取和绘制单个像素。总体目标是每隔5秒就有一个循环:



- >从图像中选择300个随机像素

- >检查每个随机像素是黑色还是白色

- >如果白色为该像素赋值

- >所有选择的红色像素颜色

- >清楚并重新开始...



我知道有一种方法可以从x,y pos调用一个像素,但是我在c#处没用那一刻,我不知道如何恭维它(emgu cv libaries ...我让它们运行)

如果有人可以帮助我使用语法访问单个像素,或者如何访问Image< ; TColor,TDepth> .Data属性(使用emgu)我真的很喜欢它



欢呼寻找

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

推荐答案





虽然这个回复很晚,但我认为最好包括答案,以防其他任何问题出现在这个问题上。



假设您已将图像作为彩色图像读取:



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");





要访问My_Image的每个像素,请调用.Data属性但不要忘记Bgr图像有3个表示红色,绿色,蓝色的矩阵





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];





现在检查这个像素是否为白色Red_Val,Green_val和Blue_Val将等于255.



现在设置一个像素一个颜色EMGU使方法更简单,然后设置红色绿色和蓝色光谱单独,你可以简单地使用:





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);







现在,如果您使用的是灰度图像,您显然无法将颜色指定为红色,但值得注意的是,对于此图像类型,只有1个矩阵表示其值,因此要访问其使用的数据:






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
        }

    }
}







这个应该帮助你开始任何你的应用程序。



干杯

Chris




This should help get you started in whatever you application may be.

Cheers
Chris


在C#中处理像素的最快方法是使用指向位图的指针:



代码片段为:



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;
}





祝你好运!



P.S.突然间我看到我有重复--SA帖子。)无论如何,希望它将是有用的代码片段。



BR,

Viktor Signaievskyi

Piligrim



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






这是最简单的方法,但很慢:

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);





使用Windows API函数的方法可能更快。 />


希望这可以帮到你。



The faster way might be using Windows API functions.

Hope this can help you.


这篇关于anybod知道如何访问像素数据吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆