彩色图像得到灰色图像求指教

.Net技术 码拜 9年前 (2015-05-11) 1074次浏览 0个评论

彩色图像得到灰色图像求指教最近学图像处理,刚开始就出现问题了,求人帮忙看下,为什么按照这样处理得到的图像,只有上半部分被黑白处理了啊。而且实际上放大能看到图像并不是纯黑白的,求解,下面的方法有什么问题?
curBitmap为载入的jpg图片。
 private void  MemeryMethod()
        {
            if (null == curBitmap)
                return;

            int width = curBitmap.Width;
            int height = curBitmap.Height;
            Rectangle rect = new Rectangle(0, 0, width, height);

            System.Drawing.Imaging.BitmapData bmpData = curBitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, curBitmap.PixelFormat);

            IntPtr ptr = bmpData.Scan0;

            int bytes = width * height * 3;

            byte[] rgbValues = new byte[bytes];

            System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0,bytes);

            double colorTemp;
            for (int i = 0; i < rgbValues.Length; i += 3)
            {
                colorTemp = rgbValues[i + 2] * 0.299 + rgbValues[i + 1] * 0.587 + rgbValues[i] * 0.114;

                rgbValues[i] = rgbValues[i + 1] = rgbValues[i + 2] = (byte)colorTemp;
            }
            System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes );
            curBitmap.UnlockBits(bmpData);
            Invalidate();
            
        }

5分
百度一下图像二值化 例子很多
引用 1 楼 sj490790083 的回复:

百度一下图像二值化 例子很多

这个是转成灰度图像,还不是二值,就是在网上看了多好,但是没感觉跟我这个有什么差别,不过别人的为什么能整幅图像都变成黑白呢,而且我感觉我这个并不是真正的灰度图片,貌似中间还有红色什么,求解啊!

图像有alpha通道
30分
所以1个像素应该是4bytes,你只用了3bytes,所以只有3/4的图像被替换了
5分
我在其他帖子的回复


            Bitmap bmp = new Bitmap(100, 100);
            using (Graphics g = Graphics.FromImage(bmp))
            {
                g.FillEllipse(Brushes.Red, new Rectangle(0, 0, 80, 80));
                g.FillRectangle(new SolidBrush(Color.FromArgb(128, Color.Green)), new Rectangle(20, 20, 80, 80));
            }
            e.Graphics.DrawImage(bmp, new Point(10, 10));
 
            //简单的
            ControlPaint.DrawImageDisabled(e.Graphics, bmp, 120, 10, Color.White);
 
            //用颜色矩阵
            System.Drawing.Imaging.ImageAttributes attr = new System.Drawing.Imaging.ImageAttributes();
            float[][] colorMatrixElements = { 
               new float[] {.33f,  .33f,  .33f,  0, 0},        // r = (r+g+b)/3
               new float[] {.33f,  .33f,  .33f,  0, 0},        // g = (r+g+b)/3
               new float[] {.33f,  .33f,  .33f,  0, 0},        // b = (r+g+b)/3
               new float[] {0,  0,  0,  1, 0},        // alpha scaling factor of 1
               new float[] {0,  0,  0,  0, 1}};    // 
            System.Drawing.Imaging.ColorMatrix matrix = new System.Drawing.Imaging.ColorMatrix(colorMatrixElements);
            attr.SetColorMatrix(matrix);
            e.Graphics.DrawImage(bmp, new Rectangle(10, 120, 100, 100), 0, 0, 100, 100, GraphicsUnit.Pixel, attr);


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明彩色图像得到灰色图像求指教
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!