private void openPan1_Click(object sender, EventArgs e)
        {
            Gdal.AllRegister();
            OpenFileDialog ofd = new OpenFileDialog();

            //允许打开的文件格式
            ofd.Filter =
                "Erdas Imagine (*.img)|*.img|" +
                "GeoTiff (*.tif *.tiff)|*.tif;*.tiff|" +
                "HDF (*.hdf *.h5 *he5)|*.hdf;*.h5;*he5|" +
                "位图文件 (*.bmp)|*.bmp|" +
                "Graphics Interchange Format (*.gif)|*.gif|" +
                "JPEG (*.jpg *.jpeg)|*.jpg;*.jpeg|" +
                "Portable Network Graphics (*.png)|*.png|" +
                "所有文件|*.*";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                file2 = ofd.FileName;
                ds2   = Gdal.Open(file2, Access.GA_ReadOnly);
            }
            Rectangle pictureRect = new Rectangle();

            pictureRect.X      = 0;
            pictureRect.Y      = 0;
            pictureRect.Width  = 600;
            pictureRect.Height = 450;
            Foudation fd   = new Foudation();
            Band      band = ds2.GetRasterBand(1);

            img2 = fd.GetImage(ds2, pictureRect, band);
        }
        private void openMsi1_Click(object sender, EventArgs e)
        {
            Gdal.AllRegister();
            OpenFileDialog ofd = new OpenFileDialog();

            //允许打开的文件格式
            ofd.Filter =
                "Erdas Imagine (*.img)|*.img|" +
                "GeoTiff (*.tif *.tiff)|*.tif;*.tiff|" +
                "HDF (*.hdf *.h5 *he5)|*.hdf;*.h5;*he5|" +
                "位图文件 (*.bmp)|*.bmp|" +
                "Graphics Interchange Format (*.gif)|*.gif|" +
                "JPEG (*.jpg *.jpeg)|*.jpg;*.jpeg|" +
                "Portable Network Graphics (*.png)|*.png|" +
                "所有文件|*.*";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                file1 = ofd.FileName;
                ds1   = Gdal.Open(file1, Access.GA_ReadOnly);
                if (ds1.RasterCount < 3)
                {
                    MessageBox.Show("不是多波段影像!\n请重新选择", "Error");
                    file1 = null;
                }
            }
            Rectangle pictureRect = new Rectangle();

            pictureRect.X      = 0;
            pictureRect.Y      = 0;
            pictureRect.Width  = 600;
            pictureRect.Height = 450;
            Foudation fd = new Foudation();

            int[] bandList = new int[3];
            bandList[0] = 3;
            bandList[1] = 2;
            bandList[2] = 1;
            img1        = fd.GetImage(ds1, pictureRect, 3, bandList);
        }
        //显示图片
        private void ShowImage_Click(object sender, EventArgs e)
        {
            TreeNode imageNode = fileTree.SelectedNode;
            string   filePath  = imageNode.Tag.ToString();

            Gdal.AllRegister();
            ds = Gdal.Open(filePath, Access.GA_ReadOnly);
            //获取波段数
            int bandCount = ds.RasterCount;


            Rectangle pictureRect = new Rectangle();

            pictureRect.X      = 0;
            pictureRect.Y      = 0;
            pictureRect.Width  = 600;
            pictureRect.Height = 450;
            Foudation fd = new Foudation();

            if (bandCount == 3)
            {
                BandSelection selection = new BandSelection();
                selection.ShowDialog();
                int[] bandList = new int[3];
                bandList[0] = selection.Band1;
                bandList[1] = selection.Band2;
                bandList[2] = selection.Band3;
                _bitmap     = fd.GetImage(ds, pictureRect, bandCount, bandList);
            }
            else
            {
                Band band = ds.GetRasterBand(1);
                // _bitmap = null;
                _bitmap = fd.GetImage(ds, pictureRect, band);
            }
            pictureBox1.Image = _bitmap;
            _pictureChoose    = true;
        }
        public static void IHS(Dataset msi, Dataset pan, string filePath)
        {
            // 全色影像单波段数据
            int xSize = pan.RasterXSize;
            int ySize = pan.RasterYSize;

            int[] pan_r = GetDNs(pan, xSize, ySize)[0];

            // 多光谱影像多波段数据
            List <int[]> msi_all = GetDNs(msi, xSize, ySize);

            int[] msi_r = msi_all[0];
            int[] msi_g = msi_all[1];
            int[] msi_b = msi_all[2];

            // RGB空间变换到IHS空间
            List <double[]> ihs = RecRGBToIHS(new List <int[]>()
            {
                msi_r, msi_g, msi_b
            });

            Foudation fd = new Foudation();

            // 对全色影像和IHS空间中的亮度分量I进行直方图匹配
            int[] matched = fd.HistoMatch(pan_r, ToInt(ihs[0]));

            // 用全色影像I代替IHS空间的亮度分量
            ihs[0] = ToDouble(matched);

            // 将IHS逆变换到RGB空间
            List <int[]> band = RecIHSToRGB(ihs);

            if (isSave == true)
            {
                fd.SaveF(pan, filePath, band, xSize, ySize);
            }
        }
