Movement Detection with Sequential Image comparison

asked 2016-03-27 15:19:05 -0500

PKumars gravatar image

updated 2016-03-27 16:31:52 -0500

Dear all, i'm trying to detect object movement using sequential image comparison in ROS. But there is some bug and I'm unable to find. here is my code snippet in detail... I want to know that is this the correct way to detect movement using sequential image comparison?

void callback(const ImageConstPtr& image_rgb, const ImageConstPtr& image_depth_source) { pcl::StopWatch watch;

//some boolean variables for added functionality
bool objectDetected = false;
//these two can be toggled by pressing 'd' or 't'
bool debugMode = false;
bool trackingEnabled = false;
//pause and resume code
bool pause = false;
//set up the matrices that we will need
//the two frames we will be comparing
Mat frame1,frame2;
//their grayscale images (needed for absdiff() function)
Mat grayImage1,grayImage2;
//resulting difference image
Mat differenceImage;
//thresholded difference image (for use in findContours() function)
Mat thresholdImage;

// Solve all of perception here... (ROS Format converted to OpenCV format here)
cv::Mat image_color = cv_bridge::toCvCopy(image_rgb)->image;
cv::Mat image_depth = cv_bridge::toCvCopy(image_depth_source)->image;

// publisher definition
std_msgs::Float32MultiArray dataArray;
// data clear 
dataArray.data.clear();

int count;

if (count<10)
{
    image_color.copyTo(frame1);
    count++;
    return;
}
else
{
    image_color.copyTo(frame2);
}

//convert frame1 to gray scale for frame differencing
cv::cvtColor(frame1,grayImage1,COLOR_BGR2GRAY);

//convert frame2 to gray scale for frame differencing
cv::cvtColor(frame2,grayImage2,COLOR_BGR2GRAY);

//perform frame differencing with the sequential images. 
cv::absdiff(grayImage1,grayImage2,differenceImage);

//threshold intensity image at a given sensitivity value
cv::threshold(differenceImage,thresholdImage,SENSITIVITY_VALUE,255,THRESH_BINARY);

//show the difference image and threshold image
cv::imshow("Difference Image",differenceImage);
cv::imshow("Threshold Image", thresholdImage);

imshow("Frame1",frame1);

frame2.copyTo(frame1);


cout<<count<<endl;
// for image refreshing 
cv::waitKey(1);

}

int main(int argc, char** argv) { ros::init(argc, argv, "vision_node");

ros::NodeHandle nh;

// topic subscription
message_filters::Subscriber<Image> RGB_sub(nh, "/camera/rgb/image_color", 1);
message_filters::Subscriber<Image> DEPTH_sub(nh, "/camera/depth/image_raw", 1);

// synchronization policy
typedef sync_policies::ApproximateTime<Image,Image> MySyncPolicy;

// ApproximateTime takes a queue size as its constructor argument, hence MySyncPolicy(10)
Synchronizer<MySyncPolicy> sync(MySyncPolicy(10), RGB_sub, DEPTH_sub);
sync.registerCallback(boost::bind(&callback, _1, _2));

// data publish for further processing 
pubData = nh.advertise<std_msgs::Float32MultiArray>("pubData", 10);

ros::spin();
return 0;

}

I'll be a great help if anyone can guide me in solving this problem.

Regards, Prasanna

edit retag flag offensive close merge delete

Comments

You expect that you only give the hint "there is some bug" and someone is reading through all your code and tries to find it? What happens? What should happen? How does your data look like?

NEngelhard gravatar image NEngelhard  ( 2016-03-27 15:37:06 -0500 )edit

I'm really sorry for that. I have edited that one. Thanks for pointing the mistake

PKumars gravatar image PKumars  ( 2016-03-27 16:15:21 -0500 )edit

That's already a lot more readable. Still: "What happens? What should happen? How does your data look like?"

NEngelhard gravatar image NEngelhard  ( 2016-03-27 16:55:48 -0500 )edit

Sorry again, here I'm trying to trace movement of any object from sequential frames. but there is something wrong and even i'm unable to see the image. I want to know is this the correct way of taking frames in ROS using Kinect or OpenNI supported cameras.

PKumars gravatar image PKumars  ( 2016-03-28 05:28:07 -0500 )edit