C# 如何将图像像素的值作为 RGB 读取到二维数组中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10127871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I read image pixels' values as RGB into 2d array?
提问by user1306322
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.
我正在为我的方形平铺平台游戏制作 2d 地图编辑器,当我意识到我真的可以使用具有重新绘制相邻像素等功能的图像编辑器时,所以我想我应该尝试通过一个应用程序读取绘制的关卡然后将其转换为轻量级格式。
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.
我不确定是否必须使用位图格式,但我想,读取特定像素比使用 PNG 更容易。
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:解决方案:我的草图如下所示:
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
}
}
示例 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;
}
}
}
}
采纳答案by Itzack
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 值,您可以从像素对象中的相应方法中获取它们。
回答by Matthias
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)
在内部 for 循环中,您可以将某些内容放入数组 [i][j]。(如果这就是你要找的)
回答by Hossein
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;
}

