关于VISP其他前提内容见其他相关文章: AprilTag-VISP
***首先,想要用这个类,必须包含头文件:***#include <visp3/detection/vpDetectorAprilTag.h>
具体的公共成员和函数可以从下面下载: VISP库的开发文件
重要的说明:
- Base class for AprilTag detector. This class is a wrapper over
AprilTag. There is no need to download and install AprilTag from source code or existing pre-built packages since the source code is embedded in ViSP。(没有必要再下载AprilTag源码) - The detect() function allows to detect multiple tags in an image。(这个函数可以一次性从一张图片中读出多个AprilTag码)
- The detect() function allows to detect multiple tags in an image. Once detected, for each tag it is possible to retrieve the location of the corners using getPolygon(), the encoded message using getMessage(), the bounding box using getBBox() and the center of gravity using getCog().(?detect()函数允许检测图像中的多个标签。??检测到后,每个标记都有可能使用 getPolygon()函数检索角的位置??()??,使用??getMessage()函数??编码消息,使用??getBBox()获取界框,使用??getCog()得到?的重心。)
- If camera parameters and the size of the tag are provided, you can also estimate the 3D pose of the tag in terms of position and orientation wrt the camera considering 2 cases:(如果已知码的大小,我们就可以检测出码的3D状态,有一下两种情况)
detect(const
vpImage<unsigned char> &, const double, const vpCameraParameters
&, std::vector<vpHomogeneousMatrix> &)
getPose()
下面都是一些举例:
- The following sample code shows how to use this class to detect the
location of 36h11 AprilTag patterns in an image.(下面是一个例子:在一张图片中检测36h11 AprilTag)
#include <visp3/detection/vpDetectorAprilTag.h>
#include <visp3/io/vpImageIo.h>
int main()
{
#ifdef VISP_HAVE_APRILTAG
vpImage<unsigned char> I;
vpImageIo::read(I, "image-tag36h11.pgm");
vpDetectorAprilTag detector(vpDetectorAprilTag::TAG_36h11);
bool status = detector.detect(I);
if (status) {
for(size_t i=0; i < detector.getNbObjects(); i++) {
std::cout << "Tag code " << i << ":" << std::endl;
std::vector<vpImagePoint> p = detector.getPolygon(i);
for(size_t j=0; j < p.size(); j++)
std::cout << " Point " << j << ": " << p[j] << std::endl;
std::cout << " Message: \"" << detector.getMessage(i) << "\"" << std::endl;
}
}
#endif
}
上面的例子应该会出现这种结果:
Tag code 0:
Point 0: 124.008, 442.226
Point 1: 194.614, 441.237
Point 2: 184.833, 540.386
Point 3: 111.948, 533.634
Message: "36h11 id: 0"
Tag code 1:
Point 0: 245.327, 438.801
Point 1: 338.116, 437.221
Point 2: 339.341, 553.539
Point 3: 238.954, 543.855
Message: "36h11 id: 1"
- This other example shows how to estimate the 3D pose of 36h11 AprilTag patterns considering that all the tags have the same size (in our example 0.053 m).(下面的例子展示了怎么得到AprilTag的3D姿态)
#include <visp3/detection/vpDetectorAprilTag.h>
#include <visp3/io/vpImageIo.h>
int main()
{
#ifdef VISP_HAVE_APRILTAG
vpImage<unsigned char> I;
vpImageIo::read(I, "image-tag36h11.pgm");
vpDetectorAprilTag detector(vpDetectorAprilTag::TAG_36h11);
std::vector<vpHomogeneousMatrix> cMo;
vpCameraParameters cam;
cam.initPersProjWithoutDistortion(615.1674805, 615.1675415, 312.1889954, 243.4373779);
double tagSize = 0.053;
bool status = detector.detect(I, tagSize, cam, cMo);
if (status) {
for(size_t i=0; i < detector.getNbObjects(); i++) {
std::cout << "Tag number " << i << ":" << std::endl;
std::cout << " Message: \"" << detector.getMessage(i) << "\"" << std::endl;
std::cout << " Pose: " << vpPoseVector(cMo[i]).t() << std::endl;
std::size_t tag_id_pos = detector.getMessage(i).find("id: ");
if (tag_id_pos != std::string::npos) {
std::string tag_id = detector.getMessage(i).substr(tag_id_pos + 4);
std::cout << " Tag Id: " << tag_id << std::endl;
}
}
}
#endif
}
上面的代码会得到以下的结果:
Tag number 0:
Message: "36h11 id: 0"
Pose: 0.1015061088 -0.05239057228 0.3549037285 1.991474322 2.04143538 -0.9412360063
Tag Id: 0
Tag number 1:
Message: "36h11 id: 1"
Pose: 0.08951250829 0.02243780207 0.306540622 1.998073197 2.061488008 -0.8699567948
Tag Id: 1
- In this other example we estimate the 3D pose of 36h11 AprilTag patterns considering that tag 36h11 with id 0 (in that case the tag message is “36h11 id: 0”) has a size of 0.040 m, while all the others have a size of 0.053m.(下面这个例子会举例如果码不一样大,id 0比较小)
#include <visp3/detection/vpDetectorAprilTag.h>
#include <visp3/io/vpImageIo.h>
int main()
{
#ifdef VISP_HAVE_APRILTAG
vpImage<unsigned char> I;
vpImageIo::read(I, "image-tag36h11.pgm");
vpDetectorAprilTag detector(vpDetectorAprilTag::TAG_36h11);
vpHomogeneousMatrix cMo;
vpCameraParameters cam;
cam.initPersProjWithoutDistortion(615.1674805, 615.1675415, 312.1889954, 243.4373779);
double tagSize_id_0 = 0.04;
double tagSize_id_others = 0.053;
bool status = detector.detect(I);
if (status) {
for(size_t i=0; i < detector.getNbObjects(); i++) {
std::cout << "Tag code " << i << ":" << std::endl;
std::cout << " Message: \"" << detector.getMessage(i) << "\"" << std::endl;
if (detector.getMessage(i) == std::string("36h11 id: 0")) {
if (! detector.getPose(i, tagSize_id_0, cam, cMo)) {
std::cout << "Unable to get tag index " << i << " pose!" << std::endl;
}
}
else {
if (! detector.getPose(i, tagSize_id_others, cam, cMo)) {
std::cout << "Unable to get tag index " << i << " pose!" << std::endl;
}
}
std::cout << " Pose: " << vpPoseVector(cMo).t() << std::endl;
}
}
#endif
}
运行完这段代码,将会得到下面的结果:
Tag code 0:
Message: "36h11 id: 0"
Pose: 0.07660838403 -0.03954005455 0.2678518706 1.991474322 2.04143538 -0.9412360063
Tag code 1:
Message: "36h11 id: 1"
Pose: 0.08951250829 0.02243780207 0.306540622 1.998073197 2.061488008 -0.8699567948
其他还有很多例子,需要一个一个得看和理解,再应用吧。
|