How can I read image pixels' values as RGB int

2019-01-08 21:06发布

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

3条回答
该账号已被封号
2楼-- · 2019-01-08 21:26

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.

查看更多
虎瘦雄心在
3楼-- · 2019-01-08 21:26

I think I've done something similar once. Here's a code snippet of what I was doing:

public static void main(String[] args) {
        try {

            String path = "src/colors.jpg";
            BufferedImage image = ImageIO.read(new File(path));
            int w = image.getWidth();
            int h = image.getHeight();
            for (int y = 0; y < h; y++) {
                for (int x = 0; x < w; x++) {
                    Color c = new Color(image.getRGB(x, y));
                    int red = c.getRed();
                    int green = c.getGreen();
                    int blue = c.getBlue();
                    countColor(red, green, blue);
                    totalCount++;
                }
            }

            printColors();

        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }

In the inner for loop you can put something into an array[i][j]. (If that is what you're looking for)

查看更多
你好瞎i
4楼-- · 2019-01-08 21:27
 public Color[][] getBitmapColorMatrix(string filePath)
    {
        Bitmap bmp = new Bitmap(filePath);
        Color[][] matrix;
        int height = bmp.Height;
        int width = bmp.Width;
        if (height > width)
        {
            matrix = new Color[bmp.Width][];
            for (int i = 0; i <= bmp.Width - 1; i++)
            {
                matrix[i] = new Color[bmp.Height];
                for (int j = 0; j < bmp.Height - 1; j++)
                {
                    matrix[i][j] = bmp.GetPixel(i, j);
                }
            }
        }
        else
        {
            matrix = new Color[bmp.Height][];
            for (int i = 0; i <= bmp.Height - 1; i++)
            {
                matrix[i] = new Color[bmp.Width];
                for (int j = 0; j < bmp.Width - 1; j++)
                {
                    matrix[i][j] = bmp.GetPixel(i, j);
                }
            }
        }
        return matrix;
    }
查看更多
登录 后发表回答