OpenCV altında SVM kullanmak. (OpenCV SVM Sample Code)

Bilindiği gibi OpenCV kütüphanesinin machine learning modülünde libsvm wraplenmiştir. Böylece biz OpenCV kullanıcıları harici bir kütüphane kullanmadan SVM(Support Vector Machines) i uygulamalarımızda kullanabiliriz. Aşağıda bu konuda bir örnek hazırlamaya çalıştım. Yorumlarda açıklamalar mevcuttur.




Kod

12 yorum - yorum yaz:

erol dedi ki...

merhaba hocam
evet dediğiniz gibi olcak. bu için libsvm kullanarak one_vs_rest C-SVM programı (C++) yazmak istiyorum yada opencv ile..
featureları girince bunu train ve test edecek iki fonksiyon olcak.bu konuda yeni olduğum için yardımlarınızı bekliyorum..

erol dedi ki...

Native' has exited with code 0 (0x0).
bu kodu çalıştırdığımda böyle bi hata veriyo opencv 1.1 kullanıyorum

Volkan SALMA dedi ki...

Yazdığının kodu dosya halinde bana mail atar mısınız? Bir de ben deniyeyim.

eoc dedi ki...

Merhabalar ornek olarak cok guzel. Bu konuda se bir sorum olacak. Benim yapmak istedigim svm uygulamasinda birden fazla taninmasi gereken deger var, eldeki isaretler gibi.

sizin orneginizde 1 ve -1 degerleri parametre olarak girilmis. ben bu degerler disinda 2 veya 0.5 gibi bir ucuncu deger girebilir miyim.

simdiden tesekkurler

Little Saiph dedi ki...

cool code...totally wish it were in spanish...or in english!

Little Saiph dedi ki...

hey does anyone else only get 0 values from the prediction method? I would expect 1 or -1 right?
what is going on? :(

Adsız dedi ki...

i'm getting 5 zeros too!

Adsız dedi ki...

#include
#include
#include
#include

using namespace std;
const int NUM_SAMPLES = 5;
const int NUM_FEATURES = 40;

int main()
{
CvSVM SVM;
CvMat* trainData=cvCreateMat(NUM_SAMPLES,NUM_FEATURES,CV_32F);
CvMat* labels=cvCreateMat(NUM_SAMPLES, 1,CV_32SC1);
CvTermCriteria criteria = cvTermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER,1000,1e-6);

int array_labels[NUM_SAMPLES]= {1, -1, 1, -1, -1};

float array_trainData[5][40] = { 1,0.443137,0.333333,0.392157,0.0313725,
0.270588,0.870588,0.964706,1,0.439216,
0.337255,0.380392,0.0235294,0.266667,0.862745,
0.964706,1,0.431373,0.337255,0.368627,
0.0156863, 0.262745, 0.854902, 0.964706, 1,
0.423529, 0.341176, 0.360784, 0.0156863, 0.258824,
0.847059, 0.964706, 1, 0.419608, 0.341176,
0.34902, 0.0117647, 0.254902, 0.839216, 0.968627,

0.572549,0.4,0.113725,0.0784314,0.137255,
0.160784,0.00784314,0.337255,0.580392,0.407843,
0.121569, 0.0745098, 0.133333, 0.156863, 0.00392157,
0.341176, 0.584314, 0.411765, 0.12549, 0.0666667 ,
0.129412, 0.152941, 0, 0.34902, 0.592157,
0.419608, 0.129412, 0.0588235, 0.12549, 0.14902,
0.00392157, 0.352941, 0.596078, 0.423529, 0.137255,
0.054902, 0.121569, 0.145098, 0.00784314, 0.356863,

0.882353, 0.741176, 0.305882, 0.305882, 0.278431,
0.141176, 0.156863, 0.509804, 0.882353, 0.741176,
0.305882, 0.305882, 0.27451, 0.141176, 0.14902,
0.501961, 0.886275, 0.741176, 0.305882, 0.305882,
0.270588, 0.141176, 0.141176, 0.498039, 0.886275,
0.741176, 0.305882, 0.305882, 0.266667, 0.141176,
0.137255, 0.490196, 0.886275, 0.737255, 0.305882,
0.305882, 0.266667, 0.137255, 0.129412, 0.486275,

0.929412, 0.917647, 0.717647, 0.356863, 0.133333,
0.156863, 0.231373, 0.709804, 0.929412, 0.913725,
0.721569, 0.356863, 0.137255, 0.164706, 0.219608,
0.694118, 0.92549, 0.905882, 0.729412, 0.356863,
0.141176, 0.168627, 0.207843,0.67451, 0.917647,
0.901961, 0.733333, 0.360784, 0.141176, 0.172549,
0.196078, 0.658824, 0.909804, 0.894118, 0.737255,
0.360784, 0.141176, 0.180392,0.180392, 0.639216,

0.760784, 0.372549, 0.407843, 0.298039, 0.160784,
0.72549, 0.988235, 0.960784, 0.772549, 0.372549,
0.411765, 0.305882, 0.160784, 0.709804, 0.992157,
0.960784, 0.780392, 0.376471, 0.419608, 0.309804,
0.160784, 0.698039, 0.992157, 0.960784, 0.784314,
0.376471, 0.419608, 0.317647, 0.164706, 0.686275,
0.996078, 0.964706, 0.792157, 0.376471, 0.423529,
0.32549, 0.160784, 0.670588, 1, 0.964706
};

for(int i=0;i<NUM_SAMPLES;i++){
for(int j=0;j<NUM_FEATURES;j++){
CV_MAT_ELEM(*trainData,float,i,j)=array_trainData[i][j];
}
CV_MAT_ELEM(*labels,int,i,0)=array_labels[i];
}

SVM.train(trainData,labels,0,0, CvSVMParams());

for(int i = 0; i < NUM_SAMPLES; i++ )
{
double r;
CvMat sample;
cvGetRow(trainData, &sample, i );
r = SVM.predict( &sample );
cout<<r<<" ";

}
}

