C#结合GDAL实现Roberts算子边缘检测

本文介绍了一种基于C#的Roberts边缘检测算法实现方法。通过读取图像文件并利用Roberts算子进行边缘检测处理,实现了对图像边缘的有效提取,并展示了处理前后的图像对比效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

private void btnRoberts_Click(object sender, EventArgs e)
        {
            string openFileName = "";
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                openFileName = ofd.FileName;
            }

            Gdal.AllRegister();

            //更改读写权限
            Dataset srcDs = Gdal.Open(openFileName, Access.GA_Update);

            DataType srcType = srcDs.GetRasterBand(1).DataType;

            int bandCount = srcDs.RasterCount;
            int srcWidth = srcDs.RasterXSize;
            int srcHeight = srcDs.RasterYSize;

            Debug.WriteLine("原始影像数据类型是:{0}", srcType);
            Debug.WriteLine("原始影像的列数:{0}", srcWidth);
            Debug.WriteLine("原始影像的行数:{0}", srcHeight);


            int[] bandArray = new int[bandCount];
            for (int i = 0; i < bandCount; i++)
            {
                bandArray[i] = i + 1;
            }

            if (srcType == DataType.GDT_UInt16)
            {
                int[] dataArray = new int[srcWidth * srcHeight * bandCount];
                int[] newArray = new int[srcWidth * srcHeight * bandCount];
                srcDs.ReadRaster(0, 0, srcWidth, srcHeight, dataArray, srcWidth, srcHeight, bandCount, bandArray, 0, 0, 0);

                /***********Roberts算子实现**************/
                
                int[] pixel = new int[4];
                for (int i = 0; i < srcHeight-1; i++)//不处理最下边
                {
                    for (int j = 0; j < srcWidth - 1; j++)//不处理最右边
                    {
                        //生成Roberts算子
                        pixel[0] = dataArray[i * srcWidth + j];
                        pixel[1] = dataArray[i * srcWidth + j + 1];
                        pixel[2] = dataArray[(i + 1) * srcWidth + j];
                        pixel[3] = dataArray[(i + 1) * srcWidth + j + 1];

                        newArray[i * srcWidth + j] =(int)Math.Sqrt((pixel[0] - pixel[3]) * (pixel[0] - pixel[3]) + (pixel[1] - pixel[2]) * (pixel[1] - pixel[2]));
                    }
                }

                //将更新数值的数据重新写入图像
                srcDs.WriteRaster(0, 0, srcWidth, srcHeight, newArray, srcWidth, srcHeight, bandCount, bandArray, 0, 0, 0);

                srcDs.FlushCache();
            }
            //最后释放资源
            srcDs.Dispose();
            MessageBox.Show("Roberts算子:success");
        }
    

原图:


Roberts边缘检测算子效果:


局部细节:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值