OpenCV LogoOf all the emails I receive thru this website, the question I am asked the most is “how do I teach OpenCV to detect my own objects?” Although the questions are usually worded very differently, they’re all pretty much asking the same thing. “How do I create my own OpenCV Haar Classifiers?” Even though I’ll be explaining this process in much greater detail in my upcoming book, Computer Vision with OpenCV and C#, I’ve decided to go ahead and share a much slimmer explanation here. I’ll do my best to explain things the best I can in the shortest amount of time as possible. But, as always, if you have any questions after reading this article, please leave them in the comments below and I’ll answer you as soon as possible. With that said, let’s begin.

Creating your own Haar classifiers is pretty much a 3 step process. The process begins by gathering material that will be fed to OpenCV which it will be trained to detect. Training material consists of 3 groups of sub-material. The first sub-material you will need is your positive set. Your positive set is basically a collection of images that contain the object you want OpenCV to detect. In this article, we will be teaching OpenCV to detect Blackberry cellphones within images and video. So, our positive set will contain a collection of images that all have a Blackberry in them. The second sub-material you will need will be your negative set. The negative set is basically a collection of images that do not contain the object you want detected. So, for the purposes of this article, we will need a collection of images that do not include a Blackberry. The third and final sub-material you will need is another set of images that do contain the object you want to detect. This set of images will be used for testing our classifier later on.

For training a Haar classifier, it is recommended to use as many training samples as possible. However, I’ll warn you now that the training process takes a long time and the more training samples you use, the longer it will take. In order to create the Haar classifier for this article, it took 5 days of non-stop processing on one of my extra laptops (2.8GHz, 4GB RAM, Windows Vista). My training set consisted of 2000 positives, 2000 negatives, and 500 tests. Let me explain how I created those image sets.

To create your positives, you will need as many pictures of your object as possible, Blackberry cellphones in my case. Now, you can grab a camera and start snapping pictures of your object from several different angles and in different lighting environments. But, this can take forever, especially when you’re considering thousands of images. Instead, I found that it was easier to record a couple of videos of my object from different angles and using different lighting. Then, all I had to do was extract each image from the videos one frame at a time and crop them accordingly.

To do that, I made use of a tool called “Positive Builder” which is a simple Windows application written in C# that can be downloaded from https://code.google.com/p/opencv-haar-cascade-positive-image-builder/. Even though it’s called “Positive Builder”, it’s also capable of creating our negatives which we’ll do shortly. As mentioned, Positive Builder is written in C#, Visual C# 2005 to be exact. I’m not sure if there is a newer version available somewhere else. But, the version I got from the address above seemed to be incomplete. So, I have updated the project to work with Visual C# 2010 and have made a lot of adjustments to it. I have added a lot of the missing pieces and have finished up some parts that were lacking. With my new version, you should have everything you need to make full use of this tool. You can download my updated version of the app from http://www.prodigyproductionsllc.com/downloads/opencv_haar_cascade_postitive_builder.zip. (Update: The 64bit version can be downloaded at the end of this article.) On a side note, Positive Builder uses OpenCvSharp for the heavy lifting which I especially like this since OpenCvSharp is the OpenCV wrapper I prefer to work with in C#.

Once you have the app loaded in Visual C#, you will need to open App.config from the Solution Explorer. This file contains the path and filename of the video you want to get your frames from. It also contains the folder path that you want your images output to as well as a string to prefix your image names with and a number to begin your file name incrementing from. This number is helpful if you’ve already processed several images, closed and / or restarted the app, and want to pick up from where you left off. After you have setup your path variables, go ahead and launch the application.

When you do, you should see the first frame of your video in the form. If so, you will need to specify where your object is located within the frame. To do that, click once in the upper-left corner just above where your object is located and click a second time in the lower-right corner just outside of where your object is located. This will draw a green box that should outline only your object. If so, press the “P” key 1 time to save that image into your positives folder and write its dimensions into positives.txt of your output folder. After the frame has been saved to your positives folder, the app will automagically jump to the next frame in the video if there is one. Once you reach the last frame of your video, the app will no longer skip to the next frame since there are no more. If there are more frames, the app will automagically jump to the next frame where you will need to repeat the process until you have cropped all of your positive images.