Esempio n. 5
0
        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            #region 打开待配准影像
            OpenFileDialog op1 = new OpenFileDialog();
            op1.Filter = "所有文件|*.*";
            op1.Title  = "打开待配准影像";
            string op1name = op1.FileName;
            if (op1.ShowDialog() == DialogResult.OK)
            {
                //初始化设置
                OSGeo.GDAL.Gdal.AllRegister();                                   // 初始化gdal库
                OSGeo.GDAL.Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES"); //设置为UTF-8编码

                //打开影像
                Dataset ds1 = OSGeo.GDAL.Gdal.Open(op1.FileName, OSGeo.GDAL.Access.GA_ReadOnly);
                ds1.GetGeoTransform(GeoTransform1);
                Rectangle pictureRect = new Rectangle();
                pictureRect.X      = 0;
                pictureRect.Y      = 0;
                pictureRect.Width  = this.pictureBox1.Width;
                pictureRect.Height = this.pictureBox1.Height;

                int[] disband = new int[3] {
                    1, 2, 3
                };
                Band      band    = ds1.GetRasterBand(1);
                Foudation fd      = new Foudation();
                Bitmap    bitmap1 = fd.GetImage(ds1, pictureRect, band); //遥感影像构建位图
                pictureBox1.Image = bitmap1;
            }
            #endregion

            #region 打开标准影像
            OpenFileDialog op2 = new OpenFileDialog();
            op2.Filter = "所有文件|*.*";
            op2.Title  = "打开标准影像";
            string op2name = op2.FileName;
            if (op2.ShowDialog() == DialogResult.OK)
            {
                //初始化设置
                OSGeo.GDAL.Gdal.AllRegister();                                   // 初始化gdal库
                OSGeo.GDAL.Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES"); //设置为UTF-8编码

                //打开影像
                Dataset ds2 = OSGeo.GDAL.Gdal.Open(op2.FileName, OSGeo.GDAL.Access.GA_ReadOnly);
                ds2.GetGeoTransform(GeoTransform2);
                Rectangle pictureRect = new Rectangle();
                pictureRect.X      = 0;
                pictureRect.Y      = 0;
                pictureRect.Width  = this.pictureBox2.Width;
                pictureRect.Height = this.pictureBox2.Height;

                int[] disband = new int[3] {
                    1, 2, 3
                };
                Foudation fd      = new Foudation();
                Bitmap    bitmap2 = fd.GetImage(ds2, pictureRect, 3, disband);; //遥感影像构建位图
                pictureBox2.Image = bitmap2;
            }
            #endregion
        }
        private void 无人机影像粗校正ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (pictureBox1.Image != null)
            {
                //ds = Gdal.Open(_filePath, Access.GA_ReadOnly);
                Bitmap          bitmap = (Bitmap)pictureBox1.Image;
                double          pi     = Math.PI;
                double[]        can    = new double[11];
                double          w      = ds.RasterXSize;
                double          h      = ds.RasterYSize;
                ImageCorrection iC     = new ImageCorrection();
                iC.ShowDialog();
                can = iC.returnCan();

                if (can[0] == 0)
                {
                    return;
                }

                //校正参数
                double    B = can[1], L = can[2], Zs = can[3], fai = can[4] / 180 * pi, w0 = can[5] / 180 * pi;
                double    k = can[6] / 180 * pi, Iw = can[7] * 0.001, Ih = can[8] * 0.001, f = can[9] * 0.001, u = can[10];
                double    Xs = 0, Ys = 0;
                Foudation fd = new Foudation();
                double[]  xy = fd.GeoTransform(B, L);
                Xs = xy[0];
                Ys = xy[1];

                //旋转矩阵参数
                double a1 = Math.Cos(fai) * Math.Cos(k) - Math.Sin(fai) * Math.Sin(w0) * Math.Sin(k);
                double a2 = -Math.Cos(fai) * Math.Sin(k) - Math.Sin(fai) * Math.Sin(w0) * Math.Cos(k);
                double a3 = -Math.Sin(fai) * Math.Cos(w0);
                double b1 = Math.Cos(w0) * Math.Sin(k);
                double b2 = Math.Cos(w0) * Math.Cos(k);
                double b3 = -Math.Sin(w0);
                double c1 = Math.Sin(fai) * Math.Cos(k) + Math.Cos(fai) * Math.Sin(w0) * Math.Sin(k);
                double c2 = -Math.Sin(fai) * Math.Sin(k) + Math.Cos(fai) * Math.Sin(w0) * Math.Cos(k);
                double c3 = Math.Cos(fai) * Math.Cos(k);

                //角点图像坐标
                double[,] Ixy = new double[5, 3];
                Ixy[1, 1]     = 0; Ixy[1, 2] = 0;
                Ixy[2, 1]     = Iw; Ixy[2, 2] = Ih;
                Ixy[3, 1]     = 0; Ixy[3, 2] = Ih;
                Ixy[4, 1]     = Iw; Ixy[4, 2] = 0;

                //图像像元大小
                double pixelSize_x = Iw / w;
                double pixelSize_y = Ih / h;

                //角点地面坐标
                double[,] XY = new double[5, 3];

                double Zp = 750;//平均高程待定
                for (int i = 1; i < 5; i++)
                {
                    XY[i, 1] = Xs + (Zp - Zs) * (a1 * Ixy[i, 1] + a2 * Ixy[i, 2] - a3 * f)
                               / (c1 * Ixy[i, 1] + c2 * Ixy[i, 2] - c3 * f);
                    XY[i, 2] = Ys + (Zp - Zs) * (b1 * Ixy[i, 1] + b2 * Ixy[i, 2] - b3 * f)
                               / (c1 * Ixy[i, 1] + c2 * Ixy[i, 2] - c3 * f);
                }

                //输出影像最小范围
                double Xmin   = (XY[1, 1] < XY[3, 1] ? XY[1, 1] : XY[3, 1]);
                double Xmax   = (XY[2, 1] > XY[4, 1] ? XY[2, 1] : XY[4, 1]);
                double Ymin   = (XY[3, 2] < XY[4, 2] ? XY[3, 2] : XY[4, 2]);
                double Ymax   = (XY[1, 2] > XY[2, 2] ? XY[1, 2] : XY[2, 2]);
                int    outwid = (int)((Xmax - Xmin) / u) + 10;
                int    outhei = (int)((Ymax - Ymin) / u) + 10;

                //构建位图
                double  Xp, Yp;
                double  x, y;
                Driver  dryMemory = Gdal.GetDriverByName("MEM");
                Dataset dsMemory  = dryMemory.Create("", outwid, outhei, ds.RasterCount, DataType.GDT_CInt32, null);
                //dsMemory.SetProjection(ds.GetGCPProjection());
                double[] GeoTrans = new double[6];
                GeoTrans[0] = Xmin; GeoTrans[3] = Ymin;
                GeoTrans[1] = u; GeoTrans[5] = -u;
                GeoTrans[2] = 0; GeoTrans[4] = 0;
                dsMemory.SetGeoTransform(GeoTrans);
                dsMemory.SetProjection(" PROGCS[''G-K'']");

                for (int i = 0; i < outhei; i++)
                {
                    int[][] R = new int[ds.RasterCount][];
                    for (int bandindex = 0; bandindex < ds.RasterCount; bandindex++)
                    {
                        R[bandindex] = new int[outwid];
                    }

                    for (int j = 0; j < outwid; j++)
                    {
                        //地面坐标
                        Xp = j * u + Xmin;
                        Yp = (i) * u + Ymin;
                        //解像点坐标
                        x = -f * (a1 * (Xp - Xs) + b1 * (Yp - Ys) + c1 * (Zp - Zs)) / (a3 * (Xp - Xs) + b3 * (Yp - Ys) + c3 * (Zp - Zs));
                        y = -f * (a2 * (Xp - Xs) + b2 * (Yp - Ys) + c2 * (Zp - Zs)) / (a3 * (Xp - Xs) + b3 * (Yp - Ys) + c3 * (Zp - Zs));
                        //对应原来图像的行列号
                        int i0 = (int)(x / pixelSize_x);
                        int j0 = (int)(y / pixelSize_y);
                        if (i0 >= 0 && i0 < ds.RasterXSize && j0 >= 0 && j0 < ds.RasterYSize)
                        {
                            for (int bandindex = 0; bandindex < ds.RasterCount; bandindex++)
                            {
                                int[] r_read = new int[1];
                                Band  band   = ds.GetRasterBand(bandindex + 1);
                                band.ReadRaster(i0, j0, 1, 1, r_read, 1, 1, 0, 0);
                                R[bandindex][j] = r_read[0];
                            }
                        }
                        else
                        {
                            for (int bandindex = 0; bandindex < ds.RasterCount; bandindex++)
                            {
                                R[bandindex][j] = 0;
                            }
                        }
                    }
                    for (int bandindex = 0; bandindex < ds.RasterCount; bandindex++)
                    {
                        Band memBand1 = dsMemory.GetRasterBand(bandindex + 1);
                        memBand1.WriteRaster(0, i, R[bandindex].Length, 1, R[bandindex], R[bandindex].Length, 1, 0, 0);
                    }
                }
                Driver drvJPG = Gdal.GetDriverByName("GTiff");

                string dstFileName = @"E:\Correction.Tif";;
                //Dataset dstDs = drvJPG.Create(dstFileName, srcWidth, srcHeight, bandCount, DataType.GDT_Byte, null);
                drvJPG.CreateCopy(dstFileName, dsMemory, 1, null, null, null);
                ds = dsMemory;
                Rectangle pictureRect = new Rectangle();
                pictureRect.X      = 0;
                pictureRect.Y      = 0;
                pictureRect.Width  = this.pictureBox1.Width;
                pictureRect.Height = this.pictureBox1.Height;
                int[] disband = new int[3] {
                    1, 2, 3
                };
                Bitmap bitmap2 = fd.GetImage(this.ds, pictureRect, 3, disband);   //遥感影像构建位图
                pictureBox1.Image = bitmap2;
                MessageBox.Show("计算完成!");
            }
            else
            {
                MessageBox.Show("没有图像!", "Error!");
            }
        }