zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    线程基础之进程,线程,任务
    Jobs深入学习
    Quartz的API简介及Jobs和Trigger介绍
    Quartz入门及简单实现
    maven仓库配置阿里云镜像
    Activiti图表bpmn对应的xml文件
    Activiti流程设计工具
    Activiti的25张表
    subprocess.Popen指令包含中文导致乱码问题解决
    Qt5.9使用QWebEngineView加载网页速度非常慢,问题解决
  • 原文地址:https://www.cnblogs.com/dearzhoubi/p/8711000.html
Copyright © 2011-2022 走看看