⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 grayprocess.cs

📁 matlab计算的自适应阈值分隔
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace WindowsApplication2
{
    public enum GrayMethod
    {
        /// <summary>
        /// 加权平均法
        /// </summary>
        WeightAveraging,

        /// <summary>
        /// 最大值法
        /// </summary>
        Maximum,

        /// <summary>
        /// 平均值法
        /// </summary>
        Average
    }

    class grayprocess
    {
        public int BPP = 4;
        private Bitmap b;
        private int width = 0;
        private int height = 0;
        public int[] Red = new int[256];
        public int[] Green = new int[256];
        public int[] Blue = new int[256];
        public Bitmap Gray(Bitmap b, GrayMethod grayMethod)
        {
            int width = b.Width;
            int height = b.Height;

            BitmapData data = b.LockBits(new Rectangle(0, 0, width, height),
              ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);

            unsafe
            {
                byte* p = (byte*)data.Scan0;
                int offset = data.Stride - width * BPP;

                byte R, G, B, gray;

                switch (grayMethod)
                {
                    case GrayMethod.Maximum:
                        for (int y = 0; y < height; y++)
                        {
                            for (int x = 0; x < width; x++)
                            {
                                R = p[2];
                                G = p[1];
                                B = p[0];

                                // gray = Max( R, G, B )
                                gray = (gray = B >= G ? B : G) >= R ? gray : R;

                                p[0] = p[1] = p[2] = gray;

                                p += BPP;
                            } // x
                            p += offset;
                        } // y
                        break;

                    case GrayMethod.Average:
                        for (int y = 0; y < height; y++)
                        {
                            for (int x = 0; x < width; x++)
                            {
                                R = p[2];
                                G = p[1];
                                B = p[0];

                                // gray = ( R + G + B ) / 3
                                gray = (byte)((R + G + B) / 3);

                                p[0] = p[1] = p[2] = gray;

                                p += BPP;
                            } // x
                            p += offset;
                        } // y
                        break;

                    case GrayMethod.WeightAveraging:
                    default:
                        for (int y = 0; y < height; y++)
                        {
                            for (int x = 0; x < width; x++)
                            {
                                R = p[2];
                                G = p[1];
                                B = p[0];

                                // gray = 0.3*R + 0.59*G + 0.11*B
                                gray = (byte)((19661 * R + 38666 * G + 7209 * B) >> 16);

                                p[0] = p[1] = p[2] = gray;

                                p += BPP;
                            } // x
                            p += offset;
                        } // y
                        break;
                } // switch
            }

            b.UnlockBits(data);

            return b;
        }
        public void histogram(Bitmap b)
        {


            int width = b.Width;
            int height = b.Height;


            BitmapData data = b.LockBits(new Rectangle(0, 0, width, height),
              ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);

            unsafe
            {
                byte* p = (byte*)data.Scan0;
                int offset = data.Stride - width * BPP;

                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++)
                    {
                        Red[p[2]]++;
                        Green[p[1]]++;
                        Blue[p[0]]++;

                        p += BPP;
                    } // x

                    p += offset;
                } // y
            }

            b.UnlockBits(data);

            // 生成 R、G、B 三分量统计类

        }
        public Bitmap Threshold(Bitmap b, int thre)
        {
            Bitmap result = b;
            int width = b.Width;
            int height = b.Height;

            BitmapData data = b.LockBits(new Rectangle(0, 0, width, height),
              ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);

            unsafe
            {
                byte* p = (byte*)data.Scan0;
                int offset = data.Stride - width * BPP;
                byte R, G, B;

                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++)
                    {

                        R = p[2];
                        G = p[1];
                        B = p[0];
                        if (R < thre)
                        { p[2] = 0; p[1] = 0; p[0] = 0; }
                        else
                        { p[2] = 255; p[1] = 255; p[0] = 255; }

                        p += BPP;
                    } // x
                    p += offset;
                } // y
            }
            b.UnlockBits(data);
            return b;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -