ROS 移植KCF代码
首先说明一下,melodic版本支持python2.7, 因此python版本的kcf没有尝试过,3.6版本的python可以直接调用
安装过opencv_contrib3.4.1的c++库,但是ros中会出现冲突,所以放弃了,直接使用自带的opencv3.2版本。使用kcf的源码进行编译。链接
如上图所示,是整个kcf包的源码,我们需要修改runtracker.pp的源码
#include <ros/ros.h>
#include <stdio.h>
#include <image_transport/image_transport.h>
#include <cv_bridge/cv_bridge.h>
#include <sensor_msgs/image_encodings.h>
#include <geometry_msgs/Twist.h>
//***split****************************
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <kcftracker.hpp>
using namespace std;
using namespace cv;
bool drawing_box = false;
bool gotBB = false;
cv::Rect box;//画出的矩形框
static const char WINDOW[] = "Image window";
//画矩形框
void mouseHandler(int event, int x, int y, int flags, void *param){
switch (event){
case CV_EVENT_MOUSEMOVE:
if (drawing_box){
box.width = x - box.x;
box.height = y - box.y;
}
break;
case CV_EVENT_LBUTTONDOWN:
drawing_box = true;
box = Rect(x, y, 0, 0);
break;
case CV_EVENT_LBUTTONUP:
drawing_box = false;
if (box.width < 0){
box.x += box.width;
box.width *= -1;
}
if (box.height < 0){
box.y += box.height;
box.height *= -1;
}
gotBB = true; //已经获得bounding box
break;
}
}
void drawBox(Mat& image, CvRect box, Scalar color, int thick){
rectangle(image, cvPoint(box.x, box.y), cvPoint(box.x + box.width, box.y + box.height), color, thick);
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "image_converter");
VideoCapture capture;
capture.open(0);
cvNamedWindow("KCF", CV_WINDOW_AUTOSIZE);
cvSetMouseCallback("KCF", mouseHandler, NULL); //用鼠标选中初始目标的bounding box
bool HOG = true;
bool FIXEDWINDOW = false;
bool MULTISCALE = true;
bool SILENT = false;
bool LAB = false ;
// Create KCFTracker object
KCFTracker tracker(HOG, FIXEDWINDOW, MULTISCALE, LAB);
Rect result;
Mat frame;
double duration = 0;
int count = 0;
while(!gotBB)
{
capture>>frame;
drawBox(frame, box, (0, 0, 255), 2); //把bounding box 画出来
imshow("KCF", frame);
if (cvWaitKey(33) == 'q')
return 0;
}
cvSetMouseCallback("KCF", NULL, NULL); //如果已经获得第一帧用户框定的box了,就取消鼠标响应
printf("Initial Bounding Box = x:%d y:%d h:%d w:%d\n", box.x, box.y, box.width, box.height);
tracker.init(box, frame);//初始化追踪框
while (capture.read(frame))
{
auto t_start = clock();
result = tracker.update(frame);//追踪
//画矩形框
auto t_end = clock();
duration += (double)(t_end - t_start) / CLOCKS_PER_SEC;
//求FPS:
cout << "FPS: " << count / duration << "\n";
count++;
rectangle(frame, Point(result.x, result.y), Point(result.x + result.width, result.y + result.height), Scalar(0, 255, 255), 1, 8);
if (!SILENT) {
imshow("KCF", frame);
waitKey(1);
}
}
//Reading an image from the file
cv::Mat cv_image ;
capture.read(cv_image);
//cv::imread("/home/jsy/Desktop/test.png");
if(cv_image.empty() )
{
ROS_ERROR("Read the picture failed!");
return -1;
}
//Convert OpenCV image to ROS message
ros::NodeHandle node;
image_transport::ImageTransport transport(node);
image_transport::Publisher image_pub;
image_pub=transport.advertise("OutImage", 1);
ros::Time time=ros::Time::now();
cv_bridge::CvImage cvi;
cvi.header.stamp = time;
cvi.header.frame_id = "image";
cvi.encoding = "bgr8";
cvi.image = cv_image;
sensor_msgs::Image im;
cvi.toImageMsg(im);
image_pub.publish(im);
ROS_INFO("Converted Successfully!");
//Show the image
cv::namedWindow(WINDOW);
cv::imshow(WINDOW,cv_image);
cv::waitKey(0);
ros::spin();
return 0;
}
其实和c++的源码一样,没有什么区别,我直接从Windows上复制粘贴的。
重要的是CMakeLists.text 文件
找到包的位置
find_package(catkin REQUIRED COMPONENTS
geometry_msgs
roscpp
std_msgs
cv_bridge
image_transport
)
find_package(OpenCV REQUIRED)
添加库
include_directories(
#include
${catkin_INCLUDE_DIRS}
)
include_directories(${OpenCV_INCLUDE_DIRS})
FILE(GLOB_RECURSE sourcefiles "src/*.cpp")
file命令将kcf包的源码编译。
添加c++ library
include_directories(${CMAKE_SOURCE_DIR}/src/)
include_directories(${PROJECT_SOURCE_DIR}/src)
INCLUDE_DIRECTORIES( ${CMAKE_BINARY_DIR}/)
添加可执行文件
add_executable(kcf_node ${sourcefiles})
添加链接文件
target_link_libraries(kcf_node ${OpenCV_LIBRARIES})
target_link_libraries(kcf_node ${catkin_LIBRARIES})
ok!
版权声明:本文为qq_39290361原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。