Sensor image convert and comparision using opencv

asked 2016-10-17 10:04:20 -0500

rasoo gravatar image

updated 2016-10-20 08:40:02 -0500

Hello All, I want to compare two(or more) consecutive image to detect movement within a range. For example, if there is anything in front of kinect camera within 60cm then it will detect and then just tell me if the object is moving or stationary. I repeat, I do not need to track any object, do not need to detect any pattern. Just need ato know if anything is moving infront of the camera within a range. I am using ros indigo, ubuntu 14.04 and having kinect cam. I am subscribing the topic /camera/depth/image_raw and getting image of type sensor_msgs::Image. So now I want to compare the two image. If there is a difference in the images then the decision is that there is something moving in front of the camera. I want to use opencv image comparison feature.

edit 1: I used another node which subscribed /camera/depth/points , and from there i detected if there is something in front of camera within 60cm range. Then i published a boolean flag. Using this node below I was trying to know if there is something within the range, it will tell us if the object is static or moving. But now I want to do it in the same node below as I came to know /camera/depth/image_raw gives image with depth data and I can get it using opencv function int depth = image_cur.at<short int="">(cv::Point(j,i));

but somehow I am not getting it exactly. Like if there is nothing moving in front of camera, i get the response something is moving. Because in each frame there is difference of values. My question is, if nothing is moving infront of camera, then why the data array are not identical for sequential images ?

I am going to update the codes below,

#include <ros ros.h=""> #include <std_msgs bool.h=""> #include "std_msgs/String.h" #include "geometry_msgs/Twist.h" #include <sensor_msgs laserscan.h=""> #include <pcl_ros point_cloud.h=""> #include <pcl point_types.h=""> #include <boost foreach.hpp=""> #include <pcl point_types.h=""> #include <pcl filters="" passthrough.h=""> #include <stdio.h>

#include <image_transport image_transport.h=""> #include <opencv2 opencv.hpp=""> #include <opencv2 highgui="" highgui.hpp=""> #include <cv_bridge cv_bridge.h=""> #include <sensor_msgs image_encodings.h=""> #include <opencv2 imgproc="" imgproc.hpp="">

//Store all constants for image encodings in the enc namespace to be used later. namespace enc = sensor_msgs::image_encodings;

using namespace::std; using namespace cv;

sensor_msgs::Image img_previous, img_current, img_primary; std_msgs::Bool check_moving_obj , obstacle_found, is_moving_obs; bool check_moving_obs= true;

ros::Publisher pub, dec_pub;

double scale_linear_, scale_angular_; void dec_callback(const std_msgs::Bool::ConstPtr& msg) { check_moving_obs = msg->data; } void callback(const sensor_msgs::Image::ConstPtr& msg){

 if(check_moving_obs)
 {
     if(img_previous.data.size() == 0)
     {
         img_previous = *msg;
         img_primary = *msg;
     }
     else
         {img_previous = img_current;}
    img_current = *msg;

    cout<<"------------------------current image info :---------:"<<img_current.data.size()<<"\n";
    cout<<"------------------------Height :---------:"<<img_current.height<<"\n";
    cout<<"------------------------Width :---------:" <<img_current.width<<"\n";

    cout<<"------------------------previous image info :---------:"<<img_previous.data.size()<<"\n";
    cout<<"------------------------Height :---------:"<<img_previous.height<<"\n";
    cout<<"------------------------Width :---------:" <<img_previous.width<<"\n";


        //Convert from the ROS image message to ...
(more)
edit retag flag offensive close merge delete

Comments

Organise your question better so it is easy for us to read and help you

patrchri gravatar imagepatrchri ( 2016-10-17 16:54:34 -0500 )edit
1

It looks like you aren't including cv_bridge in your CMakeLists.txt- it should be in find_package. You could post your CMakeLists.txt here as well.

lucasw gravatar imagelucasw ( 2016-10-18 15:33:55 -0500 )edit

Thanks @lucasw for your important observation. I added the line find_package(OpenCV 2 REQUIRED) But now its a new problem. My source is not building and it is hang. I have to restart my laptop. I guess now the installed version is different from openCV 2. But I cuold not solve the issue yet.

rasoo gravatar imagerasoo ( 2016-10-18 20:25:22 -0500 )edit

Find catkin and any catkin packages

find_package(catkin REQUIRED COMPONENTS roscpp rospy std_msgs genmsg message_generation pcl_conversions pcl_ros tf
) find_package(PCL REQUIRED) ##find_package(OpenCV 2 REQUIRED)

rasoo gravatar imagerasoo ( 2016-10-18 20:25:37 -0500 )edit

Adding cv_bridge to the other find_package catkin components will include opencv, it doesn't need to be in a separate find_package.

lucasw gravatar imagelucasw ( 2016-10-19 07:02:10 -0500 )edit

Thank you once again for your valuable response. I changed it as per your suggestion. But still the program is not compiling. Can you please have look at the part below in the next comment and please suggest if i need any change. Because listener_depthImage_comparer.cpp is the file where i used it

rasoo gravatar imagerasoo ( 2016-10-19 08:08:04 -0500 )edit

add_executable(listener_depthImage_compare src/listener_depthImage_compare.cpp) target_link_libraries(listener_depthImage_compare ${catkin_LIBRARIES} ) add_dependencies(listener_depthImage_compare beginner_tutorials_generate_messages_cpp)

I am confused about the add_dependecies section.

rasoo gravatar imagerasoo ( 2016-10-19 08:08:43 -0500 )edit

generate_messages(DEPENDENCIES std_msgs) find_package(catkin REQUIRED COMPONENTS sensor_msgs cv_bridge image_transport
roscpp rospy std_msgs genmsg message_generation pcl_conversions pcl_ros tf
)

rasoo gravatar imagerasoo ( 2016-10-19 08:12:52 -0500 )edit