将2D整数数组转换为灰度图像 [英] Convert a 2D array of ints to a greyscale image

查看:108
本文介绍了将2D整数数组转换为灰度图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个int的2D数组,范围从0到255,每个数组代表一个灰色阴影。我需要将其转换为灰度图像。图像的宽度和高度分别是数组的列数和行数。

I have a 2D array of ints ranging from 0 to 255, each representing a shade of grey. I need to turn it into a greyscale image. The width and height of the image are the number of columns and rows of the array, respectivly.

我使用的是最新的Microsoft Visual C#2010 Express(我认为)截至2013年2月,.NET框架。

I am using Microsoft Visual C# 2010 Express with the latest (I think) .NET framework as of Feb, 2013.

许多其他人都遇到了这个问题,但没有一个发布的解决方案对我有用。它们似乎都调用了我的代码中不存在的方法。我想我可能会丢失using语句或某些内容。

Many other people have had this problem but none of the solutions posted have worked for me; they all seem to call methods that don't exist in my code. I think I might missing a using statement or something.

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

By the way I am very new to programming, so please explain everything as mush as possible.

预先感谢。

编辑:好的,这就是我所拥有的:

Okay, this is what I have:

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

所以这是我想做的事情:

So here's some pseudo code of what I'm trying to do:

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.

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

The other solutions that seemed to have helped other people use a class or object called bitmap, but I don't seem to have that class, nor do I know what library it is in.

推荐答案

以与从RGB字节中获取图像相同的方式创建图像,唯一不同的是灰度级RGB将具有相同的值以获得灰色:

Create it the same way you would an image from RGB bytes, the only difference for Grey scale is that R G B will be the same value to get greys:

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

这篇关于将2D整数数组转换为灰度图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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