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