我正在尝试使用OpenCV的跟踪API。我按照这里的说明完成了OpenCV的制作:https://github.com/itseez/opencv_contrib/。在建筑时我不得不关闭CMake gui中的一些参数。在make之后,我使用Tracking API运行以下代码:
#include <opencv2/core/utility.hpp>
#include <opencv2/video/tracking.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <tracker.hpp>
#include <iostream>
#include <cstring>
using namespace std;
using namespace cv;
static Mat image;
static Rect2d boundingBox;
static bool paused;
static bool selectObject = false;
static bool startSelection = false;
static void onMouse( int event, int x, int y, int, void* )
{
if( !selectObject )
{
switch ( event )
{
case EVENT_LBUTTONDOWN:
//set origin of the bounding box
startSelection = true;
boundingBox.x = x;
boundingBox.y = y;
boundingBox.width = boundingBox.height = 0;
break;
case EVENT_LBUTTONUP:
//sei with and height of the bounding box
boundingBox.width = std::abs( x - boundingBox.x );
boundingBox.height = std::abs( y - boundingBox.y );
paused = false;
selectObject = true;
break;
case EVENT_MOUSEMOVE:
if( startSelection && !selectObject )
{
//draw the bounding box
Mat currentFrame;
image.copyTo( currentFrame );
rectangle( currentFrame, Point((int) boundingBox.x, (int)boundingBox.y ), Point( x, y ), Scalar( 255, 0, 0 ), 2, 1 );
imshow( "Tracking API", currentFrame );
}
break;
}
}
}
//
// Hot keys:
// q - quit the program
// p - pause video
//
int main( int argc, char** argv )
{
//open the capture
VideoCapture cap(0);
if( !cap.isOpened() )
{
return -1;
}
//
// "MIL", "BOOSTING", "MEDIANFLOW", "TLD"
//
string tracker_algorithm = "MIL";
if ( argc>1 ) tracker_algorithm = argv[1];
Mat frame;
paused = false;
namedWindow( "Tracking API", 0 );
setMouseCallback( "Tracking API", onMouse, 0 );
Ptr<Tracker> tracker = Tracker::create( tracker_algorithm );
if( tracker == NULL )
{
cout << "***Error in the instantiation of the tracker...***\n";
return -1;
}
//get the first frame
cap >> frame;
frame.copyTo( image );
imshow( "Tracking API", image );
bool initialized = false;
int frameCounter = 0;
for ( ;; )
{
char c = (char) waitKey( 2 );
if( c == 'q' || c == 27 )
break;
if( c == 'p' )
paused = !paused;
if ( !paused )
{
cap >> frame;
if(frame.empty())
{
break;
}
frame.copyTo( image );
if( selectObject )
{
if( !initialized )
{
//initializes the tracker
if( !tracker->init( frame, boundingBox ) )
{
cout << "***Could not initialize tracker...***\n";
return -1;
}
initialized = true;
}
else
{
//updates the tracker
if( tracker->update( frame, boundingBox ) )
{
rectangle( image, boundingBox, Scalar( 255, 0, 0 ), 2, 1 );
}
}
}
imshow( "Tracking API", image );
frameCounter++;
}
}
return 0;
}
但是我收到一个错误,即在链接时没有定义类跟踪器的功能。这是我的构建日志的一部分:
g ++。exe -LC:\ opencv \ min_bin \ install \ x64 \ mingw \ lib -o bin \ Debug \ main_project.exe obj \ Debug \ t1.o C:\ opencv \ min_bin \ install \ x64 \ mingw \ lib \ libopencv_bgsegm300.dll.a C:\ opencv \ min_bin \ install \ x64 \ mingw \ lib \ libopencv_bioinspired300.dll.a C:\ opencv \ min_bin \ install \ x64 \ mingw \ lib \ libopencv_calib3d300.dll.a C:\ opencv \ min_bin \ install \ x64 \ mingw \ lib \ libopencv_ccalib300.dll.a C:\ opencv \ min_bin \ install \ x64 \ mingw \ lib \ libopencv_core300.dll.a C:\ opencv \ min_bin \ install \ x64 \ mingw \ lib \ libopencv_dnn300.dll.a C:\ opencv \ min_bin \ install \ x64 \ mingw \ lib \ libopencv_dpm300.dll.a C:\ opencv \ min_bin \ install \ x64 \ mingw \ lib \ libopencv_face300.dll.a C:\ opencv \ min_bin \ install \ x64 \ mingw \ lib \ libopencv_features2d300.dll.a C:\ opencv \ min_bin \ install \ x64 \ mingw \ lib \ libopencv_flann300.dll.a C:\ opencv \ min_bin \ install \ x64 \ mingw \ lib \ libopencv_fuzzy300.dll.a C:\ opencv \ min_bin \ install \ x64 \ mingw \ lib \ libopencv_hal300.a C:\ opencv \ min_bin \ install \ x64 \ mingw \ lib \ libopencv_highgui300.dll.a C:\ opencv \ min_bin \安装\ 64 \ MinGW的\ LIB \ libopencv_ imgcodecs300.dll.a C:\ opencv \ min_bin \ install \ x64 \ mingw \ lib \ libopencv_imgproc300.dll.a C:\ opencv \ min_bin \ install \ x64 \ mingw \ lib \ libopencv_ml300.dll.a C:\ opencv \ min_bin \ install \ x64 \ mingw \ lib \ libopencv_objdetect300.dll.a C:\ opencv \ min_bin \ install \ x64 \ mingw \ lib \ libopencv_photo300.dll.a C:\ opencv \ min_bin \ install \ x64 \ mingw \ lib \ libopencv_plot300.dll.a C:\ opencv \ min_bin \ install \ x64 \ mingw \ lib \ libopencv_reg300.dll.a C:\ opencv \ min_bin \ install \ x64 \ mingw \ lib \ libopencv_rgbd300.dll.a C:\ opencv \ min_bin \ install \ x64 \ mingw \ lib \ libopencv_saliency300.dll.a C:\ opencv \ min_bin \ install \ x64 \ mingw \ lib \ libopencv_shape300.dll.a C:\ opencv \ min_bin \ install \ x64 \ mingw \ lib \ libopencv_stereo300.dll.a C:\ opencv \ min_bin \ install \ x64 \ mingw \ lib \ libopencv_stitching300.dll.a C:\ opencv \ min_bin \ install \ x64 \ mingw \ lib \ libopencv_structured_light300.dll.a C:\ opencv \ min_bin \ install \ x64 \ mingw \ lib \ libopencv_superres300.dll.a C:\ opencv \ min_bin \ install \ x64 \ mingw \ lib \ libopencv_surface_matching300.dll.a C:\ opencv \ min_bin \ install \ x64 \ mingw \ lib \ libopencv_text300.dll.a C:\ opencv \ min_bin \ install \ x64 \ mingw \ lib \ libopencv_ts300.a C:\ opencv \ min_bin \ install \ x64 \ mingw \ lib \ libopencv_video300.dll.a C:\ opencv \ min_bin \ install \ x64 \ mingw \ lib \ libopencv_videoio300.dll.a C:\ opencv \ min_bin \ install \ x64 \ mingw \ lib \ libopencv_videostab300.dll.a C:\ opencv \ min_bin \ install \ x64 \ mingw \ lib \ libopencv_xobjdetect300 .dll.a obj \ Debug \ t1.o:在函数
main': E:/main_proj/main_project/t1.cpp:79: undefined reference to
cv :: Tracker :: create(cv :: String const&amp;)&#39; E:/main_proj/main_project/t1.cpp:116:未定义引用cv::Tracker::init(cv::Mat const&, cv::Rect_<double> const&)' E:/main_proj/main_project/t1.cpp:126: undefined reference to
cv :: Tracker :: update(cv :: Mat const&amp;,cv :: Rect_&amp;)&#39;
我认为这是在制作OpenCV期间出现的一些问题。有人可以提出解决方案吗?
答案 0 :(得分:0)
与-L
(带库的目录路径)选项链接时,如
-LC:\opencv\min_bin\install\x64\mingw\lib
您也应该使用-l
链接到特定的库。
似乎找不到-lopencv_tracking300
库。
答案 1 :(得分:0)
我重建了整套模块,现在它可以工作了。我仍然不确定之前到底发生了什么。在成功构建之前,您需要进行多次实验。非常感谢您的建议 - @berak和@John