国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C# > 正文

C#灰度化圖像的實(shí)例代碼

2020-01-24 03:08:19
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

用偽語(yǔ)句可以表示如下

public bitmap GrayScal(bitmap orgbmp)
{
    建立一個(gè)與原圖片等大的8位的圖片
    取出原圖像中的每一個(gè)點(diǎn)
    新圖像的點(diǎn)=原圖像點(diǎn)的紅色量*系數(shù)1+綠色量*系數(shù)2+黃色量*系統(tǒng)3
    返回新圖像
}

復(fù)制代碼 代碼如下:

/// <summary>
    /// 對(duì)圖像進(jìn)行點(diǎn)運(yùn)算,
    /// </summary>
    public class PointTrans
    {
        private readonly double cb;
        private readonly double cg;
        private readonly double cr;

        /// <summary>
        /// 做點(diǎn)運(yùn)算,要給每一個(gè)偏量,做一下設(shè)置,比如做圖像的灰度圖就需要現(xiàn)設(shè)置
        /// </summary>
        /// <param name="cr"></param>
        /// <param name="cg"></param>
        /// <param name="cb"></param>
        public PointTrans(double cr, double cg, double cb)
        {
            this.cr = cr;
            this.cg = cg;
            this.cb = cb;
        }

        public  Bitmap GrayScaleBmp(Bitmap orgData)
        {
            int bmpWidth = orgData.Width, bmpHeight = orgData.Height;
            Bitmap destData = ImageTools.CreateGrayscaleImage(bmpWidth, bmpHeight);
            Rectangle bmpRect=new Rectangle(0,0,bmpWidth,bmpHeight);

            BitmapData orgBmpData = orgData.LockBits(bmpRect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);

            BitmapData destBmpData = destData.LockBits(bmpRect, ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
            ProcessFilter(orgBmpData,destBmpData);

            orgData.UnlockBits(orgBmpData);
            destData.UnlockBits(destBmpData);
            return destData;

        }


        protected unsafe void ProcessFilter(BitmapData sourceData, BitmapData destinationData)
        {
            // get width and height
            int width = sourceData.Width;
            int height = sourceData.Height;

            int srcOffset = sourceData.Stride - width*3;
            int dstOffset = destinationData.Stride - width;

            // do the job
            byte* src = (byte*) sourceData.Scan0.ToPointer();
            byte* dst = (byte*) destinationData.Scan0.ToPointer();

            // for each line
            for (int y = 0; y < height; y++)
            {
                // for each pixel
                for (int x = 0; x < width; x++, src += 3, dst++)
                {
                    *dst = (byte) (cr*src[RGB.R] + cg*src[RGB.G] + cb*src[RGB.B]);
                }
                src += srcOffset;
                dst += dstOffset;
            }
        }


    }

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 新昌县| 儋州市| 阳朔县| 苍南县| 方正县| 桐庐县| 正安县| 曲阳县| 仙游县| 余江县| 塘沽区| 漳平市| 嫩江县| 鄂伦春自治旗| 诸城市| 偃师市| 阳原县| 永善县| 青州市| 合江县| 伊通| 天柱县| 渝北区| 宽甸| 青浦区| 集贤县| 武平县| 大安市| 枣阳市| 闵行区| 微博| 南投县| 静安区| 洛川县| 新田县| 绥中县| 赤壁市| 大埔县| 嘉禾县| 大埔县| 东阿县|