Ask Your Question

Error when trying to execute the SVM example code

asked 2013-04-18 00:34:06 -0500

roomini gravatar image

updated 2013-04-18 01:37:29 -0500

Guys, I am new to openCV and programming. I tried executing the SVM code in PDF and I am getting the above exception.

Unhandled exception at 0x65e70fcd in svm.exe: 0xC0000005: Access violation reading location 0x00000000.

Can someone please help me out with this?

#include "stdafx.h"

#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include <ml.h>

 //#include <opencv2/core/core.hpp>
 //#include <opencv2/highgui/highgui.hpp>
 //#include <opencv2/ml/ml.hpp>

 using namespace cv;
 using namespace std;

 int main()
 // Data for visual representation
 int width = 512, height = 512;
 Mat image = Mat::zeros(height, width, CV_8UC3);

 // Set up training data
 float labels[4] = {1.0, -1.0, -1.0, -1.0};
 Mat labelsMat(3, 1, CV_32FC1, labels);

 float trainingData[4][2] = { {501, 10}, {255, 10}, {501, 255}, {10, 501} };
 Mat trainingDataMat(3, 2, CV_32FC1, trainingData);

 // Set up SVM’s parameters
 CvSVMParams params;
params.svm_type = CvSVM::C_SVC;
 params.kernel_type = CvSVM::LINEAR;
 params.term_crit = cvTermCriteria(CV_TERMCRIT_ITER, 100, 1e-6);

 // Train the SVM
 SVM.train(trainingDataMat, labelsMat, Mat(), Mat(), params);

 Vec3b green(0,255,0), blue (255,0,0);
 // Show the decision regions given by the SVM
 for (int i = 0; i <2; ++i)
 for (int j = 0; j <2; ++j)
 Mat sampleMat = (Mat_<float>(1,2) << i,j);
 float response = SVM.predict(sampleMat);

 if (response == 1)<Vec3b>(j, i) = green;
 else if (response == -1)<Vec3b>(j, i) = blue;

 // Show the training data
 int thickness = -1;
 int lineType = 8;
 circle( image, Point(501, 10), 5, Scalar( 0, 0, 0), thickness, lineType);
 circle( image, Point(255, 10), 5, Scalar(255, 255, 255), thickness, lineType);
 circle( image, Point(501, 255), 5, Scalar(255, 255, 255), thickness, lineType);
circle( image, Point( 10, 501), 5, Scalar(255, 255, 255), thickness, lineType);

 // Show support vectors
 thickness = 2;
 lineType = 8;
 int c = SVM.get_support_vector_count();

 for (int i = 0; i < c; ++i)
 const float* v = SVM.get_support_vector(i);
 circle( image, Point( (int) v[0], (int) v[1]), 6, Scalar(128, 128, 128), thickness, lineType);

 imwrite("result.png", image); // save the image

 imshow("SVM Simple Example", image); // show it to the user


i get only 1 exception i.e mentioned above when i add ml210.lib to the linker.. if i do not add ml210.lib to d linker i get d following errors..

1>svm.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall CvSVM::~CvSVM(void)" ([email protected]@[email protected]) referenced in function _main

1>svm.obj : error LNK2019: unresolved external symbol "public: virtual float const * __thiscall CvSVM::get_support_vector(int)const " ([email protected]@@[email protected]) referenced in function _main

1>svm.obj : error LNK2019: unresolved external symbol "public: virtual int __thiscall CvSVM::get_support_vector_count(void)const " ([email protected]@@UBEHXZ) referenced in function _main

1>svm.obj : error LNK2019: unresolved external symbol "public: virtual float __thiscall CvSVM::predict(class cv::Mat const &,bool)const " ([email protected]@@[email protected]@@[email protected]) referenced in function _main

1>svm.obj : error LNK2019: unresolved external symbol "public: virtual bool __thiscall CvSVM::train(class cv::Mat const &,class ...
edit retag flag offensive close merge delete


  1. yes, you need to link against opencv_ml210.lib, (opencv_ml210d.lib even for debug mode)
  2. you show ~100 lines of code, but where does the (runtime) exception happen ? do some debugging
  3. i can't reproduce your error with 2.4.9 so again, why are you using 2.1.0 ?
berak gravatar imageberak ( 2013-04-18 01:43:52 -0500 )edit

Since m new to this OpenCV library i didn't know which one to use.. some said 2.4.9 is complex to understand so m trying out with 2.1.0

roomini gravatar imageroomini ( 2013-04-18 01:58:01 -0500 )edit

yes, 2.4.5 is the latest stable one

berak gravatar imageberak ( 2013-04-18 02:00:06 -0500 )edit

Basically 2.4.9 you need to compile yourself and could be unstable. However, on the main download page you can download the 2.4.5 release with prebuilt binaries, which is actually the most recent and stable version.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-04-18 02:00:20 -0500 )edit

m getting error at line no. 75 i.e imwrite("result.png", image);

roomini gravatar imageroomini ( 2013-04-18 02:20:23 -0500 )edit

not so, here, but i had to add

namedWindow("SVM Simple Example");

before the next line ( since my version was compiled with opengl support )

berak gravatar imageberak ( 2013-04-18 02:23:48 -0500 )edit

M not getting whats wrong. Once this line is compiled m getting the exception

roomini gravatar imageroomini ( 2013-04-18 02:27:14 -0500 )edit

@roomini, can you please make effort to write in full. 'm' is not a word, 'I am' on the contrary is. Your posts become hard to read.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-04-18 02:35:40 -0500 )edit

ok i am sorry..

roomini gravatar imageroomini ( 2013-04-18 02:40:42 -0500 )edit

2 answers

Sort by » oldest newest most voted

answered 2013-04-18 01:47:40 -0500

Basically first of all you need to place a debug point at the beginning of your code then looping through each step and tell us on which line of code it actually crashes. Without that it is kind of hard to know what produced the error.

Also please read the beginners guide in order to format your posts a bit more clearly and to provide all necessary informatie.

edit flag offensive delete link more


Actually when i debug, eventhough i have given (i and j) <=2 its going to an infinite loop..

can u please tel me how to work out with SVM??

roomini gravatar imageroomini ( 2013-04-18 02:01:00 -0500 )edit

Please make yourself more clear. I don't have ready to serve SVM code, you will need to help yourself and ask solutions to problems that occur.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-04-18 02:02:57 -0500 )edit

I'm doing a project on "Human Activity Recognition". I have training and test set videos for 10 different activities with 8 features each. I need to classify this using SVM

roomini gravatar imageroomini ( 2013-04-18 02:24:57 -0500 )edit

answered 2013-05-25 14:09:58 -0500

I have the same error with 2.4.5. I am able to successfully run other opencv programs. For latent SVM I get this error. Any fix is appriciated?

edit flag offensive delete link more



can you please start a new question ? it will get pretty messy otherwise

berak gravatar imageberak ( 2013-05-25 14:22:11 -0500 )edit

Question Tools


Asked: 2013-04-18 00:34:06 -0500

Seen: 1,960 times

Last updated: May 25 '13