Ask Your Question

Revision history [back]

Need help with comparing histogram

I want to detect fire through camera captures and so far i have done background subtraction to captured frame, and able to obtain histogram output of the frame as well (output window very small). So, now how should i compare a reference fire/flame histogram to the captured flame? Any tips would be great.

Code:

Mat src, hsv;

bool update_bg_model = true;
Mat background, foreground;
std::vector<std::vector<cv::Point> >contours; 
CvCapture * capture;
capture = cvCaptureFromCAM(1);

string windowName = "Flamespotter Capture";

string windowName2 = "Histogram of the Capture";

string windowName3 = "Foreground Capture";

if(capture)
{
        for(;;)
        {
            //store a frame into the Mat
        src = cvQueryFrame(capture); 
            if(!src.empty())
            {
            //background subtraction
            back_obj.operator()(src,foreground);
            //obtain foreground
            back_obj.getBackgroundImage(background);

            erode(foreground,foreground,Mat());         
            dilate(foreground,foreground,Mat());     

            findContours(foreground,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);      
            drawContours(src,contours,-1,Scalar(255,255,0),4);     

            //convert to hsv
            cvtColor(src, hsv, CV_BGR2HSV);
            int hbins = 30, sbins = 32;
            int histSize[] = {hbins, sbins};
            // hue varies from 0 to 179, see cvtColor
            float hranges[] = { 0, 180 };
            // saturation varies from 0 (black-gray-white) to
            // 255 (pure spectrum color)
            float sranges[] = { 0, 256 };
            const float* ranges[] = { hranges, sranges };
            MatND hist;
            // we compute the histogram from the 0-th and 1-st channels
            int channels[] = {0, 1};

            calcHist( &hsv, 1, channels, Mat(), // do not use mask
                     hist, 2, histSize, ranges,
                     true, // the histogram is uniform
                     false );
            double maxVal=0;
            minMaxLoc(hist, 0, &maxVal, 0, 0);

            int scale = 10;
            Mat histImg = Mat::zeros(sbins*scale, hbins*10, CV_8UC3);

            for( int h = 0; h < hbins; h++ )
                for( int s = 0; s < sbins; s++ )
                {
                    float binVal = hist.at<float>(h, s);
                    int intensity = cvRound(binVal*255/maxVal);
                    rectangle( histImg, Point(h*scale, s*scale),
                                Point( (h+1)*scale - 1, (s+1)*scale - 1),
                                Scalar::all(intensity),
                                CV_FILLED );
                }
            //show the original frame
            imshow( windowName, src );
            //show histogram of the histogram frame
            imshow( windowName2, histImg );

            imshow (windowName3, foreground);
            }
            else
            {
                printf("--Error-- No captured frame!");
                break;
            }
            int c = waitKey(10);
            if((char)c=='c'){break;}
        }
 }
 return 0;