将整数的二维数组转换为灰度图像

问题描述:

我有一个从0到255的整数的二维数组,每个数组表示一个灰色阴影。我需要将它变成灰度图像。图像的宽度和高度分别是数组的列数和行数。将整数的二维数组转换为灰度图像

我使用Microsoft Visual C#2010速成与作为二月的最新的(我认为).NET框架,2013年

很多人都有过这样的问题,但没有公布为我工作的解决方案;他们似乎都调用了我的代码中不存在的方法。我想我可能会错过使用陈述或其他东西。

顺便说一下,我对编程非常陌生,所以请尽可能地解释一切。

在此先感谢。

编辑:好的,这是我有:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.IO; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      int width; 
      int height; 
      int[,] pixels; 
      Random randomizer = new Random(); 

      Start: 
      Console.WriteLine("Width of image?"); 
      string inputWidth = Console.ReadLine(); 
      Console.WriteLine("Height of image?"); 
      string inputHeight = Console.ReadLine(); 

      try 
      { 
       width = Convert.ToInt32(inputWidth); 
       height = Convert.ToInt32(inputHeight); 
      } 
      catch (FormatException e) 
      { 
       Console.WriteLine("Not a number. Try again."); 
       goto Start; 
      } 
      catch (OverflowException e) 
      { 
       Console.WriteLine("Number is too big. Try again."); 
       goto Start; 
      } 

      pixels = new int[width, height]; 

      for (int i = 0; i < width; ++i) 
       for (int j = 0; j < height; ++j) 
       pixels[i, j] = randomizer.Next(256); 



      Console.ReadKey(); 
     } 
    } 
} 

所以这里有我想要做一些伪代码:这似乎已经帮助

Initialize some variables 

Prompt user for preferred width and height of the resulting image. 

Convert input into Int. 

Set up the array to be the right size. 

Temporary loop to fill the array with random values. (this will be replaced with a series of equations when I can figure out how to write to a PNG or BMP. 

//This is where I would then convert the array into an image file. 

Wait for further input. 

的其他解决方案其他人使用称为位图的类或对象,但我似乎没有该类,也不知道它在哪个库中。

+1

请发布您尝试过的内容。 – Aditi 2013-02-19 04:52:54

+0

随时表达您所做的一切。 – TNC 2013-02-19 05:22:12

+2

欢迎来到[so]。 分享你的研究可以帮助每个人。告诉我们你发现了什么,以及它为什么不符合你的需求。这表明你已经花时间去尝试帮助自己,它使我们避免重申明显的答案,最重要的是它可以帮助你得到更具体和相关的答案:)祝你好运! – 2013-02-19 05:22:20

创建它的方式与从RGB字节的图像相同迪菲对于灰度等级来说,R G B将是相同的灰度值:

int width = 255; // read from file 
int height = 255; // read from file 
var bitmap = new Bitmap(width, height, PixelFormat.Canonical); 

for (int y = 0; y < height; y++) 
    for (int x = 0; x < width; x++) 
    { 
     int red = 2DGreyScaleArray[x][y]; // read from array 
     int green = 2DGreyScaleArray[x][y]; // read from array 
     int blue = 2DGreyScaleArray[x][y]; // read from array 
     bitmap.SetPixel(x, y, Color.FromArgb(0, red, green, blue)); 
    } 
+0

对于红 - 绿 - 蓝(RGB)灰度图像中的每个像素,R = G = B。灰度的亮度与代表原色亮度级别的数字成正比。黑色表示为R = G = B = 0. – TNC 2013-02-19 05:37:51

+0

或者您可以将数组读入一个变量并将该变量传递给'FromArgb'三次。 – Corey 2013-02-19 06:17:55

+0

@Corey Optimiser应该处理这个问题。 – JacobD 2014-07-03 06:35:42