Ejemplo n.º 1
0
        } // end of Relief

        /// <summary>
        /// 对图像进行灰度浮雕处理
        /// </summary>
        /// <param name="b">位图流</param>
        /// <param name="angle">角度[0, 360]</param>
        /// <returns></returns>
        public Bitmap Emboss(Bitmap b, int angle)
        {
            // 角度转弧度
            double radian = (double)angle * Math.PI / 180.0;

            // 每一权的弧度间隔
            double pi4 = Math.PI / 4.0;

            // 对图像进行卷积变换
            Matrix3x3 m = new Matrix3x3();

            m.TopLeft  = Convert.ToInt32(Math.Cos(radian + pi4) * 256);
            m.TopMid   = Convert.ToInt32(Math.Cos(radian + 2.0 * pi4) * 256);
            m.TopRight = Convert.ToInt32(Math.Cos(radian + 3.0 * pi4) * 256);

            m.MidLeft  = Convert.ToInt32(Math.Cos(radian) * 256);
            m.Center   = 0;
            m.MidRight = Convert.ToInt32(Math.Cos(radian + 4.0 * pi4) * 256);

            m.BottomLeft  = Convert.ToInt32(Math.Cos(radian - pi4) * 256);
            m.BottomMid   = Convert.ToInt32(Math.Cos(radian - 2.0 * pi4) * 256);
            m.BottomRight = Convert.ToInt32(Math.Cos(radian - 3.0 * pi4) * 256);

            m.Scale  = 256;
            m.Offset = 128;

            b = m.Convolute(b);

            // 对图像进行灰度变换
            GrayProcessing gp = new GrayProcessing();

            b = gp.Gray(b, GrayProcessing.GrayMethod.WeightAveraging);

            return(b);
        } // end of Emboss
Ejemplo n.º 2
0
        } // end of Thresholding

        /// <summary>
        /// 自适应阈值
        /// </summary>
        /// <param name="b">位图流</param>
        /// <returns></returns>
        public Bitmap AutoFitThreshold(Bitmap b)
        {
            // 图像灰度化
            GrayProcessing gp = new GrayProcessing();

            b = gp.Gray(b, GrayMethod.WeightAveraging);

            // 建立直方图,并获取灰度统计信息
            Histogram histogram = new Histogram(b);

            int [] GrayLevel = histogram.Red.Value;

            int peak1, peak2, valley;
            int peak1Index, peak2Index, valleyIndex;

            // 取双峰
            peak1      = peak2 = GrayLevel[0];
            peak1Index = peak2Index = 0;
            for (int i = 1; i < 256; i++)
            {
                // 如果产生新的高峰,则将第一峰退居第二峰,新的高峰升为第一峰
                if (GrayLevel[i] > peak1)
                {
                    peak2      = peak1;
                    peak2Index = peak1Index;

                    peak1      = GrayLevel[i];
                    peak1Index = i;
                }
            } // i

            // 判断两个峰值索引
            int max = peak1Index;
            int min = peak2Index;

            if (max < min)
            {
                int t = max;
                max = min;
                min = t;
            }

            // 取峰谷
            valley      = GrayLevel[min];
            valleyIndex = min;
            for (int i = min; i < max; i++)
            {
                if (GrayLevel[i] < valley)
                {
                    valley      = GrayLevel[i];
                    valleyIndex = i;
                }
            } // i

            // 根据找到的谷值对图像进行二值化
            return(Thresholding(b, (byte)valleyIndex));
        } // end of AutoFitThreshold
Ejemplo n.º 3
0
        /************************************************************
        *
        * 统计像素点、水印文字
        *
        ************************************************************/


        /// <summary>
        /// 统计出一个字符含多少个像素点
        /// </summary>
        /// <param name="character">字符</param>
        /// <returns></returns>
        public int CountTextDots(char character)
        {
            Bitmap   b = new Bitmap(20, 20);
            Graphics g = Graphics.FromImage(b);

            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            g.SmoothingMode     = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

            Font       font  = new Font(new FontFamily("宋体"), 12, FontStyle.Regular);
            SolidBrush brush = new SolidBrush(Color.White);

            g.DrawString(character.ToString(), font, brush, 0, 0);
            g.Save();

            GrayProcessing gp = new GrayProcessing();

            byte[,] Gray = gp.BinaryArray(b, 128);

            int count = 0;

            for (int y = 0; y < 20; y++)
            {
                for (int x = 0; x < 20; x++)
                {
                    // 统计白点
                    if (Gray[x, y] > 128)
                    {
                        count++;
                    }
                } // x
            }     // y

            b.Dispose();

            return(count);
        } // end of CountTextDots