Exemplo n.º 1
0
        /// <summary>
        /// Converter as coordenadas de WGS 84 / UTM zone 23S para latitude e longitude em graus
        /// </summary>
        /// <param name="lonLat">Array de vetor com as duas coordenadas Geodesicas</param>
        /// <param name="tifProjectionReference"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public static double[] LonLatToTifCoordinates(double[][] lonLat, string tifProjectionReference, int imgWidth, int imgHeight)
        {
            // referência do arquivo tif em WGS 84 / UTM zone 23S
            SpatialReference newReference = new SpatialReference(tifProjectionReference);
            // referência em latitude e longitude
            SpatialReference currentReference = GdalUtilities.getWgs84Reference();

            CoordinateTransformation ct = new CoordinateTransformation(currentReference, newReference);

            double[] tifGeoTransformerData = new double[6];
            double[] northwest             = new double[2] {
                lonLat[0][0], lonLat[0][1]
            };
            double[] southeast = new double[2] {
                lonLat[1][0], lonLat[1][1]
            };

            ct.TransformPoint(northwest);
            ct.TransformPoint(southeast);
            southeast = new double[2] {
                (southeast[0] - northwest[0]) / imgWidth,
                (southeast[1] - northwest[1]) / imgHeight
            };

            tifGeoTransformerData[0] = northwest[0];
            tifGeoTransformerData[1] = southeast[0];
            tifGeoTransformerData[2] = 0;
            tifGeoTransformerData[3] = northwest[1];
            tifGeoTransformerData[4] = 0;
            tifGeoTransformerData[5] = southeast[1];

            return(tifGeoTransformerData);
        }
        public void AplyWhiteBalance(string imagePath, string outImagePath)
        {
            using (Dataset image = Gdal.Open(imagePath, Access.GA_ReadOnly)) {
                Band redBand   = GdalUtilities.GetBand(image, ColorInterp.GCI_RedBand);
                Band greenBand = GdalUtilities.GetBand(image, ColorInterp.GCI_GreenBand);
                Band blueBand  = GdalUtilities.GetBand(image, ColorInterp.GCI_BlueBand);

                if (redBand == null || greenBand == null || blueBand == null)
                {
                    throw new NullReferenceException("One or more bands are not available.");
                }

                int width  = redBand.XSize;
                int height = redBand.YSize;

                using (Dataset outImage = Gdal.GetDriverByName("GTiff").Create(outImagePath, width, height, 3, DataType.GDT_Byte, null)) {
                    GdalUtilities.CopyGeoProjection(image, outImage);

                    Band outRedBand   = outImage.GetRasterBand(1);
                    Band outGreenBand = outImage.GetRasterBand(2);
                    Band outBlueBand  = outImage.GetRasterBand(3);
                    GdalUtilities.SetBandsColorInterpretation(outRedBand, outGreenBand, outBlueBand);

                    int[] red   = new int[width * height];
                    int[] green = new int[width * height];
                    int[] blue  = new int[width * height];
                    redBand.ReadRaster(0, 0, width, height, red, width, height, 0, 0);
                    greenBand.ReadRaster(0, 0, width, height, green, width, height, 0, 0);
                    blueBand.ReadRaster(0, 0, width, height, blue, width, height, 0, 0);

                    byte[] outRed   = WhiteBalanceBand(red);
                    byte[] outGreen = WhiteBalanceBand(green);
                    byte[] outBlue  = WhiteBalanceBand(blue);
                    outRedBand.WriteRaster(0, 0, width, height, outRed, width, height, 0, 0);
                    outGreenBand.WriteRaster(0, 0, width, height, outGreen, width, height, 0, 0);
                    outBlueBand.WriteRaster(0, 0, width, height, outBlue, width, height, 0, 0);

                    outImage.FlushCache();
                }
            }
        }