" ‘selectROI’ was not declared in this scope "
Hello,
I am trying to use OpenCV's object tracker(s) and I am running into this error of selectROI not being found. I Looked at the other posts (Post 1 Post 2) and the solutions did not work for me.
I went ahead and tried implementing the same object tracking code outside ros/catkin and it worked perfectly. So I am thinking that its a catkin problem and was hoping to get some help with this! Here is my code:
#include ros/ros.h
#include image_transport/image_transport.h
#include cv_bridge/cv_bridge.h
#include sensor_msgs/image_encodings.h
#include opencv2/imgproc.hpp
#include opencv2/highgui.hpp
#include opencv2/opencv.hpp
#include opencv2/tracking.hpp
#include opencv2/core/ocl.hpp
#include iostream
#include cstring
using namespace cv;
using namespace std;
static const std::string OPENCV_WINDOW = "Image window";
#define SSTR( x ) static_cast< std::ostringstream & >( \
( std::ostringstream() << std::dec << x ) ).str()
class ObjectTracking
{
public:
ObjectTracking():nh_(ros::NodeHandle()),
nh_private_(ros::NodeHandle("~"))
{
// image_sub_ = nh_.subscribe("camera/rgb/image_raw",1,
// &ObjectTracking::imageCb,this);
cv::namedWindow(OPENCV_WINDOW);
// tracking();
}
~ObjectTracking()
{
cv::destroyWindow(OPENCV_WINDOW);
}
// ros::Subscriber image_sub_;
//
// void imageCb(const sensor_msgs::ImageConstPtr& msg);
void tracking()
{
cv::Rect2d roi;
cv::Mat frame;
cv::Ptr<tracker> tracker = TrackerKCF::create();
cv::VideoCapture cap(0);
cap >> frame;
if (frame.empty())
{
cout<<"Import Fail!!";
}
roi = selectROI(OPENCV_WINDOW,frame);
// cv::Rect2d roi(375, 244, 65, 150);
// cv::rectangle(frame, roi, Scalar( 255, 0, 0 ), 2, 1 );
if(roi.width == 0 || roi.height == 0)
cout<<"ROI Fail";
tracker->init(frame,roi);
cout << "Start the tracking process , press ESC to quit" << endl;
while(1)
{
cap >> frame;
if(frame.rows == 0 || frame.cols == 0)
break;
tracker->update(frame,roi);
rectangle(frame,roi,Scalar(255,0,0),2,1);
Point center_of_rect =(roi.br() + roi.tl()) * 0.5;
circle(frame,center_of_rect,3,Scalar(0,0,255));
putText(frame, "KCF Tracker", Point(100,20), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(50,170,50),2);
putText(frame,"X " + SSTR(double(center_of_rect.x)) + " Y " + SSTR(double(center_of_rect.y)), Point(100,50), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(255,0,50),2 );
imshow("tracker",frame);
if(waitKey(1) == 27)break;
}
}
private:
ros::NodeHandle nh_;
ros::NodeHandle nh_private_;
};
int main(int argc, char** argv)
{
ros::init(argc, argv, "object_track");
ObjectTracking ot;
ot.tracking();
ros::spin();
return 0;
}
CMakeLists:
cmake_minimum_required(VERSION 2.8.3)
project(object_tracking)
set(OpenCV_DIR /usr/local/share/OpenCV/)
find_package( OpenCV REQUIRED)
find_package(catkin REQUIRED COMPONENTS
geometry_msgs
image_transport
roscpp
rospy
sensor_msgs
std_msgs
cv_bridge
)
catkin_package()
include_directories(
include
${catkin_INCLUDE_DIRS}
${OpenCV_INCLUDE_DIRS}
)
LINK_DIRECTORIES(/usr/local/lib)
add_executable(obj_trk2
src/obj_track.cpp
)
target_link_libraries(obj_trk2
${catkin_LIBRARIES}
${OpenCV_LIBS}
/usr/local/lib/libopencv_tracking.so
)
Are you on Kinetic? Did you build opencv yourself?
I am on Kinetic and yes, I built it myself.