detection works with video but fails to detect with static images

asked 2018-01-28 10:48:01 -0500

gunner gravatar image

Hello everybody. I made a project with OpenCV in c++ using eclipse to detect fire extinguisher. I took 1000 images with camera and while applying detection on video, it works great. But right now i want to do detection on static images to calculate accuracy of detection. Problem is when I apply detection on static images, it doesn't work (red bounding rectangle is not shown on images). Here's the code I use (just picked out the relevant lines):

int main() {

cvNamedWindow("fire_extinguisher detecting camera", 1);
// Capture images from any camera connected to the system

CvCapture* capture = cvCaptureFromAVI("/home/painkiller/Desktop/videos_krstionica/1.mp4");
// Load the trained model
CascadeClassifier fire_extinguisherDetector;
fire_extinguisherDetector.load("src/krstionica.xml");

if (fire_extinguisherDetector.empty()) {
    printf("Empty model.");
    return 0;
}

char key;

while (true) {

    for(int i=0; i < 1000; i++)
    {

    Mat frame;

    String result;
    String beginning;
    String end;

    beginning = "/home/painkiller/Desktop/dada/";
    end = ".jpg";

    std::stringstream sstm;

    sstm << beginning << i << end;
    result = sstm.str();

    frame = imread(result);          // THIS IS USED NOW FOR IMAGE DETECTION

    //   frame = cvQueryFrame(capture);    // THIS WAS USED FOR VIDEO DETECTION

    stringstream x,y;

    std::vector<Rect> fire_extinguishers;

    fire_extinguisherDetector.detectMultiScale(frame, fire_extinguishers, 1.10,1,
                    0 | CV_HAAR_SCALE_IMAGE, Size(50, 450));

    for (int i = 0; i < (int) fire_extinguishers.size(); i++) {
        Point pt1(fire_extinguishers[i].x, fire_extinguishers[i].y);
        Point pt2(fire_extinguishers[i].x + fire_extinguishers[i].width,
                fire_extinguishers[i].y + fire_extinguishers[i].height);

        // Draw a rectangle around the detected object
        rectangle(frame, pt1, pt2, Scalar(0, 0, 255), 2);   
    }

    // Show the transformed frame
    if (!frame.empty())
    imshow("fire_extinguisher detecting camera", frame);
    waitKey(3000);

    // Read keystrokes, exit after ESC pressed
    key = cvWaitKey(10);
    if (char(key) == 27) {
        break;
    }
}

return 0;

} }

I also tried with cvCaptureFromFile:

CvCapture* capture = cvCaptureFromFile("/home/painkiller/Desktop/dada/1.jpg");

frame = cvQueryFrame(capture);

But that didn't work either...

edit retag flag offensive close merge delete

Comments

  • please do not use outdated c-api calls like cvCaptureFromFile, cvCaptureFromAVI, cvWaitKey, but their modern c++ equivalents, like cv::VideoCapture, which also has means of reading a static image sequence like: VideoCapture cap("/home/painkiller/Desktop/dada/%d.jpg"); (this would also save you from making a stringstream mess)
  • you're specifiying a minimum size in your detectMultiScale() call here: Size(50, 450). can it simply be, that your images don't have enough resolution to find it ?
berak gravatar imageberak ( 2018-01-28 11:14:17 -0500 )edit

Yes sir, you are right! So fast and so smart answer. Thank you very much for your help, I really appreciate it :D

gunner gravatar imagegunner ( 2018-01-28 11:28:48 -0500 )edit

All my images are 400x300. So I specify Size(10, 100) and now it is working ok. Not really sure why this size works and Size(10,200) doesn't so i will keep using the first one.

gunner gravatar imagegunner ( 2018-01-28 11:38:30 -0500 )edit

so, you only got 300 pixels in y direction, and your minSize has 450

berak gravatar imageberak ( 2018-01-28 11:42:42 -0500 )edit