Monday, January 24, 2011

SVM in OpenCV

   

0.1,0.12,0.43,0.11,0.13,0
0.1,0.2,0.4,0.1,0.3,0
0.12,0.22,0.41,0.12,0.32,0
0.31,0.23,0.44,0.21,0.23,0
0.13,0.23,0.43,0.13,0.23,0
0.14,0.24,0.44,0.14,0.34,0
0.14,0.42,0.44,0.14,0.34,0
0.51,0.52,0.54,0.51,0.53,0
0.51,0.52,0.54,0.51,0.53,1
0.52,0.58,0.57,0.54,0.55,1
0.53,0.54,0.55,0.56,0.54,1
0.53,0.57,0.59,0.59,0.59,1
0.69,0.62,0.69,0.69,0.53,2
0.61,0.62,0.54,0.59,0.53,2
0.71,0.72,0.74,0.51,0.53,2
0.81,0.82,0.84,0.81,0.83,2
0.85,0.82,0.85,0.81,0.83,2
0.85,0.92,0.85,0.81,0.93,2
0.95,0.82,0.85,0.91,0.83,2

- last columns represent classes and first columns represent features


vector> descriptor;
    vector classes;
    ifstream f;
    f.open("c:\\bayes.txt");
    while(f)
    {
        string str = "";
        f>>str;
        vector desc;
        size_t found = str.find(",");
        while(found<100 && found>0)
        {
            string t = str.substr(0,found);
            desc.push_back(strtod(t.c_str(), NULL));
            str = str.substr(found+1);
            found = str.find(",");
        }
        if(desc.size()>0)
        {
            descriptor.push_back(desc);
            classes.push_back(strtod(str.c_str(), NULL));
        }
    }
    f.close();
   
    vector v;
    v.push_back(0.9481);
    v.push_back(0.9432);
    v.push_back(0.434);
    v.push_back(0.0531);
    v.push_back(0.533);
   
    int num_samples = descriptor.size();
    int num_features = descriptor[0].size();
   
    CvMat* data = cvCreateMat( num_samples, num_features, CV_32F );
    CvMat* responses = cvCreateMat( descriptor.size(), 1, CV_32F );
   
    for(int i=0;i
        {
          for(int j=0;j
             CV_MAT_ELEM(*data,float,i,j)=descriptor[i][j];
         CV_MAT_ELEM(*responses,float,i,0)=classes[i];
        }
       
       
   // initialize parameters
   CvTermCriteria crit;
   crit.epsilon = CV_TERMCRIT_EPS;
   crit.max_iter = 1000;
   crit.type = 1;
  
   CvSVMParams params;
  
   params.svm_type = CvSVM::C_SVC;
   params.kernel_type = CvSVM::RBF;
   params.gamma = 0.1;
   params.C = 3;
   params.nu = 0.1;
   params.term_crit = crit;
    

   CvMat* sample = cvCreateMat( 1,num_features, CV_32F );
   for(int j=0;j
             CV_MAT_ELEM(*sample,float,0,j)=v[j];

   CvSVM SVM;
   SVM.train(data,responses,0,0,params);
   float   r = SVM.predict( sample );
  
  
   SVM.clear();
   cvReleaseMat(&sample);
   cvReleaseMat(&data);
   cvReleaseMat(&responses); 

Thursday, January 13, 2011

Open MP C++ in Visual Studio

To set this compiler option in the Visual Studio development environment

   1. Open the project's Property Pages dialog box. For details, see How to: Open Project Property Pages.
   2. Expand the Configuration Properties node.
   3. Expand the C/C++ node.
   4. Select the Language property page.
   5. Modify the OpenMP Support property.

Verify that _OPENMP directive is set:


#ifdef _OPENMP  
fn();  
#endif
 





Sample of program that displays the number of procesors


#include "stdafx.h"
#include "time.h"
#include < iostream >
#include < memory >
#include < omp.h >
  
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{


    time_t start,end;
    time(&start);

    #pragma omp parallel
     printf("Hello from thread %d, nthreads %d\n",  omp_get_thread_num(), omp_get_num_threads());

    time(&end);

    double dif = difftime(end, start);
    cout<
   
}




// sample using loops


#include "stdafx.h"
#include "time.h"
#include < iostream >
#include < memory >
#include < omp.h >
  
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
   time_t start,end;
   time(&start);
   #pragma omp parallel for default(shared)
   for(int i=0;i<2000000000;i++)
    {
            int a = 3;
            int b = 4;
            int aux;
            aux = a; a=b;b=aux;
            for(int t=0;t<20;t++)
            {
                int a2 = 3;
                int b3 = 4;
                aux = a2; a2=b;b=aux;
            }
     }

    time(&end);
    double dif = difftime(end, start);
    cout<
   
return 0;

}


 

Tuesday, January 11, 2011

Get element height - Javascript

When you try to get the height of an element that does not exist yet elem.style.height won't work.

So this is how it should be retreived:

document.getElementById('elem_name').offsetHeight;

Friday, January 7, 2011

learn c++

Code analyser c# and c++:

http://cppcheck.sourceforge.net/manual.pdf
https://secure.wikimedia.org/wikipedia/en/wiki/List_of_tools_for_static_code_analysis
http://articles.techrepublic.com.com/5100-10878_11-5034931.html
fxcop:
http://code.msdn.microsoft.com/codeanalysis/Release/ProjectReleases.aspx?ReleaseId=553
stylecop
http://stylecop.codeplex.com/

Books

http://www.amazon.com/dp/0201749629/?tag=stackoverfl08-20
Effective C++: 50 Specific Ways to Improve Your Programs and Design (2nd Edition) (Addison-Wesley Professional Computing Series
Effective STL: 50 Specific Ways to Improve Your Use of the Standard Template Library