OpenCV Haar Cascade Trainer - Positive Image Builder and Cropper

The Positive Builder tool also includes other functions which are defined on the form. For instance, if you come across a frame that does not include your object or the object is obstructed too much by something else, you can press the “Q” key to query the next frame in the video. If you draw your box and aren’t happy with it, you can press the “R” key to reset your lines and start over with the current frame. If you come across a frame that does not include your object, you can press the “N” key to flag it as a negative image. Remember, negative images are images that do not contain the object you want to detect.

To quickly create negative images, you can load up a video that you know does not contain your object and keep pressing the “N” key until all frames have been processed as negatives. You can also press the “2″ key to process every frame in a video as negatives. Then, if any of your frames did in fact contain your object, you can browse to your negatives folder and manually delete the images that did contain your object. You can also do the same for positives by pressing the “1″ key to process every frame as a positive frame that does include your object. However, I wouldn’t recommend doing this because your object won’t be cropped and will return the width and height of the video as the cropped dimensions. So, be cautious when using the “All Positives” option.

When cropping and processing your positive images, each image will be stored in your positives folder. And, all positive images get recorded in a file called “positives.txt”. This file contains the full path and filename of each positive image as well as a few other parameters. The first parameter after the image name is an indicator that tells OpenCV how many instances of your object can be found within that image. The rest of the parameters are the coordinates of your object within the image (left, top, width, height). The “negatives.txt” file only contains the full path and file names of your negative image set.

Now that we have all of our negative and positive images, it’s time to create our .vec file. To do this, we will make use of our “createsamples.exe” file that comes with OpenCV. You can either copy the exe file into the same folder that contains your positives.txt and negatives.txt files or you can reference the executables by fully qualifying the path to where createsamples.exe lives (probably inside C:\Program files\OpenCV\bin\). Or, you can always use my version of Positive Builder by pressing the “V” button which will dump your .vec file into the same folder as your positives.txt and negatives.txt files (output folder).

Once you have your .vec file, it’s just a matter of Haar training. All you have to do to build your cascade at this point is to press the “T” key and the “haarTraining.exe” application will take care of the rest. This is the final step, yet it is also the longest. This is the step that took 5 days to run for me. I’ve read several articles about other OpenCV users getting the same results. So, I know it’s the application and not just a fluke. Anyways, when the Haar training has completed, you will have yourself a brand new haarcascade.xml file in your output folder. You can now test out your shiny new cascade by loading it up in any of the OpenCV head, face, or eye detection and tracking applications I’ve already shared with you on this website.

P.S. I’ve already begun updating the Positive Builder application. My next version will include a built-in video recorder which you can use for stripping frames out of for your positive and negative images. The next version will also include a built-in testing tool that you can use to auto-load your new cascade into for testing. I’ll also be doing a lot of cleanup to make the application easier to use and will be providing a way to pick your output folder, images, and other parameters without having to change the app.config file. Until then, this should hopefully help you get the job done. Let me know what you think about this article in the comments below. Let me know if you found it helpful or not. Also, feel free to leave your suggestions and questions in the comments below as well. Until next time, HAPPY CODING!

Update: Here is the updated 64bit version of the Positive Builder. It is also using the latest versions of OpenCV and OpenCvSharp (2.3.1). http://www.prodigyproductionsllc.com/downloads/opencv_positive_builder_64bit.zip

Related Posts

Tagged with:  

