【问题标题】:Colour percentage in Bitmap位图中的颜色百分比
【发布时间】:2013-09-16 09:54:06
【问题描述】:

在不了解位图的情况下开始**

To get total pixels in bitmap  height*Width 
To get total white pixels Where R==255 & B==255 & G==255
To get total black pixels Where R==0 & B==0 & G==0
To get total grey pixels where R=G=B

其余的将是应该给我的混合颜色。显然程序会运行数千次,所以我需要使用 Lockbits。

当前的问题是结果不准确。请建议。 尝试使用 aforge.net 或 imagemagick.net 库来检查它是否可以给出准确的结果


如何找到位图中的颜色像素百分比,最初位图对象来自 PDF 页面。我尝试使用 bitmap.getpixel() 它需要很长时间,LockBits 的性能更好,想知道使用 Lockbits 找到不包括黑色、白色和灰色的彩色像素的百分比。这是为了识别 PDF 文件中的彩色页面和打印特定页面的颜色使用情况。

我刚刚得到一个代码来检测黑白像素的数量,我只是想利用这个代码来检测百分比只是通过找到总像素然后差异应该给我彩色像素,不确定方法对不对!!

 public void ColourPercentage(Bitmap page, ref int nBlackCount, ref int nWhiteCount)
    {
        System.Drawing.Image image = null;
        Bitmap bmpCrop = null;

        BitmapData bmpData = null;
        byte[] imgData = null;
        int n = 0;
        try
        {
            image = page;
            bmpCrop = new Bitmap(image);
            for (int h = 0; h < bmpCrop.Height; h++)
            {

                bmpData = bmpCrop.LockBits(new System.Drawing.Rectangle(0, h, bmpCrop.Width, 1),
                System.Drawing.Imaging.ImageLockMode.ReadOnly, image.PixelFormat);
                imgData = new byte[bmpData.Stride];
                System.Runtime.InteropServices.Marshal.Copy(bmpData.Scan0, imgData, 0
                , imgData.Length);
                bmpCrop.UnlockBits(bmpData);

                for (n = 0; n <= imgData.Length - 3; n += 3)
                {
                    if ((int)imgData[n] == 000 && (int)imgData[n + 1] == 0 && (int)imgData[n + 2] == 000)// R=0 G=0 B=0 represents Black
                    {
                        nBlackCount++;
                    }
                    else if ((int)imgData[n] == 255 && (int)imgData[n + 1] == 255 && (int)imgData[n + 2] == 255) //R=255 G=255 B=255 represents White
                    {
                        nWhiteCount++;
                    }
                    else if ((int)imgData[n] == (int)imgData[n + 1] && (int)imgData[n + 1] == (int)imgData[n + 2])
                        nBlackCount++;
                }

            }

        }
        catch (Exception ex)
        {
            System.Windows.MessageBox.Show(ex.Message);
        }     
    }

  public void blackwhiteCount(Bitmap page, ref int nBlackCount, ref int nWhiteCount)
    {
        System.Drawing.Color pixel;
        try
        {
            for (int i = 0; i < page.Height; i++)
            {
                for (int j = 0; j < page.Width; j++)
                {
                    pixel = page.GetPixel(i, j);
                    if (pixel.R == 0 && pixel.G == 0 && pixel.B == 0)
                        nBlackCount++;
                    else if (pixel.R == 255 && pixel.G == 255 && pixel.B == 255)
                        nWhiteCount++;
                }
            }
        }
        catch (Exception ex)
        {
            System.Windows.MessageBox.Show("Unable to parse image " + ex);
        }
    }

ColourPercentage(page, ref nblack, ref nwhite);
                             double nTotal = page.Width * page.Height;
                            string blackper, whiteper, colourper;
                            double black =(double) nblack*100 / nTotal;
                            double white =(double) nwhite *100 / nTotal;
                            double colour = 100 - (black + white);

【问题讨论】:

  • for 循环应该是for (n = 0; n &lt;= imgData.Length - 3; n += 3)。否则,您会跳过最后一个像素。结果如何不准确?这种方法看起来不错。
  • 当我用空白白页/全黑页测试时,结果也不正确,预计只有黑页中的黑数和空白页中的白数(页面转换为位图,使用MSWord创建文件并保存到 PDF)..!!虽然很好地抓住了循环
  • 你检查过实际值吗?不准确可能是由 JPEG 转换引起的,对此您无能为力。除了引入阈值。
  • 它不是Jpeg,它来自流,我不是物理存储,逻辑上验证位图的颜色,我也通过保存验证图像,看起来很好。有没有其他方法我可以找颜色吗?
  • 我发现自己在计算总像素时确实犯了一个错误,现在我得到了接近的结果。概念错误但在代码中以不同的方式进行,更正和更新了代码。

标签: c# wpf image bitmap


【解决方案1】:

我自己发现了,为了方便观众重新使用,添加更正(问题已经更新了答案)。

要获得总像素,请使用图像尺寸,而不是计算 bitlockdata 中的循环像素。

Image page= new Image(filePath);
double nTotal = page.Width * page.Height;

【讨论】:

    猜你喜欢
    • 2018-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-27
    • 2014-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多