I want to find the non-white area of an image from a camera using OpenCV. I can already find circles using images from my web cam. I want to make a grid or something so I can determine the percent of the image is not white. Any ideas?
+2
A:
If you want to find the percentage of pixels in your image which is not white, why don't you just count all the pixels which 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()
{
// Acquire the image (I'm reading it from a file);
IplImage* img = cvLoadImage("image.bmp",1);
int i,j,k;
// Variables to store image properties
int height,width,step,channels;
uchar *data;
// Variables to store the number of white pixels and a flag
int WhiteCount,bWhite;
// Acquire image unfo
height = img->height;
width = img->width;
step = img->widthStep;
channels = img->nChannels;
data = (uchar *)img->imageData;
// Begin
WhiteCount = 0;
for(i=0;i<height;i++)
{
for(j=0;j<width;j++)
{ // Go through each channel of the image (R,G, and B) to see if it's equal to 255
bWhite = 0;
for(k=0;k<channels;k++)
{ // This checks if the pixel's kth channel is 255 - it can be faster.
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
2009-10-23 18:13:22
What would be a method of doing that?
kman99
2009-10-23 20:50:43
Are you using OpenCV2.0 (i.e. the `Mat` class) or an older version (`IplImage*`,`CvArray*`,..)?
Jacob
2009-10-23 21:03:51
The old IplImage* using 1.1
kman99
2009-10-24 13:50:03
Alright, the sample code in C is up
Jacob
2009-10-24 14:27:22