OpenCV - C++ 代码在 Eclipse 中运行,但不在终端中运行
OpenCV - C++ Code runs in Eclipse but not in terminal?
我正在尝试使Mohammad Reza Mostajabi(http://alum.sharif.ir/~mostajabi/Tutorial.html)的以下代码在Ubuntu 12.04和OpenCV 2.4.6.1下运行。我对包含的库进行了一些小的更改,并在启动主文件后立即添加了"cv::initModule_nonfree()"。
#include "cv.h"
#include "highgui.h"
#include "ml.h"
#include <stdio.h>
#include <iostream>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/nonfree/nonfree.hpp>
#include <vector>
using namespace cv;
using namespace std;
using std::cout;
using std::cerr;
using std::endl;
using std::vector;
char ch[30];
//--------Using SURF as feature extractor and FlannBased for assigning a new point to the nearest one in the dictionary
Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("FlannBased");
Ptr<DescriptorExtractor> extractor = new SurfDescriptorExtractor();
SurfFeatureDetector detector(500);
//---dictionary size=number of cluster's centroids
int dictionarySize = 1500;
TermCriteria tc(CV_TERMCRIT_ITER, 10, 0.001);
int retries = 1;
int flags = KMEANS_PP_CENTERS;
BOWKMeansTrainer bowTrainer(dictionarySize, tc, retries, flags);
BOWImgDescriptorExtractor bowDE(extractor, matcher);
void collectclasscentroids() {
IplImage *img;
int i,j;
for(j=1;j<=4;j++)
for(i=1;i<=60;i++){
sprintf( ch,"%s%d%s%d%s","train/",j," (",i,").jpg");
const char* imageName = ch;
img = cvLoadImage(imageName,0);
vector<KeyPoint> keypoint;
detector.detect(img, keypoint);
Mat features;
extractor->compute(img, keypoint, features);
bowTrainer.add(features);
}
return;
}
int main(int argc, char* argv[])
{
cv::initModule_nonfree();
int i,j;
IplImage *img2;
cout<<"Vector quantization..."<<endl;
collectclasscentroids();
vector<Mat> descriptors = bowTrainer.getDescriptors();
int count=0;
for(vector<Mat>::iterator iter=descriptors.begin();iter!=descriptors.end();iter++)
{
count+=iter->rows;
}
cout<<"Clustering "<<count<<" features"<<endl;
//choosing cluster's centroids as dictionary's words
Mat dictionary = bowTrainer.cluster();
bowDE.setVocabulary(dictionary);
cout<<"extracting histograms in the form of BOW for each image "<<endl;
Mat labels(0, 1, CV_32FC1);
Mat trainingData(0, dictionarySize, CV_32FC1);
int k=0;
vector<KeyPoint> keypoint1;
Mat bowDescriptor1;
//extracting histogram in the form of bow for each image
for(j=1;j<=4;j++)
for(i=1;i<=60;i++){
sprintf( ch,"%s%d%s%d%s","train/",j," (",i,").jpg");
const char* imageName = ch;
img2 = cvLoadImage(imageName,0);
detector.detect(img2, keypoint1);
bowDE.compute(img2, keypoint1, bowDescriptor1);
trainingData.push_back(bowDescriptor1);
labels.push_back((float) j);
}
//Setting up SVM parameters
CvSVMParams params;
params.kernel_type=CvSVM::RBF;
params.svm_type=CvSVM::C_SVC;
params.gamma=0.50625000000000009;
params.C=312.50000000000000;
params.term_crit=cvTermCriteria(CV_TERMCRIT_ITER,100,0.000001);
CvSVM svm;
printf("%sn","Training SVM classifier");
bool res=svm.train(trainingData,labels,cv::Mat(),cv::Mat(),params);
cout<<"Processing evaluation data..."<<endl;
Mat groundTruth(0, 1, CV_32FC1);
Mat evalData(0, dictionarySize, CV_32FC1);
k=0;
vector<KeyPoint> keypoint2;
Mat bowDescriptor2;
Mat results(0, 1, CV_32FC1);;
for(j=1;j<=4;j++)
for(i=1;i<=60;i++){
sprintf( ch,"%s%d%s%d%s","eval/",j," (",i,").jpg");
const char* imageName = ch;
img2 = cvLoadImage(imageName,0);
detector.detect(img2, keypoint2);
bowDE.compute(img2, keypoint2, bowDescriptor2);
evalData.push_back(bowDescriptor2);
groundTruth.push_back((float) j);
float response = svm.predict(bowDescriptor2);
results.push_back(response);
}
//calculate the number of unmatched classes
double errorRate = (double) countNonZero(groundTruth- results) / evalData.rows;
printf("%s%f","Error rate is ",errorRate);
return 0;
}
完成此操作后,我可以毫无问题地编译代码。我也可以在 Eclipse 中运行它,但是一旦我尝试让它在终端中工作,我就会收到以下错误消息:
" OpenCV 错误:在 add、file/home/mark/Downloads/FP/opencv-2.4.6.1/modules/features2d/src/bagofwords.cpp,第 57 行中断言_descriptors失败在抛出"cv::异常"实例后终止调用 what():/home/mark/Downloads/FP/opencv-2.4.6.1/modules/features2d/src/bagofwords.cpp:57: 错误: (-215) !_descriptors.empty() 在函数中添加"
几天来我一直在尝试解决问题,但我无法摆脱这个错误。我也尝试使用CodeBlocks来做到这一点,这给了我同样的错误。我将非常感谢一些帮助!
谢谢!
您的程序可能无法加载输入图像(从终端窗口启动时),因为它找不到它们。确保将输入图像复制到从中运行应用程序的目录。Eclipse 可能具有不同的主目录,因此当程序在 Eclipse 中启动时,它会看到图像。
相关文章:
- 如何在linux终端中同时编译和运行c++代码
- 使用 ubuntu 终端在 c++ 上运行线程类的问题
- Mac os x app bundle 在下载并运行时崩溃,但在终端或更改 Info.plist 时运行良好
- 如何在不丢失输出的情况下杀死/停止终端中正在运行的进程?
- 在终端中运行可执行文件与在Mac查找器中打开
- 终端命令在程序运行后自动打开图形
- 使用 (cin) 用户输入将其粘贴到 std::system 中,并在另一个终端中运行带有输入的命令
- 在集成终端上运行vscode lldb调试器时,如何获取程序的输出?
- 如何在调试模式下通过终端运行可执行的 c++?
- 如何在Visual Studio Code的终端内运行C++代码?
- 如何从终端运行用 eclipse 编写的 c++ 程序
- 我想在终端窗口中运行我的 CLion 程序
- 无法在终端中的 Windows 5.10.1 上运行 qt10.1 的编译器'cl'
- 崇高文本 3 - 在终端中编译并运行 C++ 程序(路径包含空格)
- 如何在不将其连接到终端的情况下运行'screen',以便我可以在C++ (qt) 应用程序中捕获输出?
- 如何通过终端在Macbook上运行基于GUI的程序
- macOS 差异在“访达”和“终端”中运行应用程序
- 从 eclipse IDE 运行工作正常,但在从终端运行时终止
- 如何停止任务运行终端在程序以视觉工作室代码结束后自动退出
- 通过QT运行终端