类似于以下使用Emgu OpenCV for .NET的工作。
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using System;
using System.Drawing;
using System.IO;
using (Image<Bgr, Byte> img = new Image<Bgr, Byte>("MyImage.jpg"))
{
Matrix<Int32> matrix = new Matrix<Int32>(img.Width, img.Height);
for (int i = 0; i<img.Height;i++)
{
for (int j = 0; j<img.Width;j++)
{
if (img.Data[i,j,2] == 255 &&
img.Data[i,j,1] == 255 &&
img.Data[i,j,0] == 255)
{
matrix.Data[i,j] = 0;
}
else
{
matrix.Data[i,j] = 1;
}
}
}
TextWriter tw = new StreamWriter("output.txt");
for (int i = 0; i<img.Height;i++)
{
for (int j = 0; j<img.Width;j++)
{
tw.Write(matrix.Data[i,j]);
}
tw.Write(tw.NewLine);
}
}
请注意,上面的代码片加载彩色图像,并创建一个矩阵,其中白色为0,否则为1。
为了加载并用灰度影像 的Image<Bgr, Byte>
成为Image<Gray, Byte>
和比较简化为只 if (img.Data[i,j,0] == 255)
。
也是这样做的阈值(从颜色转换为灰度为黑色和白色),您可以用大津的使用cvThreshold
方法阈值,使用类似:
int threshold = 150;
Image<Bgr, Byte> img = new Image<Bgr, Byte>("MyImage.jpg");
Image<Gray, Single> img2 = img1.Convert<Gray, Single>();
Image<Gray, Single> img3 = new Image<Gray, Single>(img2.Width, img2.Height);
CvInvoke.cvThreshold(img2, img3, threshold, 255, THRESH.CV_THRESH_OTSU);
其他可能的工具包括
convert
来自ImageMagick,来自netpbm的pnmoraw
,正如您链接的URL中所提及的那样,示例代码片段convert lib/dragon_face.xbm pbm: | pnmnoraw
。
- 使用PIL(Python的图像库)通过图像数据和Python的IO函数来迭代将输出写入数据
- 使用System.Drawing.Bitmap特别与getPixel方法通过图像数据进行迭代,和C#IO函数写入的输出数据。
我认为你需要更清楚地解释一下你的需求,或许有一个输入和输出的例子。你想要多少灰度值?你的原始图像是黑白还是彩色?之类的东西。否则我认为你的奖金可能会被浪费。 – 2012-03-25 11:18:35
好的,我会更新问题 – Alaa 2012-03-25 15:33:43