This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include <cv.h> | |
#include <cxcore.h> | |
#include <ml.h> | |
using namespace std; | |
int main() | |
{ | |
CvSVM SVM; | |
CvMat trainData; | |
CvMat labels; | |
CvTermCriteria criteria = cvTermCriteria(CV_TERMCRIT_EPS,100,0.001); //eğitim için term kriterimiz. | |
int array_labels[5]= {1,1,1,-1,-1}; //5 eğitim verisinin bildiğimiz etiketleri.(labels, responses). | |
//ilk 3 veri 1 sınıfına, diğer iki veri ise -1 sınıfına ait. | |
//eğitim verilerini diziye doldurdum. Gerçek uygulamalarda filedan okuma veya hesaplama şeklinde olur. | |
//kodu basit tutmak için bu şekilde gösterdim. | |
#pragma region data | |
//5 egitim verisi olsun herbiri 40 tane feature içersin. | |
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 | |
}; | |
#pragma endregion data | |
cvInitMatHeader(&trainData,5,40,CV_32FC1,array_trainData); //eğitim verimizi tutacak matris. | |
cvInitMatHeader(&labels,1,5,CV_32SC1,array_labels); //etiketlerimizi tutacak matris. | |
//classification olduğu için CV_32SC1 kullandık. | |
SVM.train(&trainData,&labels,0,0,CvSVMParams(CvSVM::C_SVC,CvSVM::RBF,3,0.025,0,1,0.5,0.1,NULL, | |
criteria)); //train fonksiyonumuz (libsvm ile kontrol etmek için libsvm ile | |
//gelen svm-train.exe nin default parametreleri kullanıldı.) | |
//Parametrelerin detaylı açıklaması için opencv docs. | |
//SVM.save("test.volkan"); //eğittiğimiz modeli kaydedip daha sonra kullanabiliriz. | |
for(int i = 0; i < 5; i++ ) | |
{ | |
double r; | |
CvMat sample; | |
cvGetRow(&trainData, &sample, i ); | |
r = SVM.predict( &sample ); //predict edip sonuçları kontrol edelim. | |
} | |
} |
Kod
12 yorum - yorum yaz:
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..
Native' has exited with code 0 (0x0).
bu kodu çalıştırdığımda böyle bi hata veriyo opencv 1.1 kullanıyorum
Yazdığının kodu dosya halinde bana mail atar mısınız? Bir de ben deniyeyim.
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
cool code...totally wish it were in spanish...or in english!
hey does anyone else only get 0 values from the prediction method? I would expect 1 or -1 right?
what is going on? :(
i'm getting 5 zeros too!
#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
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....
@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.
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...
@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