我正在尝试实现linemod算法以从OpenCV进行无纹理物体识别,但是在将模板(训练图像以有效匹配)添加到检测器时遇到了一些麻烦。
实例化主要检测器类的方法似乎有很多:
linemod::Detector detector; //empty constructor
linemod::Detector detector (Modalities, T_pyramid); //regular constructor
Ptr<linemod::Detector> detector
detector= linemod::getDefaultLINEMOD //Setting a pointer to a default version
然后添加模板:
int templ_id = detector.addTemplate(sources,class_id,mask)
int templ_id = detector->addTemplate(sources,class_id,mask)
我的问题是,除非我使用空的构造函数版本,否则模板添加失败,在这种情况下,它似乎可以正常工作。不幸的是,我不确定如何使用空构造函数,它似乎是围绕从文件加载而构建的。默认版本,或者我构建的版本,是我要使用的版本。
奇怪的一点是,即使模板加载失败,尝试添加模板也会将class_ID添加到检测器-这会在templ_id上返回-1,并且numTemplates的调用为0 ..但是numClasses从0开始并上升为1,即使模板无法添加。
我已经尝试了无休止的尝试,但似乎无法使其正常工作。我是c ++的新手,所以很可能会缺少一些明显的上下文或简单的解决方案。
这是检测器类以及所有其他linemod类,函数等的参考: https://docs.opencv.org/3.4/d7/d07/classcv_1_1linemod_1_1Detector.html
和完整代码:
//LINEMOD LOAD TEMPLATE TO DETECTOR CLASS
/*
Loosely based on http://answers.opencv.org/question/17424/opencv-cvlinemod-throwing-runtime-errors/
Code displays:
-Appropriate libraries
-How to instantiate the detector class from opencv
-How to load a template from file to the detector
Next stage is to load full set for one or many objects, or to test matching capability.
PW: 02/07/18
*/
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/core/utility.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/rgbd.hpp"
using namespace std;
using namespace cv;
string train_path = "/home/pete/work/data/train/15/rgb/0500.png";
string test_path = "/home/pete/work/data/test/";
string test_image = "/home/pete/work/pxw762/linemod/test_image.jpeg";
int main()
{
//Load in image(s)
Mat rgb;
Mat depth;
rgb = imread("/home/pete/work/data/train/15/rgb/0500.png", CV_LOAD_IMAGE_COLOR);
depth = imread("/home/pete/work/data/train/15/depth/0500.png", CV_LOAD_IMAGE_ANYDEPTH);
vector<Mat> sources;
sources.push_back(rgb);
sources.push_back(depth); //Put images into source vector
//Create mask
Mat gray, mask;
cvtColor(rgb,gray, CV_BGR2GRAY);
threshold(gray,mask,0,1,THRESH_BINARY);
mask.convertTo(mask, CV_8U);
//Add a template
cv::Ptr<cv::linemod::Detector> detector;
detector=linemod::getDefaultLINEMOD();
string class_id = "15";
int templ_ID = detector->addTemplate(sources, class_id, mask); //Add a template to the detector
cout<<templ_ID<<endl;
//Testing section
string num_classes= to_string(detector->numClasses());
string num_templates= to_string(detector->numTemplates());
cout<<"Number of classes: "<<num_classes<<endl;
cout<<"Number of Templates: "<<num_templates<<endl;
return 0;
}
有人对这里可能出什么问题有任何想法吗?
谢谢, 皮特