C++ Opencv c++;linemod-加载模板时出现问题

C++ Opencv c++;linemod-加载模板时出现问题,c++,opencv,C++,Opencv,我试图从OpenCV实现用于无纹理对象识别的linemod算法,但在将模板(训练图像以有效匹配)添加到检测器时遇到了一些问题 似乎有许多方法可以实例化主检测器类: linemod::Detector detector; //empty constructor linemod::Detector detector (Modalities, T_pyramid); //regular constructor Ptr<linemod::Detector> detector detect

我试图从OpenCV实现用于无纹理对象识别的linemod算法,但在将模板(训练图像以有效匹配)添加到检测器时遇到了一些问题

似乎有许多方法可以实例化主检测器类:

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
我的问题是模板添加失败,除非我使用空的构造函数版本,在这种情况下,它似乎工作正常。不幸的是,我不确定如何使用空构造函数,它似乎是围绕从文件加载构建的。我想要使用的是默认版本,或者我构建的版本

奇怪的一点是,即使模板加载失败,尝试添加模板也会将class_ID添加到检测器中-这将在temp_ID上返回-1,并且numTemplates的调用为0。。但是numClasses从0开始,上升到1,即使模板无法添加

我试着无休止地摆弄它,但似乎无法让它工作。我是C++新手,所以我可能缺少一些明显的上下文或简单的解决方案。 这是探测器类和所有其他linemod类、函数等的参考:

完整代码:

//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;
}
//LINEMOD将模板加载到检测器类
/*
松散地基于http://answers.opencv.org/question/17424/opencv-cvlinemod-throwing-runtime-errors/
代码显示:
-适当的图书馆
-如何从opencv实例化detector类
-如何将模板从文件加载到检测器
下一阶段是加载一个或多个对象的完整集合,或者测试匹配能力。
PW:02/07/18
*/
#包括
#包括“opencv2/core/core.hpp”
#包括“opencv2/core/utility.hpp”
#包括“opencv2/highgui.hpp”
#包括“opencv2/imgproc.hpp”
#包括“opencv2/rgbd.hpp”
使用名称空间std;
使用名称空间cv;
字符串train_path=“/home/pete/work/data/train/15/rgb/0500.png”;
字符串test_path=“/home/pete/work/data/test/”;
字符串test_image=“/home/pete/work/pxw762/linemod/test_image.jpeg”;
int main()
{
//加载图像(个)
Mat-rgb;
铺层深度;
rgb=imread(“/home/pete/work/data/train/15/rgb/0500.png”,CV\u LOAD\u IMAGE\u COLOR);
深度=imread(“/home/pete/work/data/train/15/depth/0500.png”,CV\u LOAD\u IMAGE\u ANYDEPTH);
矢量源;
来源。推回(rgb);
sources.push_back(depth);//将图像放入源向量
//创建遮罩
垫灰色,面具;
CVT颜色(rgb、灰色、CV_BGr2灰色);
阈值(灰度、掩模、0,1、阈值二元);
掩模转换器(掩模,CV_8U);
//添加模板
cv::Ptr检测器;
检测器=linemod::getDefaultLINEMOD();
字符串类_id=“15”;
int temp_ID=detector->addTemplate(sources,class_ID,mask);//向检测器添加模板

coutI也有类似的问题,在我的例子中,掩码出现了故障。有趣的是,如果我只是声明
Mat mask
并将其传递给addTemplate函数,它就可以工作了。你可以尝试一下,但实际上我认为你的看起来很好。也许你可以先尝试只使用ColorGradient模式,以进一步隔离问题?谢谢,伙计,我在不久前就解决了这个问题。实际上,这里有两个独立的问题导致了问题,两个问题都必须同时解决才能正常工作。一个是大小,因为算法针对800x600图像进行了优化(我认为),无法生成任何大小相差很大的匹配项……第二个,我真的记不得了!
//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;
}