65 Responses to How to Train OpenCV Haar Classifiers

  1. [...] couple of days ago, I wrote an article showing you how to train your own Haar classifiers for use with OpenCV. Since I posted that article, I have received several emails asking what a Haar classifier is and [...]

    • djinty says:

      Hey I got Windows 7 x64 and Visual Studio 2010. I downloaded different versions of OpenCvSharp (v2.0×86, v2.2×86 and v2.2×64). I tried to delete the references for Opencvsharp and add the new paths, delete the debug folder and re-compile.

      I changed the App.Config File: Added the Path to opencvsharp, modified an Video-Path to an avi video and selected the output-Path.

      I always geht the same error like azurerhythm. And I also downloaded an tried the Version LuCaS posted in response to his or her problem.

      So does anyone know how to get this thing running, and how can I get captures from my usb webcam? Where can I specify that?

      Thanks in Advance, greetings

      • LuCuS says:

        I’ve had problems getting the newest versions of OpenCvSharp to work correctly. When time permits, I’ll take a look at getting the new stuff to work and will be writing some new articles once I get that working. I haven’t been messing with OpenCvSharp much lately. What little time I have for computer vision, I’ve been messing with OpenCV for Android. I’ll be writing some articles about that experience in the near future too.

        • djinty says:

          So you think there is no chance to get the Application running on my system? Do you have any suggestions which platform I could setup to get the Application running?

          Thank you in Advance

          • LuCuS says:

            It’s possible. I just haven’t had good experience with the newest version of OpenCvSharp. I’ll try to find some time this week to put together an example app with the newest version.

          • LuCuS says:

            You should be able to run the app “as-is” without replacing the runtimes. I’m running it “as-is” (with the current runtimes) on Windows 7 x64 with VS10 as well. Try downloading the app from the link above, launching it in VS, and run it without any changes except for those required in App.config.

          • djinty says:

            I just can’t get it running, an example app would really be nice. Thank you in advance.

  2. Ahmedn1 says:

    Very helpful in my project
    thank you very much

  3. azurerhythm says:

    I’ve been running into a problem with your positive builder program. I am sure I have modified the App.config file correctly, but when I try to run it, I get an error like this:

    “An exception has occurred because of P/Invoke.
    Please check the following:

    1. OpenCV’s DLL files exist in the same directory as the executable file.
    2. Visual C++ Redistributable Package has been installed.
    3. The target platform (x86/x64) of OpenCV’s DLL files and OpenCvSharp is the same as your project’s.

    System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
    at OpenCvSharp.CvInvoke.cvReleaseImage(IntPtr& image)
    at OpenCvSharp.Utilities.PInvokeHelper.TryPInvoke()”

    Any suggestions? I’m stumped on this one. Thanks!

    • LuCuS says:

      Are you running 32bit or 64bit Windows and OpenCV? I’ve had some issues with the newest versions of OpenCV and OpenCVSharp for Windows 64bit. However, I do have a working copy of this program for Windows 32bit. There were a couple of other issues with this app that I have now fixed. Try downloading this new version as it has everything you need (including OpenCV’s DLL files). You can download it from http://www.prodigyproductionsllc.com/downloads/PositivesBuilder.zip. Just be sure to update your App.config according to your file system. In the mean time, I’m in the process of updating this to work with the newest OpenCV and OpenCVSharp as well as running on 64bit.

      • SK says:

        Hi LuCuS! This is the most helpful article i’ve read on using c# and opencv together. Great work! Have you by any chance finished the positivies builder for running on 64 bit ? I’m trying to train a haar classifier and although I have an understanding of how to do it from your awesome article, I am unable to because I can’t seem to get positives builder working (I realized it could be because I run a 64 it system). So thought I’d ask! :)

        Thanks in advance!

        • LuCuS says:

          Howdy. Thank you for those kind words! Unfortunately, I have not had time to get this stuff running on 64 bit. I’ve been backed up with working on my new startup and haven’t had time to devote to anything else. I have however received a lot of requests lately and am trying to work in some time to upgrade all of my OpenCV examples to work with 64 bit. Right now, I hope to have time toward the end of next week, but I can’t make any promises yet. As soon as I do get everything upgraded, I’ll be posting the new versions on my site. Until then, here is a list of all of my OpenCV articles using C# in case you haven’t seen them already. http://www.prodigyproductionsllc.com/articles/category/software-development/opencv/ Thanks again and stay tuned!

          • SK says:

            Awesome! Thanks, it is much appreciated!

          • henryt says:

            First of all, thank you LuCuS for providing this tool!

            I’m just starting a project which requires building a Haar Classifier, and I’m sure this tool will be of tremendous help.

            However, I also came across the same error that azurerhythm described above when running the Positive Builder on x64.

            It appears that the problem is due the compiler config in VS2010 as mentioned on the OpenCVSharp FAQ page:
            http://code.google.com/p/opencvsharp/wiki/FAQ

            To solve the problem, you just need to do the following steps in the Positive Builder project:

            Note: The version I am using is the 32-bit version LuCus provided on Dec 04, 2011:
            http://www.prodigyproductionsllc.com/downloads/PositivesBuilder.zip

            1. Follow the steps mentioned on this page to change the target platform to x86:
            http://www.broculos.net/en/article/badimageformatexception-was-unhandled-exception

            2. After doing this, you’ll get some missing references in the project, so you’ll need to download the version of OpenCVSharp (v2.0) which was used from here:
            http://code.google.com/p/opencvsharp/downloads/detail?name=OpenCvSharp-En-2.0-x86-20091222.zip&can=2&q=

            Unzip the files to a directory.

            3. Then remove all of the OpenCVSharp binaries referenced in the project and re-add them by browsing to the files you unzipped above.

            The ones to add are:
            OpenCvSharp.Blob.dll
            OpenCvSharp.CPlusPlus.dll
            OpenCvSharp.dll
            OpenCvSharp.Extensions.dll
            OpenCvSharp.MachineLearning.dll
            OpenCvSharp.UserInterface.dll

            4. Then the final step is to go to the new build path which is under “\PositiveBuilder\bin\x86\debug\” and copy over the OpenCV binaries from the original build path which is under

            “\PositiveBuilder\bin\debug\”

            The files to copy over are:
            cv200.dll
            cvaux200.dll
            cxcore200.dll
            highgui200.dll
            ml200.dll
            opencv_ffmpeg200.dll
            PresentationCore.dll

            5. Then Clean and Rebuild the project and you should be able to run it on a x64 system

            Note: Running the Positive Builder on x64 should solve a lot of problems if you intend to do processing with large video files.
            I actually tried running the 32-bit version of Positive Builder under Windows 7 32bit, but I kept getting out of memory crashes after marking 10 or so images on an x86 machine with 4GB of RAM. But on an x64 system with 8GB of RAM, I’m not longer getting out of memory crashes.

          • LuCuS says:

            Awesome! Thank you for the tip. I’ll take a look at implementing this either tonight or tomorrow and will release a 64bit version when I’m finished. And, yes, I believe you are correct with the OOM issue when running on 32bit. No matter how much memory you have in your computer, Windows 32bit limits you to something like 3.24GB on Windows7 and 3.45GB on Windows XP.

          • LuCuS says:

            Thanks again for your tip. I followed what you said, but kept running into an issue about opencv_imgproc231.dll not being found. Even though I copied this file into my bin folder, I still got the message. With a little bit of research, I found out that I also needed to copy “tbb.dll” into the bin folder. Once I did that, everything worked as expected. I’ve built a new 64bit version and have uploaded it for everyone else to enjoy. You can get it from http://www.prodigyproductionsllc.com/downloads/opencv_positive_builder_64bit.zip. One thing you’ll notice is that I’m using the latest version of OpenCV and OpenCvSharp, version 2.3.1 whereas you were referencing a slightly older version. Let me know if this helps or if you have any other issues.

  4. vukanilod says:

    Hi! I’m pretty new to AR projects but I find your articles very interesting. Especially this one!
    I also wanted to ask what do you think is the best number of positives and negatives for human body recognition? Are there any cascade files already made for this issue???

    Thanks a lot and best regards!

    • LuCuS says:

      When creating cascades for detecting anything, I’ve always had fairly good success with around 2000 positives and 2000 negatives. Though, the more you have of both (especially positives), the better your results will be. Due to the infinite number of body sizes and shapes, you will need to gather as many positives & negatives as possible in order to get something that works efficiently. With that said, OpenCV actually comes with cascades for upper body, lower body, and full body. You can find these cascades in the “\data\haarcascades\” folder when you install OpenCV. The cascades work well for detecting bodies from the front or from the back, but do not work well from the side. A couple of months ago, I ran across a guy that was in the process of creating a cascade for detecting people from the sides. But, he hadn’t released any files at that point and I can’t remember what his site was. I’ll try to locate his site again to see if he has released anything yet.

      • vukanilod says:

        Thanks,this was very useful explanation. I downloaded lfiles from link Ahmedn1′s link, but still need help how to implement them.

        I’m pretty new to all of this Open CV and C#, so thank you in advance for your help.

      • vukanilod says:

        Hi, thank you very much for you rlink.
        But still, I’m pretty new in this and as I mentioned above, I need help in order to implement this files for body detection.

        Thank you!

        • Ahmedn1 says:

          can you please post me the code you are using for detection so I can point you to the point you will change to add those files?

        • LuCuS says:

          I threw together a quick app that does body detection that you can download from http://www.prodigyproductionsllc.com/downloads/BodyTracker.zip. In it, you will see the 3 haar cascades (upper body, lower body, and full body) that Ahmedn1 shared with us above. You can swap out these classifiers in the code depending on your needs. You’ll probably also need to mess around with the ScaleFactor, MinNeighbors, and min_size (the last parameter passed to the HaarDetectObjects method) to get the best performance for your camera and light.

          • vukanilod says:

            Thanks a lot! Your posts are being more then useful for me. But, still, I have problem with project you posted. I can start exe file, but when I try to build a solution I receive an error:Failed to create CVCapture ,at the code line:CvCapture cap = CvCapture.FromCamera(1);
            I also tried to swap CVCapture.FromCamera(1) with CVCapture.FromCamera(CaptureDevice.DShow) but it ain’t been of help.
            Once again, thank you!

          • LuCuS says:

            How many cameras do you have attached to your computer? Does Windows recognize your camera when you plug it in or did you have to install drivers to make your camera work? Open My Computer. Do you see a “USB Video Device” there and if so, can you double-click it and see your video feed? Does it popup another window asking for input parameters for your camera? Try changing FromCamera(1) to FromCamera(0).

          • vukanilod says:

            Hi, LuCuS!
            I have 1 usb cam, I had to install it manually, but when I double click on it in my computer I get the video without any additional parameters or pop-ups.
            Also, I tried to change FromCamera(1) to FromCamera(0) but it ain’t been of help.

          • LuCuS says:

            Does the app work when you run the .exe? Have you been able to use any of my other apps that use the camera?

  5. vukanilod says:

    .exe is working just fine, but I cant build a solution, same problem ocured with your AR application

    • LuCuS says:

      Hmmm? That’s strange. Are you running 32bit or 64bit Windows? Is it XP, Vista, or Win7? What version of OpenCV are you running? Also, in the Solution Explorer, drop down References and make sure that all dependencies are being found correctly.

    • LuCuS says:

      BTW, what do you have your build settings set for? I have those projects set for Debug > x86. You can find out what you have by right-clicking on the project and selecting Project Properties. Then, click on the Build tab. You can also click on the Solution name and select Project > Properties on the menu and then selecting Configuration Properties.

  6. vukanilod says:

    It’s 32 bit XP, but I can’t find open cv version??? All the dependencies are loaded correctly but I can’t find Open CV version. I think this is causing main problem.
    Thank you, I’ll try reinstalling Open CV.
    Which version are you using, can you send me download link.

  7. vukanilod says:

    I’ll try installing Open CV again if you have some version to recommend me, that would be great.

    thanks and regards!

    • LuCuS says:

      I’ve tested those apps with several different versions and haven’t had any issues. I’ll keep digging to see if I can figure out why your executable works, but not the Solution build.

  8. vukanilod says:

    Hi, LuCuS!

    I reinstalled Open CV 2.1.0 and still having same issue…
    Thanks for your help!

  9. umar says:

    Hi . I downloaded the LuCuS version of positive builder . but i get an error in haartraining unable to obtain positive samples in cvcreatetreecascadeclassifier i line 2467. I have created the positives.txt , negatives.txt and positives.vec files . i dont know where is the error popping from.

    • LuCuS says:

      Are your positive and negative image folders in the same directory as your positives.txt, negatives.txt, and positives.vec files? That folder should also contain folders for haarcascade and output as well.

      • umar says:

        Yes my folders are in the same directory.The 0,1 and 2 folders already present in haarcascade directory were creating the error . But Thanks for the reply . I am working on a Project of hand gesture recognition . so I might need your help. Thanks again.

  10. Mr.Lucus =)) hope everything is okay, i know that may be you are busy with some serious work nowadays, but i hope you can spare me a few minutes of your valuable time though =)) i’ve downloaded your positivebuilder but i’m having a lil obstacle in reading the video file from its directory,

    i’v audjusted the app config file as showen above but it gives an error:

    ************** Exception Text **************
    System.IO.FileNotFoundException: File not found
    File name: ‘C:\Users\A.wmv\’
    i’d be grateful for your help =)) and sorry for stealing your valuable time =))

    • LuCuS says:

      Try removing the final “\” (backslash) after the “.wmv” piece. It should just be: “C:\Users\A.wmv”. Also, I’ve never tried using a “wmv” with OpenCv. I’m not sure if it supports that format or not.

      • it worked !! greaaaaaat thank you pretty much Mr.Lucus, well btw i need to ask you a question, can i make a video including the hand from many angels and a different lighting enviroments to detect the hand gestures and postures ?

        • LuCuS says:

          Yes. That’s definitely possible. I’ve also seen other readers apply filters to their videos and images so that they all have essentially the same lighting which helps with performance. I haven’t done this myself. But, one filter you might want to consider is the “canny” filter. It’s basically the same filter used in that YouTube video you posted a few days ago. It’s the black and white windows on the right. You can find an example of how to do it in my “Edge Detection with OpenCV and C#” article. http://www.prodigyproductionsllc.com/articles/programming/opencv-edge-detection-and-video-capture-with-c/. You’ll have to tweak it a bit to remove all of the background noise.

          • yeah i have downloaded that Edge Detection program before since the first time i found your articles and it was interesting, how can i tweak it a bit to remove all of the background noise ?

          • Cv.Canny(gray, dstCanny, 50, 50, ApertureSize.Size3) –> i’v tweaked it a bit and removed the noise, i should have tried before askin you :) ), okay there another question i made a video 30 minutes including my hand from many angels and have built the positive images by pressing 1 and it has been generated as you mentioned the article exaclty without being croped but the coordinates still saved in the positive.txt , i’v made another video which didn’t include my hand, and built the negatives from it, now it comes to the .vec file every time i press v an error appears :
            ************** Exception Text **************
            System.ComponentModel.Win32Exception (0×80004005): The system cannot find the file specified
            at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo)
            at System.Diagnostics.Process.Start()
            at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
            at System.Diagnostics.Process.Start(String fileName, String arguments)
            at PositiveBuilder.Form1.WriteVec() in E:\Programs\Computer Science\Graduation Year\Project’s Stuff\My Thoughts\OpenCV Stuff\Projects\HaarClassifier\opencv_haar_cascade_positive_builder\opencv_positive_builder\PositiveBuilder\Form1.cs:line 172
            at PositiveBuilder.Form1.Form1_KeyPress(Object sender, KeyPressEventArgs e) in E:\Programs\Computer Science\Graduation Year\Project’s Stuff\My Thoughts\OpenCV Stuff\Projects\HaarClassifier\opencv_haar_cascade_positive_builder\opencv_positive_builder\PositiveBuilder\Form1.cs:line 249

            so what would that be =)) ?

          • LuCuS says:

            Does the video that includes your hand have other things in it as well such as yourself? If the video shows more than just your hand, you will need to crop each image by clicking and creating lines like I did in the example image above. Also, in your App.config file, what do you have set for OpenCvRoot and OutputFolder? Have you changed these values to match those of your file system? Right before line #172, add the following line:

            Console.WriteLine(“~!~!~! ” + command);

            Then, run the app. Right before the app blows up at line 172, you should see the above command written to your Output window. Copy and paste that for me to look at.

  11. this is how i adjusted the AppConfig

  12. the code of App.Config doesn’t appear in the comments here, it supposed to be pasted before the sentence “this is how i adjusted the AppConfig”.

  13. I’ve used a video containing me and my hand captured from the webcam actually, and i’ve set the upper left corner of the hand and the right lower corner (Y), and the configurations for the App.config are:

    OpenCvRoot –> C:\Program Files\OpenCV2.1\
    ———————————
    OutoutFolder –> E:\Programs\Computer Science\Graduation Year\Project\OpenCV\Haar Classifier\opencv_haar_cascade_positive_builder\Output\
    ——————————–
    i added the line too before lines number 172 but i can’t understand this part:

    [ run the app. Right before the app blows up at line 172, you should see the above command written to your Output window. Copy and paste that for me to look at. ]

    which Output window?, and how can i know that the app blowing up at 172 !!

    • LuCuS says:

      When you run the application, you see your video window popup over everything. Visual Studio is still behind that window. While the application is running, you should see a window in VS labeled “Output”. If you do not see the Output window, click on View > Output. Then, right before your application blows up, you should see the command printed to that Output window. It will be the line that begins with “~!~!~!”. The reason I know it’s blowing up at line 172 is because the stack trace from your last message says so. Take a look at the following line that I copied from your last message.

      Stuff\Projects\HaarClassifier\opencv_haar_cascade_positive_builder\opencv_positive_builder\PositiveBuilder\Form1.cs:line 172

      At the end of it, it says “line 172″. Typically when debugging an error like this I would put a break point at line 172 and walk the application until it hits this break point. Since I can’t recreate your problem, I can only know what your application is doing by writing output to the console. That’s where that Console.WriteLine(….) code comes into play.

  14. pruthviraj says:

    hi ,
    problem-.vec file can’t created
    i was downloaded your positive builder,using that i have created positive.txt and negative.txt . images were saved in positive folder and negative folder but i have one problem is that when i press ‘v’ key then .vec file can’t created and i got error ‘the system can’t find fiIe specified’.
    In App.config,

    for key opencvroot which path will be set because i have opencv folder in c drive ,not in program file . so i changed path

    still it is not working ….
    plz help me……..

  15. IRV4479 says:

    Hello LuCuS. First off all thanks for your work. I’m trying to use your application but I alwaya take the same exception when I try to run the build .exe. This is what I get:

    OpenCvSharp.OpenCvSharpException: Failed to create CvCapture
    en OpenCvSharp.Cv.CreateFileCapture(String filename)
    en PositiveBuilder.Form1.SetupVideo() en D:\opencv_positive_builder_64\PositiveBuilder\Form1.cs:línea 134
    en PositiveBuilder.Form1.Form1_Load(Object sender, EventArgs e) en D:\opencv_positive_builder_64\PositiveBuilder\Form1.cs:línea 60
    en System.Windows.Forms.Form.OnLoad(EventArgs e)
    en System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
    en System.Windows.Forms.Control.CreateControl()
    en System.Windows.Forms.Control.WmShowWindow(Message& m)
    en System.Windows.Forms.Control.WndProc(Message& m)
    en System.Windows.Forms.Form.WmShowWindow(Message& m)
    en System.Windows.Forms.Form.WndProc(Message& m)
    en System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

    I’m using VS2010 on Windows 7 x64 and your updated code for x64 architecture whit no changes except for connfig directories.

    Thank you in advance.

    • LuCuS says:

      It looks like it’s having problems finding your video. Have you double checked that your video file path is correct? Also, what type of video is it?

      • IRV4479 says:

        Yes, I tried to move to root and use a simplier name like abc. I tried avi, wmv and mp4. Thanks for your answer.

      • IRV4479 says:

        Furthermore, I´ve got some images with more than one desired object. Is there anyway to take more than one object? I tried to mark al of the in the frame, but only the first one is passed to the .txt file.

        Thank you very much.

        • LuCuS says:

          You’ll need to create individual haar files for each object you want to detect. Then, in your app, you’ll need to iterate through each of the haar files to check for the existence of each object.

          • IRV4479 says:

            it seems that you didn’t understood me. I have the same object twice and Opencv documentation tells that it can be marked more tha one object in description file. Two faces in example.

          • LuCuS says:

            When building the classifier, OpenCV only needs 1 of the objects. My app only reads the first marked image because that’s all OpenCV needs to train the classifier with. When using your classifier in an application, OpenCV will detect the object no matter how many times it appears in your image / video. For example, if you create a classifier to detect an iPhone, you will build a classifier with only 1 iPhone in the image. When OpenCV detects the iPhone in your app, OpenCV will outline each iPhone it detects within each frame.

  16. Aharris says:

    Hey
    I’ve manage to get most of the Positive builder working, thank you for that, but when using the haartraining I didn’t get a xml file rather it created a haarcascade folder which in turn contained multiple other folders numbered 1-7. all of these folders contain the same single file named AdaBoostCARTHaarClassifier.

  17. Avin says:

    Dear LuCus.

    Thank you for your post and the work you have done which has really helped me to understand how to train my own classifier.

    Please may I ask for your guidance.

    My Application: Detect fixed sized symbols that light up on a LCD.

    Idea: Train my own OpenCV Haarclassifier for each symbol and use OpenCv to detect when a symbol lights up. Similar to the logo example that you described in your post.

    OpenCv Version Used: 2.3

    Progress: I have created 1 positive image, 1 negative image, a vector file.

    Query: I have run the haartraining.exe for approximately 5 days but am unable to train my own Haar Classifier.

    Command line used:
    opencv_haartraining.exe -data tick_classifier -vec tick.vec -bg negatives.txt -w 44 -h 51 -npos 1 -nneg 1

    Output from the application:
    Data dir name: tick_classifier
    Vec file name: tick.vec
    BG file name: negatives.txt, is a vecfile: no
    Num pos: 1
    Num neg: 1
    Num stages: 14
    Num splits: 1 (stump as weak classifier)
    Mem: 200 MB
    Symmetric: TRUE
    Min hit rate: 0.995000
    Max false alarm rate: 0.500000
    Weight trimming: 0.950000
    Equal weights: FALSE
    Mode: BASIC
    Width: 44
    Height: 51
    Applied boosting algorithm: GAB
    Error (valid only for Discrete and Real AdaBoost): misclass
    Max number of splits in tree cascade: 0
    Min number of positive samples per cluster: 500
    Required leaf false alarm rate: 6.10352e-005

    Then it gives me the different parent nodes that it is processing.
    The application has not progressed past “Parent Node 6″ though there is processing in the background.

    Question: Is there anything that you could suggest I am doing wrong?

    I would appreciate any help you could provide.

    • LuCuS says:

      Working with only 1 positive image and 1 negative image isn’t nearly enough to train a haar classifier. However, being that you only have 2 images and their small sizes, there’s no reason it should be running as long as it has been. Try the following command to build your XML:

      opencv_haartraining.exe -data tick_lassifier -vec tick.vec -bg negatives.txt -nstages 20 -nsplits 2 -minhitrate 0.999 -maxfalsealarm 0.5 -npos 1 -nneg 1 -w 44 -h 51 -nonsym -mem 512 -mode ALL

      Also, are your LEDs different colors? If so, I wouldn’t bother building a haar classifier. Instead, I would use histograms to watch for the colors of your LEDs. Here is a snippet from my hand detection app that you can easily modify to watch for the colors of your LEDs. Right now, the color ranges are set for flesh-tones. Just change your ranges to match those of your LEDs.

      private static void RetrieveFleshRegion(IplImage imgSrc, IplImage[] hsvPlanes, IplImage imgDst)
      {
      int[] histSize = new int[] { 30, 32 };
      float[] hRanges = { 0.0f, 20f };
      float[] sRanges = { 50f, 255f };
      float[][] ranges = { hRanges, sRanges };

      imgDst.Zero();
      using (CvHistogram hist = new CvHistogram(histSize, HistogramFormat.Array, ranges, true))
      {
      hist.Calc(hsvPlanes, false, null);
      float minValue, maxValue;
      hist.GetMinMaxValue(out minValue, out maxValue);
      hist.Normalize(imgSrc.Width * imgSrc.Height * 255 / maxValue);
      hist.CalcBackProject(hsvPlanes, imgDst);
      }
      }

Leave a Reply