Different results with Haar cascade when in release/debug mode
Asked Answered
D

2

13

I am using a Haar cascade classifier trained from the MIT cars dataset to detect vehicles in OpenCV (trained using the utilities provided with OpenCV). This works reasonably well when compiled in Debug mode, but when compiled in Release mode the cascade doesn't make any detections at all. Running the following code on the test image below gives a detection in debug mode but nothing in release mode (this behaviour continues through all images in my data sequence).

Can you suggest why this occurs and, more importantly, what I can do to obtain detections when running in Release mode?

Cascade File

Code

cv::Mat testImage = cv::imread("testImage.png",0);
cv::equalizeHist(testImage, testImage);

cv::CascadeClassifier vehicleCascade;
vehicleCascade.load("cars3.xml");

// Detect vehicles
std::vector<cv::Rect> cars;
vehicleCascade.detectMultiScale(
    testImage,                  // Input image
    cars,                   // Output bounding boxes
    1.1,                    // scale factor - how much image size is reduced at each scale
    5,                      // min neighbours - how many neighbours required to maintain rect
    0|CV_HAAR_SCALE_IMAGE,  // Not used
    cv::Size(30,30),        // Min poss object size
    cv::Size()              // Max poss object size
    );

std::cout << "Found " << cars.size() << " objects.\n";

for (int i=0; i<cars.size(); ++i)
    cv::rectangle(testImage, cars.at(i), CV_RGB(255,0,0), 3);

cv::namedWindow("Haar cascade");
cv::imshow("Haar cascade", testImage);
cv::waitKey(0);
cv::imwrite("output.png", testImage);

TestImage

testImage

Downward answered 20/12, 2012 at 11:48 Comment(4)
The image and cascade files load fine, in my actual code I use full directory paths. As far as I can tell, there are no errors in either mode, the only difference being the number of objects detectMultiScale returns.Downward
Ok :/ I deleted my first comment since the part about the png was not relevant.Hamil
This is often a sign of having UB and relying on specific behaviour (either in your code or in the library) or relying on specifically generated code that is different (e.g. floating point accuracy due to stuff staying in registers in release mode). The best thing you can do here is to compare all involved computations step by step in both modes and figure out where they start to deviateColver
Agree with PlasmaHH: very likely UB is to blame. You might have e.g. a fencepost error that happens to often produce certain behaviour in the debug version and then different behaviour in the release version because the allocations end up being in a different order. As a quick and cheap test, have you tried running the program in valgrind?Pathos
U
3
  1. It's strange, but for me you code is working fine in both modes. I'm using Visual Studio 2010 on Windows 7 32bit. Here is my project - https://www.dropbox.com/s/5kubn5tlu7k6ziy/opencvhw.rar, so you can check executables(Release and Debug directories). If you are using visual studio and want to build it on your own you will have to change paths to library and include directories for both modes(include directories path is the same, library directories path is different). (project -> ... properties -> configuration properties -> vc++ directories)

  2. Generally i would recommend to check really carefully project configuration. It's really easy to make small mistake which can cause very weird behavior. Sometimes the best option is to configure everything from the scratch once again.

  3. OpenCV have very strange bugs - it's nothing new :) For example i can't use some codecs while debugging - if i run program in debug mode, but without debugging everything is fine, but if i try to debug - each frame readed from file is empty(but its size is correct). It's possible that you have just encountered something a bit similar. Try to convert image to different format(i think that bmp is the best choice for test - it's should always work without any additional libraries).

  4. Also note that you have uploaded jpg file so i had to rename and convert it on my own - maybe during conversions something in this file have changed so we are not testing your code on exactly the same files - upload you png file on dropbox so i can test it.

Ulyanovsk answered 12/1, 2013 at 4:34 Comment(1)
Thanks for looking into this for me. Unfortunately I don't have access to dropbox at the moment so can't test your project or upload more files, but I can confirm that I definitely still have the same problem. I use CMake to generate the project so maybe there's an option in there that I'm missing. I've omitted the Haar cascade from my project at the moment and will return to investigate this more at a later date.Downward
Y
2

See this post: This type of error could be caused by linking to the debug library of opencv in release mode.

Yaelyager answered 20/12, 2012 at 12:2 Comment(1)
I've read that, but I'm not having any problems with loading the xml file, or running the code, it's just the output which is different. Also, the correct OpenCV dlls are being loaded - debug for debug, release for release.Downward

© 2022 - 2024 — McMap. All rights reserved.