The percentage of color in the bitmap

Starts without knowing about the 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 

The rest of them will be mixed colors that should give me. Obviously, the program will run thousands of times, so I need to use Lockbits.

the current problem is an inaccurate result. pls offer. Trying to use the aforge.net or imagemagick.net libraries to check if they can give accurate results.


How to find the percentage of pixel pixels in a bitmap, initially the bitmap object is on a PDF page. I tried with bitmap.getpixel (), it takes age, LockBits are better in performance, I would like to know, using Lockbits, finding the percentage of color pixels, excluding black, white and gray. This is necessary to define color pages in a PDF file and use color to print a specific page.

I just got a code to determine the number of black and white pixels, I'm just trying to use this code to determine the percentage, just by finding the common pixels, and then the difference should give me color pixels, not sure if this is the right 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); 
0
source share
1 answer

I found myself for ease of viewing for reuse by adding a fix (the question has already been updated with the answer).

To get common pixels, use image measurements, rather than counting the pixels in the loop in bitlocks.

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

All Articles