Win8 Metro(C#)数字图像处理--2.54迭代法图像二值化

简介: 原文:Win8 Metro(C#)数字图像处理--2.54迭代法图像二值化  [函数名称]   迭代法图像二值化      int IterativeThSegment(WriteableBitmap src...
原文: Win8 Metro(C#)数字图像处理--2.54迭代法图像二值化



[函数名称]

  迭代法图像二值化      int IterativeThSegment(WriteableBitmap src)

[函数代码]

<strong>       /// <summary>
        /// Iterative method of image segmention.
        /// </summary>
        /// <param name="src">The source image.</param>
        /// <returns></returns>
        public static WriteableBitmap IterativeThSegment(WriteableBitmap src) ////迭代法阈值分割
        {
            if (src != null)
            {
                int w = src.PixelWidth;
                int h = src.PixelHeight;
                WriteableBitmap dstImage = new WriteableBitmap(w, h);
                byte[] temp = src.PixelBuffer.ToArray();
                byte[] tempMask = (byte[])temp.Clone();
                //定义灰度图像信息存储变量
                int[] srcData = new int[w * h];
                //定义背景和目标像素个数变量C1,C2,总体灰度和变量sum
                int C1 = 0, C2 = 0, sum = 0;
                //定义背景和目标的灰度和变量G1,G2,前后两次灰度均值变量t0,t
                double G1 = 0, G2 = 0, t0 = 256, t = 0;
                //定义阈值变量
                int Th = 0;
                //定义循环控制变量
                bool s = true;
                for(int j=0;j<h;j++)
                {
                    for(int i=0;i<w;i++)
                    {
                        srcData[i + j * w] = (int)((double)tempMask[i * 4 + j * w * 4] * 0.114 + (double)tempMask[i * 4 + 1 + j * w * 4] * 0.587 + (double)tempMask[i * 4 + 2 + j * w * 4]*0.299);
                        sum += srcData[i + j * w];
                    }
                }
                //初始化阈值
                Th = sum / (w*h);
                while (s)
                {
                    for (int i = 0; i < srcData.Length; i++)
                    {
                        if (srcData[i] < Th)
                        {
                            C1++;
                            G1 += srcData[i];
                        }
                        else
                        {
                            C2++;
                            G2 += srcData[i];
                        }
                    }
                    t = (double)((G1 / C1 + G2 / C2) / 2);
                    if (Math.Abs(t - t0) < 1)
                    {
                        s = false;
                        for (int j = 0; j < h; j++)
                        {
                            for (int i = 0; i < w; i++)
                            {
                                temp[i * 4 + j * w * 4] = temp[i * 4 + 1 + j * w * 4] = temp[i * 4 + 2 + j * w * 4] = (byte)(srcData[i + j * w] < Th ? 0 : 255);
                            }
                        }
                    }
                    else
                    {
                        t0 = t;
                        C1 = 0; C2 = 0; G1 = 0; G2 = 0;
                        //更新阈值
                        Th = (int)t;
                    }
                }
                Stream sTemp = dstImage.PixelBuffer.AsStream();
                sTemp.Seek(0, SeekOrigin.Begin);
                sTemp.Write(temp, 0, w * 4 * h);
                return dstImage;
            }
            else
            {
                return null;
            }
        }
<span style="font-size:14px;">[图像效果]</span></strong>

目录
相关文章
|
4月前
|
监控 API 开发工具
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK获取每张图像的微秒时间和FrameID功能(C#)
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK获取每张图像的微秒时间和FrameID功能(C#)
55 0
|
4月前
|
存储 传感器 开发工具
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK修改图像像素格式Mono8或者Mono10(C#)
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK修改图像像素格式Mono8或者Mono10(C#)
59 0
|
4月前
|
存储 数据处理 开发工具
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK实现相机的高速图像保存(C#)
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK实现相机的高速图像保存(C#)
40 0
|
4月前
|
监控 算法 开发工具
Baumer工业相机堡盟工业相机如何联合NEOAPI SDK和OpenCV实现获取图像并对图像进行边缘检测(C#)
Baumer工业相机堡盟工业相机如何联合NEOAPI SDK和OpenCV实现获取图像并对图像进行边缘检测(C#)
42 1
|
4月前
|
存储 监控 开发工具
Baumer工业相机堡盟工业相机如何联合NEOAPI SDK和OpenCV实现相机图像转换为AVI视频格式(C#)
Baumer工业相机堡盟工业相机如何联合NEOAPI SDK和OpenCV实现相机图像转换为AVI视频格式(C#)
28 0
|
4月前
|
数据采集 API 开发工具
Baumer工业相机堡盟工业相机如何通过BGAPI SDK实现Raw格式的图像保存(C#)
Baumer工业相机堡盟工业相机如何通过BGAPI SDK实现Raw格式的图像保存(C#)
27 0
|
4月前
|
存储 传感器 监控
Baumer工业相机堡盟工业相机如何通过BGAPISDK将相机图像高速保存到电脑内存(C#)
Baumer工业相机堡盟工业相机如何通过BGAPISDK将相机图像高速保存到电脑内存(C#)
55 0
|
4月前
|
存储 数据处理 开发工具
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK实现相机图像转换由Mono10转换为Mono8(C#)
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK实现相机图像转换由Mono10转换为Mono8(C#)
38 0
|
4月前
|
存储 新制造 开发工具
Baumer工业相机堡盟工业相机如何联合NEOAPI SDK和OpenCV实现相机图像转换为Mat图像格式(C#)
Baumer工业相机堡盟工业相机如何联合NEOAPI SDK和OpenCV实现相机图像转换为Mat图像格式(C#)
31 0
|
4月前
|
存储 传感器 监控
工业相机如何实现实时和本地Raw格式图像和Bitmap格式图像的保存和相互转换(C#代码,UI界面版)
工业相机如何实现实时和本地Raw格式图像和Bitmap格式图像的保存和相互转换(C#代码,UI界面版)
32 0