中易网

c# 图像波段 获取

答案:2  悬赏:60  
解决时间 2021-03-06 21:04
c#中对图像进行读取时怎样获取图像的波段数,以至于在对像素值进行运算时能够进行band,width和height的三重循环,c#中是使用哪个参数获取,另外在处理后怎样将其在picturebox里面显示。

你这是二维的,我是要把遥感图像的波段这一维也要考虑到,二维的我已经有了
最佳答案
给个实例你,是底片效果的,我之前做了个小程序,想要的话,可以私聊。
private void button1_Click(object sender, EventArgs e)
{
//以底片效果显示图像
int Height = this.pictureBox1.Image.Height;
int Width = this.pictureBox1.Image.Width;
Bitmap newbitmap = new Bitmap(Width, Height);
Bitmap oldbitmap = (Bitmap)this.pictureBox1.Image;
Color pixel;
for (int x = 1; x < Width; x++)
{
for (int y = 1; y < Height; y++)
{
int r, g, b;
pixel = oldbitmap.GetPixel(x, y);
r = 255 - pixel.R;
g = 255 - pixel.G;
b = 255 - pixel.B;
newbitmap.SetPixel(x, y, Color.FromArgb(r, g, b));
}
}
this.pictureBox1.Image = newbitmap;
}

//不好意思,这个我不会. 好象挺难的,要找算法获取图象的波段.
全部回答
由于c#托管代码中不能使用指针(后来经过我的验证,即使是在unsafe代码中用指针操作,速度仍然很慢),因此我们就不能像c++里面一样直接利用指针在内存中完成读写操作。好在.net framework提供了托管内存与非托管内存之间的读写功能,一个强大的marshal类,使得我们快速读写图像的像素信息成为可能。 在.net framework 2.0中,bitmap类多了两个有关内存操作的新方法:lockbits与unlockbits,其具体含义请参见msdn,我在这里就直接给出利用它们以及marshal类进行内存读写的代码。上面的getrgb方法经过改进之后: public static bool getrgb(bitmap source, out int[,] r, out int[,] g, out int[,] b) { try { int iwidth = source.width; int iheight = source.height; rectangle rect = new rectangle(0, 0, iwidth, iheight); system.drawing.imaging.bitmapdata bmpdata = source.lockbits(rect, system.drawing.imaging.imagelockmode.readwrite, source.pixelformat); intptr iptr = bmpdata.scan0; int ibytes = iwidth * iheight * 3; byte[] pixelvalues = new byte[ibytes]; system.runtime.interopservices.marshal.copy(iptr, pixelvalues, 0, ibytes); source.unlockbits(bmpdata); // 注意这个地方图像的两维方向与数组两维的方向是转置的关系 r = new int[iheight, iwidth]; g = new int[iheight, iwidth]; b = new int[iheight, iwidth]; int ipoint = 0; for (int i = 0; i < iheight; i++) { for (int j = 0; j < iwidth; j++) { // 注意,windows 中三基色的排列顺序是 bgr 而不是 rgb! b[i, j] = convert.toint32(pixelvalues[ipoint++]); g[i, j] = convert.toint32(pixelvalues[ipoint++]); r[i, j] = convert.toint32(pixelvalues[ipoint++]); } } return true; } catch (exception) { r = null; g = null; b = null; return false; } } 提醒注意的是,这段代码仅仅适合于像素是24位rgb的图像(8位r、8位b和8位g,不包含alpha通道,format24bpprgb),如果要其它像素格式的(可以参见system.drawing.imaging.pixelformat的成员),可以此类推,加上一组case语句,成为完整功能的读图方法。 回写则与之相反,把需要写入的像素信息先写到byte数组中,再调用marshal.copy的第一个重载方法即可。下面给出我从灰度矩阵构建bitmap类的方法: /// /// 由灰度矩阵创建位图 /// /// 灰度矩阵(取值0~255) /// 矩阵对应的位图 public static bitmap fromgray(int[,] gray) { int iwidth = gray.getlength(1); int iheight = gray.getlength(0); bitmap result=new bitmap(iwidth,iheight,system.drawing.imaging.pixelformat.format24bpprgb); rectangle rect=new rectangle(0,0,iwidth,iheight); system.drawing.imaging.bitmapdata bmpdata = result.lockbits(rect, system.drawing.imaging.imagelockmode.readwrite, system.drawing.imaging.pixelformat.format24bpprgb); intptr iptr = bmpdata.scan0; int istride = bmpdata.stride; int ibytes = iwidth * iheight * 3; byte[] pixelvalues = new byte[ibytes]; int ipoint=0; for (int i=0;i
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