最近基于TX2平台做项目,需要用到opencv的VideoCapture功能,我在使用过程中遇到了标题中所描述的问题,在此记录下解决的方法。
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
eigen_conversions
message_generation
cv_bridge
image_transport
)
add_executable(rtsp_camera
src/network/rtsp_camera.cpp)
target_link_libraries(rtsp_camera
${OpenCV_LIBRARIES}
${catkin_LIBRARIES}
)
#include <opencv2/core/core.hpp>
#include <image_transport/image_transport.h>
#include <opencv2/highgui/highgui.hpp>
#include <cv_bridge/cv_bridge.h>
#include <opencv2/videoio/videoio.hpp>
#include <opencv2/video.hpp>
#include <opencv2/imgproc/imgproc.hpp>
void
RtspCamera::spin()
{
ros::Rate loop_rate(3);
//rtsp视频流地址
string filename = "rtsp://admin:admin@192.168.42.108:554/cam/realmonitor?channel=1&subtype=0";
Mat frame;
cv::VideoCapture cap;
cap.open(filename);
if (!cap.isOpened()) {
cerr << "打开视频流失败\n";
return;
}
while(ros::ok())
{
cap.read(frame);
if (frame.empty()) {
cerr << "未提取到视频帧\n";
break;
}
image_ = cv_bridge::CvImage(std_msgs::Header(), "rgb8", frame).toImageMsg();
image_raw_pub_.publish(&image_);
ros::spinOnce();
loop_rate.sleep();
}
}
- 编译发现报标题中所示错误,查看/usr/local/include/opencv2/videoio.hpp中有VideoCapture类,因此猜想可能没有找到相关静态链接库,因此在cmakelists文件中补充相关静态链接库,如下所示,问题解决
target_link_libraries(rtsp_camera
${OpenCV_LIBRARIES}
${catkin_LIBRARIES}
/usr/local/lib/libopencv_videoio.so
)
|