c#数字图像处理(十三)图像开运算与闭运算

图像开运算与闭运算定义

二值图像开运算的数学表达式为:

g(x, y)=open[f(x, y ), B]=dilate{erode[f(x, y),B],B}

二值图像的开运算事实上就是先作腐蚀运算,再作膨胀运算。

二值图像闭运算的数学表达式为:

g(x, y)=close[f(x, y ), B]=erode{dilate[f(x, y),B],B}

二值图像的闭运算事实上就是先作膨胀运算,再作腐蚀运算

        private void opening_Click(object sender, EventArgs e)
        {
            if (curBitmap != null)
            {
                struction struForm = new struction();
                struForm.Text = "开运算结构元素";
                if (struForm.ShowDialog() == DialogResult.OK)
                {
                    Rectangle rect = new Rectangle(0, 0, curBitmap.Width, curBitmap.Height);
                    System.Drawing.Imaging.BitmapData bmpData = curBitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, curBitmap.PixelFormat);
                    IntPtr ptr = bmpData.Scan0;
                    int bytes = curBitmap.Width * curBitmap.Height;
                    byte[] grayValues = new byte[bytes];
                    Marshal.Copy(ptr, grayValues, 0, bytes);

                    byte flagStru = struForm.GetStruction;

                    byte[] temp1Array = new byte[bytes];
                    byte[] tempArray = new byte[bytes];
                    for (int i = 0; i < bytes; i++)
                    {
                        tempArray[i] = temp1Array[i] = 255;
                    }

                    switch (flagStru)
                    {
                        case 0x11:
                            //腐蚀运算
                            for (int i = 0; i < curBitmap.Height; i++)
                            {
                                for (int j = 1; j < curBitmap.Width - 1; j++)
                                {
                                    if (grayValues[i * curBitmap.Width + j] == 0 &&
                                        grayValues[i * curBitmap.Width + j + 1] == 0 &&
                                        grayValues[i * curBitmap.Width + j - 1] == 0)
                                    {
                                        temp1Array[i * curBitmap.Width + j] = 0;
                                    }

                                }
                            }
                            //膨胀运算
                            for (int i = 0; i < curBitmap.Height; i++)
                            {
                                for (int j = 1; j < curBitmap.Width - 1; j++)
                                {
                                    if (temp1Array[i * curBitmap.Width + j] == 0 ||
                                        temp1Array[i * curBitmap.Width + j + 1] == 0 ||
                                        temp1Array[i * curBitmap.Width + j - 1] == 0)
                                    {
                                        tempArray[i * curBitmap.Width + j] = 0;
                                    }

                                }
                            }
                            break;
                        case 0x21:
                            //腐蚀运算
                            for (int i = 0; i < curBitmap.Height; i++)
                            {
                                for (int j = 2; j < curBitmap.Width - 2; j++)
                                {
                                    if (grayValues[i * curBitmap.Width + j] == 0 &&
                                        grayValues[i * curBitmap.Width + j + 1] == 0 &&
                                        grayValues[i * curBitmap.Width + j - 1] == 0 &&
                                        grayValues[i * curBitmap.Width + j + 2] == 0 &&
                                        grayValues[i * curBitmap.Width + j - 2] == 0)
                                    {
                                        temp1Array[i * curBitmap.Width + j] = 0;
                                    }

                                }
                            }
                            //膨胀运算
                            for (int i = 0; i < curBitmap.Height; i++)
                            {
                                for (int j = 2; j < curBitmap.Width - 2; j++)
                                {
                                    if (temp1Array[i * curBitmap.Width + j] == 0 ||
                                        temp1Array[i * curBitmap.Width + j + 1] == 0 ||
                                        temp1Array[i * curBitmap.Width + j - 1] == 0 ||
                                        temp1Array[i * curBitmap.Width + j + 2] == 0 ||
                                        temp1Array[i * curBitmap.Width + j - 2] == 0)
                                    {
                                        tempArray[i * curBitmap.Width + j] = 0;
                                    }

                                }
                            }
                            break;
                        case 0x12:
                            //腐蚀运算
                            for (int i = 1; i < curBitmap.Height - 1; i++)
                            {
                                for (int j = 0; j < curBitmap.Width; j++)
                                {
                                    if (grayValues[i * curBitmap.Width + j] == 0 &&
                                        grayValues[(i - 1) * curBitmap.Width + j] == 0 &&
                                        grayValues[(i + 1) * curBitmap.Width + j] == 0)
                                    {
                                        temp1Array[i * curBitmap.Width + j] = 0;
                                    }

                                }
                            }
                            //膨胀运算
                            for (int i = 1; i < curBitmap.Height - 1; i++)
                            {
                                for (int j = 0; j < curBitmap.Width; j++)
                                {
                                    if (temp1Array[i * curBitmap.Width + j] == 0 ||
                                        temp1Array[(i - 1) * curBitmap.Width + j] == 0 ||
                                        temp1Array[(i + 1) * curBitmap.Width + j] == 0)
                                    {
                                        tempArray[i * curBitmap.Width + j] = 0;
                                    }

                                }
                            }
                            break;
                        case 0x22:
                            //腐蚀运算
                            for (int i = 2; i < curBitmap.Height - 2; i++)
                            {
                                for (int j = 0; j < curBitmap.Width; j++)
                                {
                                    if (grayValues[i * curBitmap.Width + j] == 0 &&
                                        grayValues[(i - 1) * curBitmap.Width + j] == 0 &&
                                        grayValues[(i + 1) * curBitmap.Width + j] == 0 &&
                                        grayValues[(i - 2) * curBitmap.Width + j] == 0 &&
                                        grayValues[(i + 2) * curBitmap.Width + j] == 0)
                                    {
                                        temp1Array[i * curBitmap.Width + j] = 0;
                                    }

                                }
                            }
                            //膨胀运算
                            for (int i = 2; i < curBitmap.Height - 2; i++)
                            {
                                for (int j = 0; j < curBitmap.Width; j++)
                                {
                                    if (temp1Array[i * curBitmap.Width + j] == 0 ||
                                        temp1Array[(i - 1) * curBitmap.Width + j] == 0 ||
                                        temp1Array[(i + 1) * curBitmap.Width + j] == 0 ||
                                        temp1Array[(i - 2) * curBitmap.Width + j] == 0 ||
                                        temp1Array[(i + 2) * curBitmap.Width + j] == 0)
                                    {
                                        tempArray[i * curBitmap.Width + j] = 0;
                                    }

                                }
                            }
                            break;
                        case 0x14:
                            //腐蚀运算
                            for (int i = 1; i < curBitmap.Height - 1; i++)
                            {
                                for (int j = 1; j < curBitmap.Width - 1; j++)
                                {
                                    if (grayValues[i * curBitmap.Width + j] == 0 &&
                                        grayValues[(i - 1) * curBitmap.Width + j] == 0 &&
                                        grayValues[(i + 1) * curBitmap.Width + j] == 0 &&
                                        grayValues[i * curBitmap.Width + j + 1] == 0 &&
                                        grayValues[i * curBitmap.Width + j - 1] == 0)
                                    {
                                        temp1Array[i * curBitmap.Width + j] = 0;
                                    }

                                }
                            }
                            //膨胀运算
                            for (int i = 1; i < curBitmap.Height - 1; i++)
                            {
                                for (int j = 1; j < curBitmap.Width - 1; j++)
                                {
                                    if (temp1Array[i * curBitmap.Width + j] == 0 ||
                                        temp1Array[(i - 1) * curBitmap.Width + j] == 0 ||
                                        temp1Array[(i + 1) * curBitmap.Width + j] == 0 ||
                                        temp1Array[i * curBitmap.Width + j + 1] == 0 ||
                                        temp1Array[i * curBitmap.Width + j - 1] == 0)
                                    {
                                        tempArray[i * curBitmap.Width + j] = 0;
                                    }

                                }
                            }
                            break;
                        case 0x24:
                            //腐蚀运算
                            for (int i = 2; i < curBitmap.Height - 2; i++)
                            {
                                for (int j = 2; j < curBitmap.Width - 2; j++)
                                {
                                    if (grayValues[i * curBitmap.Width + j] == 0 &&
                                        grayValues[(i - 1) * curBitmap.Width + j] == 0 &&
                                        grayValues[(i + 1) * curBitmap.Width + j] == 0 &&
                                        grayValues[(i - 2) * curBitmap.Width + j] == 0 &&
                                        grayValues[(i + 2) * curBitmap.Width + j] == 0 &&
                                        grayValues[i * curBitmap.Width + j + 1] == 0 &&
                                        grayValues[i * curBitmap.Width + j - 1] == 0 &&
                                        grayValues[i * curBitmap.Width + j + 2] == 0 &&
                                        grayValues[i * curBitmap.Width + j - 2] == 0)
                                    {
                                        temp1Array[i * curBitmap.Width + j] = 0;
                                    }

                                }
                            }
                            //膨胀运算
                            for (int i = 2; i < curBitmap.Height - 2; i++)
                            {
                                for (int j = 2; j < curBitmap.Width - 2; j++)
                                {
                                    if (temp1Array[i * curBitmap.Width + j] == 0 ||
                                        temp1Array[(i - 1) * curBitmap.Width + j] == 0 ||
                                        temp1Array[(i + 1) * curBitmap.Width + j] == 0 ||
                                        temp1Array[(i - 2) * curBitmap.Width + j] == 0 ||
                                        temp1Array[(i + 2) * curBitmap.Width + j] == 0 ||
                                        temp1Array[i * curBitmap.Width + j + 1] == 0 ||
                                        temp1Array[i * curBitmap.Width + j - 1] == 0 ||
                                        temp1Array[i * curBitmap.Width + j + 2] == 0 ||
                                        temp1Array[i * curBitmap.Width + j - 2] == 0)
                                    {
                                        tempArray[i * curBitmap.Width + j] = 0;
                                    }

                                }
                            }
                            break;
                        case 0x18:
                            //腐蚀运算
                            for (int i = 1; i < curBitmap.Height - 1; i++)
                            {
                                for (int j = 1; j < curBitmap.Width - 1; j++)
                                {
                                    if (grayValues[i * curBitmap.Width + j] == 0 &&
                                        grayValues[(i - 1) * curBitmap.Width + j] == 0 &&
                                        grayValues[(i + 1) * curBitmap.Width + j] == 0 &&
                                        grayValues[i * curBitmap.Width + j + 1] == 0 &&
                                        grayValues[i * curBitmap.Width + j - 1] == 0 &&
                                        grayValues[(i - 1) * curBitmap.Width + j - 1] == 0 &&
                                        grayValues[(i + 1) * curBitmap.Width + j - 1] == 0 &&
                                        grayValues[(i - 1) * curBitmap.Width + j + 1] == 0 &&
                                        grayValues[(i + 1) * curBitmap.Width + j + 1] == 0)
                                    {
                                        temp1Array[i * curBitmap.Width + j] = 0;
                                    }

                                }
                            }
                            //膨胀运算
                            for (int i = 1; i < curBitmap.Height - 1; i++)
                            {
                                for (int j = 1; j < curBitmap.Width - 1; j++)
                                {
                                    if (temp1Array[i * curBitmap.Width + j] == 0 ||
                                        temp1Array[(i - 1) * curBitmap.Width + j] == 0 ||
                                        temp1Array[(i + 1) * curBitmap.Width + j] == 0 ||
                                        temp1Array[i * curBitmap.Width + j + 1] == 0 ||
                                        temp1Array[i * curBitmap.Width + j - 1] == 0 ||
                                        temp1Array[(i - 1) * curBitmap.Width + j - 1] == 0 ||
                                        temp1Array[(i + 1) * curBitmap.Width + j - 1] == 0 ||
                                        temp1Array[(i - 1) * curBitmap.Width + j + 1] == 0 ||
                                        temp1Array[(i + 1) * curBitmap.Width + j + 1] == 0)
                                    {
                                        tempArray[i * curBitmap.Width + j] = 0;
                                    }

                                }
                            }
                            break;
                        case 0x28:
                            //腐蚀运算
                            for (int i = 2; i < curBitmap.Height - 2; i++)
                            {
                                for (int j = 2; j < curBitmap.Width - 2; j++)
                                {
                                    if (grayValues[(i - 2) * curBitmap.Width + j - 2] == 0 &&
                                        grayValues[(i - 2) * curBitmap.Width + j - 1] == 0 &&
                                        grayValues[(i - 2) * curBitmap.Width + j] == 0 &&
                                        grayValues[(i - 2) * curBitmap.Width + j + 1] == 0 &&
                                        grayValues[(i - 2) * curBitmap.Width + j + 2] == 0 &&
                                        grayValues[(i - 1) * curBitmap.Width + j - 2] == 0 &&
                                        grayValues[(i - 1) * curBitmap.Width + j - 1] == 0 &&
                                        grayValues[(i - 1) * curBitmap.Width + j] == 0 &&
                                        grayValues[(i - 1) * curBitmap.Width + j + 1] == 0 &&
                                        grayValues[(i - 1) * curBitmap.Width + j + 2] == 0 &&
                                        grayValues[i * curBitmap.Width + j - 2] == 0 &&
                                        grayValues[i * curBitmap.Width + j - 1] == 0 &&
                                        grayValues[i * curBitmap.Width + j] == 0 &&
                                        grayValues[i * curBitmap.Width + j + 1] == 0 &&
                                        grayValues[i * curBitmap.Width + j + 2] == 0 &&
                                        grayValues[(i + 2) * curBitmap.Width + j - 2] == 0 &&
                                        grayValues[(i + 2) * curBitmap.Width + j - 1] == 0 &&
                                        grayValues[(i + 2) * curBitmap.Width + j] == 0 &&
                                        grayValues[(i + 2) * curBitmap.Width + j + 1] == 0 &&
                                        grayValues[(i + 2) * curBitmap.Width + j + 2] == 0 &&
                                        grayValues[(i + 1) * curBitmap.Width + j - 2] == 0 &&
                                        grayValues[(i + 1) * curBitmap.Width + j - 1] == 0 &&
                                        grayValues[(i + 1) * curBitmap.Width + j] == 0 &&
                                        grayValues[(i + 1) * curBitmap.Width + j + 1] == 0 &&
                                        grayValues[(i + 1) * curBitmap.Width + j + 2] == 0)
                                    {
                                        temp1Array[i * curBitmap.Width + j] = 0;
                                    }

                                }
                            }
                            //膨胀运算
                            for (int i = 2; i < curBitmap.Height - 2; i++)
                            {
                                for (int j = 2; j < curBitmap.Width - 2; j++)
                                {
                                    if (temp1Array[(i - 2) * curBitmap.Width + j - 2] == 0 ||
                                        temp1Array[(i - 2) * curBitmap.Width + j - 1] == 0 ||
                                        temp1Array[(i - 2) * curBitmap.Width + j] == 0 ||
                                        temp1Array[(i - 2) * curBitmap.Width + j + 1] == 0 ||
                                        temp1Array[(i - 2) * curBitmap.Width + j + 2] == 0 ||
                                        temp1Array[(i - 1) * curBitmap.Width + j - 2] == 0 ||
                                        temp1Array[(i - 1) * curBitmap.Width + j - 1] == 0 ||
                                        temp1Array[(i - 1) * curBitmap.Width + j] == 0 ||
                                        temp1Array[(i - 1) * curBitmap.Width + j + 1] == 0 ||
                                        temp1Array[(i - 1) * curBitmap.Width + j + 2] == 0 ||
                                        temp1Array[i * curBitmap.Width + j - 2] == 0 ||
                                        temp1Array[i * curBitmap.Width + j - 1] == 0 ||
                                        temp1Array[i * curBitmap.Width + j] == 0 ||
                                        temp1Array[i * curBitmap.Width + j + 1] == 0 ||
                                        temp1Array[i * curBitmap.Width + j + 2] == 0 ||
                                        temp1Array[(i + 2) * curBitmap.Width + j - 2] == 0 ||
                                        temp1Array[(i + 2) * curBitmap.Width + j - 1] == 0 ||
                                        temp1Array[(i + 2) * curBitmap.Width + j] == 0 ||
                                        temp1Array[(i + 2) * curBitmap.Width + j + 1] == 0 ||
                                        temp1Array[(i + 2) * curBitmap.Width + j + 2] == 0 ||
                                        temp1Array[(i + 1) * curBitmap.Width + j - 2] == 0 ||
                                        temp1Array[(i + 1) * curBitmap.Width + j - 1] == 0 ||
                                        temp1Array[(i + 1) * curBitmap.Width + j] == 0 ||
                                        temp1Array[(i + 1) * curBitmap.Width + j + 1] == 0 ||
                                        temp1Array[(i + 1) * curBitmap.Width + j + 2] == 0)
                                    {
                                        tempArray[i * curBitmap.Width + j] = 0;
                                    }

                                }
                            }
                            break;
                        default:
                            MessageBox.Show("错误的结构元素!");
                            break;
                    }


                    grayValues = (byte[])tempArray.Clone();

                    System.Runtime.InteropServices.Marshal.Copy(grayValues, 0, ptr, bytes);
                    curBitmap.UnlockBits(bmpData);
                }

                Invalidate();
            }
        }

 

        #region 关于图像尺寸的说明

        //本代码只能处理8位深度的512*512图像。可自行修改,如修改3位水平方向结构元素代码:

        //01修改成如下代码即可处理任意尺寸的8位深度的图像
        //int bytes = bmpData.Stride * curBitmap.Height;
        //for (int i = 0; i < curBitmap.Height; i++)
        //{
        //    for (int j = 1; j < curBitmap.Width - 1; j++)
        //    {
        //        if (grayValues[i * bmpData.Stride + j] == 0 &&
        //            grayValues[i * bmpData.Stride + j + 3] == 0 &&
        //            grayValues[i * bmpData.Stride + j - 1] == 0)
        //        {
        //            tempArray[i * bmpData.Stride + j] = 0;
        //            tempArray[i * bmpData.Stride + j + 1] = 0;
        //            tempArray[i * bmpData.Stride + j + 2] = 0;
        //        }
        //    }
        //}

        //for (int i = 0; i < curBitmap.Height; i++)
        //{
        //    for (int j = 1; j < curBitmap.Width - 1; j++)
        //    {
        //        if (grayValues[i * bmpData.Stride + j] == 0 ||
        //            grayValues[i * bmpData.Stride + j + 3] == 0 ||
        //            grayValues[i * bmpData.Stride + j - 1] == 0)
        //        {
        //            tempArray[i * bmpData.Stride + j] = 0;
        //            tempArray[i * bmpData.Stride + j + 1] = 0;
        //            tempArray[i * bmpData.Stride + j + 2] = 0;
        //        }
        //    }
        //}

        //02修改成如下代码即可处理任意尺寸的24位深度的图像
        //int bytes = bmpData.Stride * curBitmap.Height;
        //for (int i = 0; i < curBitmap.Height; i++)
        //{
        //    for (int j = 4; j < curBitmap.Width * 3 - 3; j += 3)
        //    {
        //        if (grayValues[i * bmpData.Stride + j] == 0 &&
        //            grayValues[i * bmpData.Stride + j + 3] == 0 &&
        //            grayValues[i * bmpData.Stride + j - 1] == 0)
        //        {
        //            tempArray[i * bmpData.Stride + j] = 0;
        //            tempArray[i * bmpData.Stride + j + 1] = 0;
        //            tempArray[i * bmpData.Stride + j + 2] = 0;
        //        }
        //    }
        //}

        //for (int i = 0; i < curBitmap.Height; i++)
        //{
        //    for (int j = 1; j < curBitmap.Width - 1; j++)
        //    {
        //        if (grayValues[i * bmpData.Stride + j] == 0 ||
        //            grayValues[i * bmpData.Stride + j + 3] == 0 ||
        //            grayValues[i * bmpData.Stride + j - 1] == 0)
        //        {
        //            tempArray[i * bmpData.Stride + j] = 0;
        //            tempArray[i * bmpData.Stride + j + 1] = 0;
        //            tempArray[i * bmpData.Stride + j + 2] = 0;
        //        }
        //    }
        //}
        #endregion

 

转载于:https://www.cnblogs.com/dearzhoubi/p/8711000.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值