位图中的颜色比例 [英] Colour percentage in Bitmap

查看:108
本文介绍了位图中的颜色比例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

开始不知道有关的位图的**

 来获得高度位图总像素*宽
要获得总白色像素其中R == 255安培;乙== 255安培;摹= = 255
为了让黑总像素其中R == 0安培;乙== 0安培;摹== 0
要获得总灰度像素,其中R = G = B

他们中的其余部分将是一个混合的颜色应该给我。显然,该计划将为数千次运行,所以我需要使用Lockbits。

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


我如何找到位图彩色像素百分比,原来位图对象为PDF页面。我试着用bitmap.getpixel()正在采取的年龄,LockBits是更好的性能,想用Lockbits发现彩色像素数,不包括百分比要知道黑色,白色和灰色。这是确定的PDF文件,并​​使用颜色的彩页打印特定页面。

我刚刚得到了code检测黑色和白色的像素数,我只是想利用这code的只是找到总像素检测的百分比,然后区别应该给我的色彩像素,不知道这是正确的做法还是不!!

 公共无效ColourPercentage(位图页面,楼盘INT nBlackCount,楼盘INT nWhiteCount)
    {
        System.Drawing.Image对象图像= NULL;
        位图bmpCrop = NULL;        的BitmapData bmpData = NULL;
        字节[] imgData = NULL;
        INT N = 0;
        尝试
        {
            图像=页;
            bmpCrop =新位图(图片);
            对于(INT H = 0; H< bmpCrop.Height; H ++)
            {                bmpData = bmpCrop.LockBits(新System.Drawing.Rectangle(0,h时,bmpCrop.Width,1),
                System.Drawing.Imaging.ImageLockMode.ReadOnly,image.PixelFormat);
                imgData =新的字节[bmpData.Stride]
                System.Runtime.InteropServices.Marshal.Copy(bmpData.Scan0,imgData,0
                ,imgData.Length);
                bmpCrop.UnlockBits(bmpData);                为(n = 0时; N&下; = imgData.Length - 3; N + = 3)
                {
                    如果((int)的imgData [n]的== 000放大器;及(INT)imgData [N + 1] == 0&放大器;及(INT)imgData [N + 2] == 000)// R = 0时G = 0 B = 0重新presents黑
                    {
                        nBlackCount ++;
                    }
                    否则如果((int)的imgData [n]的== 255放大器;及(INT)imgData [N + 1] == 255放大器;及(INT)imgData [N + 2] == 255)// R = 255,G = 255 B = 255重新presents白
                    {
                        nWhiteCount ++;
                    }
                    否则如果((int)的imgData [n]的==(int)的imgData [N + 1]放大器;及(INT)imgData [N + 1] ==(int)的imgData [N + 2])
                        nBlackCount ++;
                }            }        }
        赶上(异常前)
        {
            System.Windows.MessageBox.Show(ex.Message);
        }
    }


 公共无效blackwhiteCount(位图页面,楼盘INT nBlackCount,楼盘INT nWhiteCount)
    {
        的System.Drawing.Color像素;
        尝试
        {
            的for(int i = 0; I< page.Height;我++)
            {
                对于(INT J = 0; J< page.Width; J ++)
                {
                    像素= page.GetPixel(I,J);
                    如果(pixel.R == 0安培;&安培; pixel.G == 0安培;&安培; pixel.B == 0)
                        nBlackCount ++;
                    否则,如果(pixel.R == 255安培;&安培; pixel.G == 255安培;&安培; pixel.B == 255)
                        nWhiteCount ++;
                }
            }
        }
        赶上(异常前)
        {
            System.Windows.MessageBox.Show(无法解析图像+ EX);
        }
    }


  Col​​ourPercentage(页,楼盘nblack,裁判nwhite);
                             双n总= page.Width * page.Height;
                            串blackper,whiteper,colourper;
                            双黑=(双)nblack * 100 / n总;
                            双白=(双)nwhite * 100 / n总;
                            双色= 100 - (黑+白);


解决方案

我发现自己,为观众方便的使用重新补充修正(问题与已回答更新)。

要获得总像素使用的图像尺寸,而不是bitlockdata的循环像素的个数。

 图片页面=新的图像(文件路径);
双n总= page.Width * page.Height;

Started without knowing about bitmap**

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

Rest of them will be a mixed colours which should give me. Obviously the program will run for thousands of times,so I need to use Lockbits.

current problem is inaccurate result. pls suggest. Trying to use aforge.net or imagemagick.net libraries to check whether it can give accurate results


How do I find the color pixels percentage in bitmap, originally the bitmap object is from PDF page. I tried with bitmap.getpixel() it is taking ages, LockBits is better in performance, Would like to know using Lockbits finding the percentage of color pixels excluding black, white and grey. This is to identify the colour pages in PDF file and colour usage for printing specific page.

I have just got a code to detect the count of black and white pixels, I am just trying to make use of this code to detect percentage just by finding the total pixels and then the difference should give me colour pixels,not sure it is correct approach or not!!

 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);

解决方案

I have found myself, for viewers convenience for re usage, adding the correction(Question is updated with the answer already).

To get total pixels use image dimensions, instead of counting the looping pixels in bitlockdata.

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

这篇关于位图中的颜色比例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