GDAL实现图像反色

using OSGeo.GDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace antiColor
{
    class Program
    {
        static void Main(string[] args)
        {
            Gdal.AllRegister();
            string srcFile = @"E:\GDAL\GDALPractice\GDAL_in_CSharp\chapter07\ZoomFile01.tif";
            Dataset srcDs= Gdal.Open(srcFile, Access.GA_ReadOnly);
            Driver srcDrv = srcDs.GetDriver();

            int xSize = srcDs.RasterXSize;
            int ySize = srcDs.RasterYSize;
            int bandCount = srcDs.RasterCount;

            string dstFile=@"E:\GDAL\GDALPractice\GDAL_in_CSharp\chapter07\antiColor.tif";
            Dataset dstDs= srcDrv.Create(dstFile, xSize, ySize, bandCount, DataType.GDT_Byte, null);

            byte[] srcArray=new byte[xSize];//存储一行的大小
            byte[] dstArray=new byte[xSize];

            for (int i = 1; i <= bandCount; i++)
            {
                Band srcBand = srcDs.GetRasterBand(i);
                Band dstBand = dstDs.GetRasterBand(i);

                for (int j = 0; j < ySize; j++)//循环图像高
                {
                    srcBand.ReadRaster(0, j, xSize, 1, srcArray, xSize, 1, 0, 0);

                    for (int h = 0; h < xSize; h++)//循环图像宽
                    {
                        dstArray[h] = (byte)((255) - srcArray[h]);
                    }
                    dstBand.WriteRaster(0, j, xSize, 1, dstArray, xSize, 1, 0, 0);
                }
            }
            dstDs.FlushCache();
            dstDs.Dispose();
            Console.WriteLine("成功");
            Console.ReadKey();
        }
    }
}

效果:

原图:

反色结果:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值