gpt4 book ai didi

opencv - 使用功能包进行分类的词汇/字典

转载 作者:行者123 更新时间:2023-12-02 16:41:13 28 4
gpt4 key购买 nike

我正在尝试使用SVM进行3类分类。为此,我正在SVM培训期间准备词汇表。但是,由于我在SVM预测期间获得随机结果,因此我怀疑我的词汇创建方法中存在一些问题。我创建词汇的代码如下:

//Mat train --- it should contain the feature vectors
//Mat response-- it will contain the class labels

void svm::createTrainingDateUsingBOW(int flag,Mat& train, Mat& response, int label)
{

int cluster = 9; // Common for all classes
cv::Mat imageForTraining;
std::vector<cv::KeyPoint> keypoints;
cv::SurfFeatureDetector detector(500);
cv::Ptr<cv::DescriptorExtractor> cvDescExt = new cv::SurfDescriptorExtractor();
cv::Mat descriptors;

cv::BOWKMeansTrainer bow(cluster, cv::TermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 10, FLT_EPSILON), 1, cv::KMEANS_PP_CENTERS);
cv::Mat vocabulary;

if (flag == 1)
{
for(int i=1; i<=400; i++)
{
counter++;
cout<<"\n counter: "<<counter;

char filepath[255];
sprintf(filepath, "class1/%d.JPG",i); // we need class1/1.JPG etc

imageForTraining = cv::imread(filepath, CV_LOAD_IMAGE_GRAYSCALE);

// Preparing keypoints using detector
detector.detect(imageForTraining, keypoints);

// now getting the DESCRIPTORS for the given keypoints
cvDescExt->compute(imageForTraining, keypoints, descriptors);

//BOW
if(keypoints.size() > cluster) // so that (N<k) error won't come
{
if (!descriptors.empty()) bow.add(descriptors);

//VOCABULARY
vocabulary = bow.cluster();

cv::Ptr<DescriptorExtractor> extractor = new SurfDescriptorExtractor();
cv::Ptr<DescriptorMatcher> matcher = cv::DescriptorMatcher::create("FlannBased");
cv::BOWImgDescriptorExtractor descExtractor (extractor, matcher);

descExtractor.setVocabulary(vocabulary);
Mat bowDescriptors;
descExtractor.compute(imageForTraining, keypoints, bowDescriptors);
if ( !bowDescriptors.empty())
{
train.push_back(bowDescriptors);
response.push_back(label);
}

}// ending if loop

} // ending For loop
}// ending if(flag ) loop


if (flag == 2)
{
for(int i=1; i<=400; i++)
{
counter++;
cout<<"\n counter: "<<counter;

char filepath[255];
sprintf(filepath, "class2/%d.JPG",i); // we need class1/1.JPG etc

imageForTraining = cv::imread(filepath, CV_LOAD_IMAGE_GRAYSCALE);

// Preparing keypoints using detector
detector.detect(imageForTraining, keypoints);

// now getting the DESCRIPTORS for the given keypoints
cvDescExt->compute(imageForTraining, keypoints, descriptors);

//BOW
if(keypoints.size() > cluster) // so that (N<k) error won't come
{
if (!descriptors.empty()) bow.add(descriptors);

//VOCABULARY
vocabulary = bow.cluster();

cv::Ptr<DescriptorExtractor> extractor = new SurfDescriptorExtractor();
cv::Ptr<DescriptorMatcher> matcher = cv::DescriptorMatcher::create("FlannBased");
cv::BOWImgDescriptorExtractor descExtractor (extractor, matcher);

descExtractor.setVocabulary(vocabulary);
Mat bowDescriptors;
descExtractor.compute(imageForTraining, keypoints, bowDescriptors);
if ( !bowDescriptors.empty())
{
train.push_back(bowDescriptors);
response.push_back(label);
}

}// ending if loop

} // ending For loop
}// ending if(flag ) loop

....Similary for Class-3

}

最佳答案

您应该在词典中包含所有词汇。
这意味着您应该一次循环播放所有图片。

关于opencv - 使用功能包进行分类的词汇/字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21317545/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com