If you want to find the percentage of pixels in an image that is not white, why don't you just count all the pixels that are not white and divide it by the total number of pixels in the image?
Code in C
#include <stdio.h>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
int main()
{
IplImage* img = cvLoadImage("image.bmp",1);
int i,j,k;
int height,width,step,channels;
uchar *data;
int WhiteCount,bWhite;
height = img->height;
width = img->width;
step = img->widthStep;
channels = img->nChannels;
data = (uchar *)img->imageData;
WhiteCount = 0;
for(i=0;i<height;i++)
{
for(j=0;j<width;j++)
{
bWhite = 0;
for(k=0;k<channels;k++)
{
if (data[i*step+j*channels+k]==255) bWhite = 1;
else
{
bWhite = 0;
break;
}
}
if(bWhite == 1) WhiteCount++;
}
}
printf("Percentage: %f%%",100.0*WhiteCount/(height*width));
return 0;
}
Jacob source
share