I need to count the black and white pixels of the object in REAL TIME. What is the best method to use for this ? In my program, First I get the biggest contour and used the " &= " to combine the contour drawing and the frame. The next process is to count the pixels(black and white) of the object that have no mask but I don't know how to do it. Thanks in advance.
#include "opencv\cvaux.h"
#include "opencv\cxmisc.h"
#include "opencv\highgui.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdlib.h>
using namespace std;
using namespace cv;
int main(int, char**)
{
Mat threshold_output;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
RNG rng(12345);
CvCapture* capture = cvCaptureFromCAM(0);
cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, 270);
cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, 190);
cv::Mat frame; cv::Mat src_gray;
while(true) {
frame = cvQueryFrame( capture );
cvtColor( frame,src_gray, CV_BGR2GRAY );
blur( src_gray, src_gray, Size(11,11) );
threshold( src_gray, threshold_output, 100, 200, CV_THRESH_BINARY);
findContours( threshold_output, contours, hierarchy,CV_RETR_EXTERNAL,
CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
vector<RotatedRect> minRect( contours.size() );
Mat drawing = Mat::zeros( threshold_output.size(), CV_8UC3 );
for (int i = 0; i < contours.size(); i++)
{
double area =contourArea(contours[i]);
Rect rect = boundingRect(contours[i]);
if (area >= 7200 && abs(1 - ((double)rect.width / (double)rect.height)) <=
0.1)
{
printf("ContourArea = %.0f\n\n",
((contourArea(contours[i]))+( arcLength( contours[i], true )/2)));
drawContours( drawing, contours, i, Scalar::all(255), -1);
}
}
drawing &= frame;
imshow( "Contours", drawing );
imshow( "frame", frame );
cvWaitKey(33);
}
return 0;
}