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

来源:互联网 发布:云计算发展历史 编辑:程序博客网 时间:2024/05/16 15:15
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边缘检测算子效果:


局部细节:



1 0
原创粉丝点击