//Hope this help

Bia dedi ki...

Hello, Can you help me how can i form this

float array_trainData[5][40] = { 1,0.443137,0.333333,0.392157,0.0313725,
0.270588,0.870588,0.964706,1,0.439216,
0.337255,0.380392,0.0235294,0.266667,0.862745,
0.964706,1,0.431373,0.337255,0.368627,
0.0156863, 0.262745, 0.854902, 0.964706, 1,
0.423529, 0.341176, 0.360784, 0.0156863, 0.258824,
0.847059, 0.964706, 1, 0.419608, 0.341176,
0.34902, 0.0117647, 0.254902, 0.839216, 0.968627,

0.572549,0.4,0.113725,0.0784314,0.137255,
0.160784,0.00784314,0.337255,0.580392,0.407843,
0.121569, 0.0745098, 0.133333, 0.156863, 0.00392157,
0.341176, 0.584314, 0.411765, 0.12549, 0.0666667 ,
0.129412, 0.152941, 0, 0.34902, 0.592157,
0.419608, 0.129412, 0.0588235, 0.12549, 0.14902,
0.00392157, 0.352941, 0.596078, 0.423529, 0.137255,
0.054902, 0.121569, 0.145098, 0.00784314, 0.356863,

0.882353, 0.741176, 0.305882, 0.305882, 0.278431,
0.141176, 0.156863, 0.509804, 0.882353, 0.741176,
0.305882, 0.305882, 0.27451, 0.141176, 0.14902,
0.501961, 0.886275, 0.741176, 0.305882, 0.305882,
0.270588, 0.141176, 0.141176, 0.498039, 0.886275,
0.741176, 0.305882, 0.305882, 0.266667, 0.141176,
0.137255, 0.490196, 0.886275, 0.737255, 0.305882,
0.305882, 0.266667, 0.137255, 0.129412, 0.486275,

0.929412, 0.917647, 0.717647, 0.356863, 0.133333,
0.156863, 0.231373, 0.709804, 0.929412, 0.913725,
0.721569, 0.356863, 0.137255, 0.164706, 0.219608,
0.694118, 0.92549, 0.905882, 0.729412, 0.356863,
0.141176, 0.168627, 0.207843,0.67451, 0.917647,
0.901961, 0.733333, 0.360784, 0.141176, 0.172549,
0.196078, 0.658824, 0.909804, 0.894118, 0.737255,
0.360784, 0.141176, 0.180392,0.180392, 0.639216,

0.760784, 0.372549, 0.407843, 0.298039, 0.160784,
0.72549, 0.988235, 0.960784, 0.772549, 0.372549,
0.411765, 0.305882, 0.160784, 0.709804, 0.992157,
0.960784, 0.780392, 0.376471, 0.419608, 0.309804,
0.160784, 0.698039, 0.992157, 0.960784, 0.784314,
0.376471, 0.419608, 0.317647, 0.164706, 0.686275,
0.996078, 0.964706, 0.792157, 0.376471, 0.423529,
0.32549, 0.160784, 0.670588, 1, 0.964706
};

training data using cvExtractsurf for featuring
extraction then form this array for SVM


please help me....

Volkan SALMA dedi ki...

@Bia

Dear Bia,

I'm not sure that I got your question correctly. But ı will
try to answer it.

'cvExtractsurf(const CvArr* image, const CvArr* mask, CvSeq** keypoints, CvSeq** descriptors, CvMemStorage* storage, CvSURFParams params)'

the fourth parameter of function is output descriptors. You can get the values in a loop with 'cvGetSeqElem' function. And
you could fill our feature array as you see above in the example code.

bilgeakkoca dedi ki...

merhaba, ben kodunuzu düzenleyip çalıştırdığımda train fonksiyonundan sonraki satırda access violation reading location hatası veriyor. Çok aradım ama çözümü bulamadım. sizce nasıl çözebilirim? şimdiden teşekkürler...

Volkan SALMA dedi ki...

@bilgeakkoca

Merhabalar,
Öyle sanıyorum ki versiyon farklılığından kaynaklanan bir durum var. Çünkü yazıyı yazdıktan sonra epey bir opencv versiyonu çıktı. Hangi versiyonuu kullanıyorsunuz ve hangi platformda kodunuzu yazıyorsunuz. Yazının altında adsız isminde birinin yazdığı yorum var. Oradaki kodu da bir deneyin çalışmazsa eğer ben yeni opencv versiyonunda yeni bir opencv-svm yazısı yazayım. İyi çalışmalar

Yorum Gönder