当前位置:编程学习 > C#/ASP.NET >>

关于用c#写灰度直方图的问题

 Rectangle rect = new Rectangle(0, 0, curBitmap.Width, curBitmap.Height);
                    System.Drawing.Imaging.BitmapData bmpData = curBitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, curBitmap.PixelFormat);
                    IntPtr ptr = bmpData.Scan0;
                    int bytes = curBitmap.Width * curBitmap.Height;
                    byte[] grayValues = new byte[bytes];
                    System.Runtime.InteropServices.Marshal.Copy(ptr, grayValues, 0, bytes);
                    int temp = 0;
                    double a = Convert.ToDouble(linearForm.GetScaling);
                    double b = Convert.ToDouble(linearForm.GetOffset);
                    for (int i = 0; i < bytes; i++)
                    {
                        temp = (int)(a * grayValues[i] + b + 0.5);

                        if (temp > 255)
                        {
                            grayValues[i] = 255;
                        }
                        else if (temp < 0)
                        {
                            grayValues[i] = 0;
                        }
                        else
                            grayValues[i] = (byte)temp;
                    }
                    System.Runtime.InteropServices.Marshal.Copy(grayValues, 0, ptr, bytes);
                    curBitmap.UnlockBits(bmpData);
上面这段代码是选自《c#图像处理典型算法实例》中的灰度直方图中的代码,这是灰度直方图求灰度数据的部分。
看到这段代码有些地方不太明白,“curBitmap.PixelFormat”中PixelFormat的24位和32位的有什么区别,在指针地址上是否不同?
“for (int i = 0; i < bytes; i++)
                    {
                        temp = (int)(a * grayValues[i] + b + 0.5);

                        if (temp > 255)
                        {
                            grayValues[i] = 255;
                        }
                        else if (temp < 0)
                        {
                            grayValues[i] = 0;
                        }
                        else
                            grayValues[i] = (byte)temp;
                    }
”这一段是获取图像灰度的,据我所知图像区的数据不是以BGRBGRBGR形式存放的吗,文中直接用grayValues[i]获取数据对吗?grayValues[i]中是单纯的灰度数据吗?不是BGRBGR数据吗?
究竟用c#写灰度直方图怎么写?以及curBitmap.PixelFormat的不同 --------------------编程问答-------------------- 你这样写到何年何月,用opencv好了,还是开源的,一个函数搞定 --------------------编程问答-------------------- 必须的,做图象不用这种库,是很难出成果的。
引用 1 楼 yunhaic 的回复:
你这样写到何年何月,用opencv好了,还是开源的,一个函数搞定
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,