我如何读取图像像素“值作为RGB到二维数组? [英] How can I read image pixels' values as RGB into 2d array?

查看:2257
本文介绍了我如何读取图像像素“值作为RGB到二维数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的正方形瓷砖成熟的游戏2D地图编辑器,当我意识到我真的可以使用图像编辑器,其能力重绘相邻的像素更多的人,所以我想我应该尝试阅读一画水平一个应用程序,然后将其转换成lightweigh格式。

我不知道,如果使用的是位图格式是强制性的这样的事情,但我想,读一本特定的像素会比同巴例如更容易。

所以我的目标是要打开图像,遍历每个像素,寻找那些颜色适合我的瓷砖计划,并把相应的瓷砖成块的数组。

注:我已经有我的lightweigh格式,所以我只需要读出象素值到数组



解决方案:我的草图看起来是这样的:

 位图MYBITMAP =新位图(@input.png);
            为(中间体X = 0; X&下; myBitmap.Width; X ++)
            {
                对于(INT Y = 0; Y< myBitmap.Height; Y ++)
                {
                    色pixelColor = myBitmap.GetPixel(X,Y);
                    //事情我们有pixelColor做
                }
            }



实施例2:

 位图MYBITMAP =新位图(@input.png);            为(中间体X = 0; X&下; myBitmap.Width; X ++)
            {
                对于(INT Y = 0; Y< myBitmap.Height; Y ++)
                {
                    //获取内MYBITMAP一个像素的颜色。
                    色pixelColor = myBitmap.GetPixel(X,Y);
                    串pixelColorStringValue =
                        pixelColor.R.ToString(D3)++
                        pixelColor.G.ToString(D3)++
                        pixelColor.B.ToString(D3)+,;                    开关(pixelColorStringValue)
                    {
                        案255 255 255:
                            {
                                //白色像素
                                打破;
                            }
                        案000 000 000:
                            {
                                //黑色像素
                                打破;
                            }
                    }
                }
            }


解决方案

好吧,如果我理解正确的话,您想通过图像中的像素进行迭代,执行某种测试,如果通过你要存储像素阵列。 Here's你怎么能做到这一点:

 使用System.Drawing中;位图IMG =新位图(* *的ImagePath);
的for(int i = 0; I< img.Width;我++)
{
    对于(INT J = 0; J< img.Height; J ++)
    {
        色像素= img.GetPixel(I,J);        如果(像素== * somecondition *)
        {
            **商店像素这里数组或列表或任何在**
        }
    }
}

鸵鸟政策认为你需要什么。如果您需要的特定RGB值,你可以从像素对象的相应方法得到他们。

I was making a 2d map editor for my square tile platformer game, when I realized I could really use an image editor with its abilities to repaint adjacent pixels and many more, so I figured I should try and read a painted level by an app that will then convert it into a lightweigh format.

I'm not sure if using a bitmap format is mandatory for such thing, but I guess, reading a specific pixel would be easier than with PNG for example.

So my goal is to open an image, iterate through every pixel, look for those which colors fit my tile scheme and put corresponding tile into the array of blocks.

Note: I already have my lightweigh format, so I need only reading pixels values into array.


Solution: My sketch looks like this:

Bitmap myBitmap = new Bitmap(@"input.png");            
            for (int x = 0; x < myBitmap.Width; x++)
            {
                for (int y = 0; y < myBitmap.Height; y++)
                {                    
                    Color pixelColor = myBitmap.GetPixel(x, y);
                    // things we do with pixelColor
                }
            }


Example 2:

Bitmap myBitmap = new Bitmap(@"input.png");

            for (int x = 0; x < myBitmap.Width; x++)
            {
                for (int y = 0; y < myBitmap.Height; y++)
                {
                    // Get the color of a pixel within myBitmap.
                    Color pixelColor = myBitmap.GetPixel(x, y);
                    string pixelColorStringValue =
                        pixelColor.R.ToString("D3") + " " +
                        pixelColor.G.ToString("D3") + " " +
                        pixelColor.B.ToString("D3") + ", ";

                    switch (pixelColorStringValue)
                    {
                        case "255 255 255":
                            {
                                // white pixel
                                break;
                            }
                        case "000 000 000":
                            {
                                // black pixel
                                break;
                            }
                    }
                }
            }

解决方案

Well, if I understood correctly, you want to iterate through the pixels in the image, perform some kind of test, and if it passes you want to store that pixel in an array. Here´s how you could do that:

using System.Drawing;

Bitmap img = new Bitmap("*imagePath*");
for (int i = 0; i < img.Width; i++)
{
    for (int j = 0; j < img.Height; j++)
    {
        Color pixel = img.GetPixel(i,j);

        if (pixel == *somecondition*)
        {
            **Store pixel here in a array or list or whatever** 
        }
    }
} 

Don´t think you need anything else. If you need the specific RGB values you can get them from the corresponding methods in the pixel object.

这篇关于我如何读取图像像素“值作为RGB到二维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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