关于GDAL图像拼接的问题 20C

图片说明

private void mosicaer( OSGeo.GDAL.Dataset ds1, OSGeo.GDAL.Dataset ds2)
{
Gdal.AllRegister();

        OSGeo.GDAL.Band band11 = ds1.GetRasterBand(1);
        OSGeo.GDAL.Band band12 = ds1.GetRasterBand(2);
        OSGeo.GDAL.Band band13 = ds1.GetRasterBand(3);
        //打开图一
        int rasterX1 = ds1.RasterXSize; //影像宽度
        int rasterY1 = ds1.RasterYSize; //影像高度
        int bandCount1 = ds1.RasterCount; //波段数
        int[] dataArray1 = new int[rasterX1 * rasterY1 * bandCount1];
        int[] bandArray1 = new int[bandCount1];
        for (int i = 0; i < bandCount1; i++)
        {
            bandArray1[i] = i + 1;
        }
        double[] tmpD1 = new double[6];
        ds1.GetGeoTransform(tmpD1); //影像坐标变换参数
        string proj1 = ds1.GetProjection(); //影像坐标系信息(WKT格式字符串)
        double minX1 = tmpD1[0];
        double maxY1 = tmpD1[3];
        double pixelWidth1 = tmpD1[1];
        double pixelHeight1 = tmpD1[5];
        double maxX1 = minX1 + (rasterX1 * pixelWidth1);
        double minY1 = maxY1 + (rasterY1* pixelHeight1);

        band11.ReadRaster(0, 0, rasterX1, rasterY1, dataArray1, rasterX1, rasterY1, 0, 0);
        band12.ReadRaster(0, 0, rasterX1, rasterY1, dataArray1, rasterX1, rasterY1, 0, 0);
        band13.ReadRaster(0, 0, rasterX1, rasterY1, dataArray1, rasterX1, rasterY1, 0, 0);

        band11.SetNoDataValue(0);
        band12.SetNoDataValue(0);
        band13.SetNoDataValue(0);

        //打开图二

        OSGeo.GDAL.Band band21 = ds2.GetRasterBand(2);
        OSGeo.GDAL.Band band22 = ds1.GetRasterBand(2);
        OSGeo.GDAL.Band band23 = ds1.GetRasterBand(3);

        int rasterX2 = ds2.RasterXSize; //影像宽度
        int rasterY2 = ds2.RasterYSize; //影像高度
        int bandCount2 = ds2.RasterCount; //波段数
        int[] dataArray2 = new int[rasterX2 * rasterY2 * bandCount2];
        int[] bandArray2 = new int[bandCount2];
        for (int i = 0; i < bandCount2; i++)
        {
            bandArray2[i] = i + 1;
        }
        double[] tmpD2 = new double[6];
        ds2.GetGeoTransform(tmpD2); //影像坐标变换参数

        string proj2 = ds2.GetProjection(); //影像坐标系信息(WKT格式字符串)
        double minX2 = tmpD2[0];
        double maxY2 = tmpD2[3];//左上角点坐标
        double pixelWidth2 = tmpD2[1];//东西距离
        double pixelHeight2 = tmpD2[5];//南北距离
        double maxX2 = minX2 + (rasterX2 * pixelWidth2);
        double minY2 = maxY2 + (rasterY2 * pixelHeight2);

        band21.ReadRaster(0, 0, rasterX2, rasterY2, dataArray2, rasterX2, rasterY2, 0, 0);
        band22.ReadRaster(0, 0, rasterX2, rasterY2, dataArray2, rasterX2, rasterY2, 0, 0);
        band23.ReadRaster(0, 0, rasterX2, rasterY2, dataArray2, rasterX2, rasterY2, 0, 0);

        band21.SetNoDataValue(0);
        band22.SetNoDataValue(0);
        band23.SetNoDataValue(0);

        //获取输出图像坐标
        double minX = Math.Min(minX1, minX2);
        double maxX = Math.Max(maxX1, maxX2);
        double minY = Math.Min(minY1, minY2);
        double maxY = Math.Max(maxY1, maxY2);
      //获取输出图像的行与列
        int cols = (int)((maxX - minX) / pixelWidth1 + 0.5);
        int rows = (int)((maxY - minY)/Math.Abs(pixelHeight1) + 0.5);
    ////计算图1左上角的偏移值(在输出图像中)
        int xOffset1 = (int)((minX1 - minX) / pixelWidth1);
        int yOffset1 = (int)((maxY1 - maxY) / pixelHeight1);

        ////计算图2左上角的偏移值(在输出图像中)
        int xOffset2 = (int)((minX2 - minX) / pixelWidth1);
        int yOffset2 = (int)((maxY2 - maxY) / pixelHeight1);
        //创建一个输出图像
        //OSGeo.GDAL.Driver driver = ds1.GetDriver();
        string strMemory = @"E:\mosica.tif";
        OSGeo.GDAL.Driver driver = Gdal.GetDriverByName("GTiff");

                OSGeo.GDAL.Dataset dsOut = driver.Create(strMemory, cols, rows, bandCount1, band11.DataType, null);
        OSGeo.GDAL.Band bandOut1 = dsOut.GetRasterBand(1);
        OSGeo.GDAL.Band bandOut2 = dsOut.GetRasterBand(2);
        OSGeo.GDAL.Band bandOut3= dsOut.GetRasterBand(3);


        //读图1的数据并将其写到输出图像中
        ds1.ReadRaster(0, 0, rasterX1, rasterY1, dataArray1, rasterX1, rasterY1, bandCount1, bandArray1, 0, 0, 0);

        bandOut1.WriteRaster(xOffset1, yOffset1, rasterX1, rasterY1, dataArray1, rasterX1, rasterY1, 0, 0);
        bandOut2.WriteRaster(xOffset1, yOffset1, rasterX1, rasterY1, dataArray1, rasterX1, rasterY1, 0, 0);
        bandOut3.WriteRaster(xOffset1, yOffset1, rasterX1, rasterY1, dataArray1, rasterX1, rasterY1, 0, 0);
        dsOut.WriteRaster(xOffset1, yOffset1, rasterX1, rasterY1, dataArray1, rasterX1, rasterY1, bandCount1, bandArray1, 0, 0, 0);

        //读图2的数据并将其写到.输出图像中    

        ds2.ReadRaster(0, 0, rasterX2, rasterY2, dataArray2, rasterX2, rasterY2, bandCount2, bandArray2, 0, 0, 0);
        bandOut1.WriteRaster(xOffset2, yOffset2, rasterX2, rasterY2, dataArray2, rasterX2, rasterY2, 0, 0);
        bandOut2.WriteRaster(xOffset2, yOffset2, rasterX2, rasterY2, dataArray2, rasterX2, rasterY2, 0, 0);
        bandOut3.WriteRaster(xOffset2, yOffset2, rasterX2, rasterY2, dataArray2, rasterX2, rasterY2, 0, 0);
        dsOut.WriteRaster(xOffset2, yOffset2, rasterX2, rasterY2, dataArray2, rasterX2, rasterY2, bandCount2, bandArray2, 0, 0, 0);
        // ''' 写图像步骤'''
        bandOut1.FlushCache();
        bandOut2.FlushCache();
        bandOut3.FlushCache();
        // 设置输出图像的几何信息和投影信息
        double[] geotransform = new double[6] { minX, pixelWidth1, 0,maxY,0,pixelHeight1};

        dsOut.SetGeoTransform(geotransform);
        dsOut.SetProjection(ds1.GetProjection());
       /* GDALDestroyDriverManager()*/;

        // 建立输出图像的金字塔
        OSGeo.GDAL.Gdal.SetConfigOption("HFA_USE_RRD", "YES");
        int[] overviewlist = new int[4] { 2, 4, 8, 16 };//4层;

        dsOut.BuildOverviews("nearest", overviewlist);

        //dsOut.
        MessageBox.Show("图像拼接:success");


    }



```![图片说明](https://img-ask.csdn.net/upload/201905/15/1557905356_242793.png)
毕设做了一个无人机影像拼接的算法,结果拼出来是这样的,请问如何能去掉黑边啊?

1个回答

摄像头存在畸变,图像是凸的,先处理一下把畸变去掉再拼接

CALch
CALch 不是,畸变是一种弯曲
5 个月之前 回复
qq_42434995
qq_42434995 畸变就是那些黑边吗
5 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!