OpenCV can't be used in ROS
I have been trouble by opencv problems. I have planed to transport my windows's code into ROS, which I have tested by local video files. I found that ROS doesn't support local video files, until ffmpeg has been installed. And Checking my ubuntu that it had been installed, I find ROS didn't support local video files neither.
I reinstall my ffmpeg and my opencv2.2, which I find my ROS doesn't support opencv unluckily. I test my older packages, such as gencam_cu. It all doesn't work, while it will works very well without opencv, like usb_cam. I think my ROS system maybe disorder. So I reinstall my ROS refer to ROS.
But also it doesn't work. Especially, when uses "cvCaptureFromCAM" it can't get frames information. Try to find the reason for this problem, but I am so disappointed. I have been bored for many days. Could some one give me helps?
Given a example, codes below
Class UsbCamNode {Public :
ros::NodeHandle &node_;
sensor_msgs::Image img_;
std::string video_device_name_;
std::string io_method_name_;
int image_width_, image_height_;
std::string pixel_format_name_;
ros::Time next_time_;
int count_;
IplImage* src;
IplImage* src_canny;
CvCapture* capture;
image_transport::Publisher image_pub;
UsbCamNode(ros::NodeHandle &node):node_(node)
{
src=NULL;
capture=NULL;
image_transport::ImageTransport it(node);
image_pub=it.advertise("image_raw",1);
int camera_num=0;
ros::NodeHandle nh("~");
nh.getParam("camera_index",camera_num);
printf("%d",camera_num);
if(NULL==(capture=cvCaptureFromCAM(camera_num)))
{
printf("\nError on cvCaptureFromCAM");
}
if(NULL==(src=cvQueryFrame(capture)))
{
printf("\nError on cvQueryFrame");
}
printf("Acquired image (%d/%d%)\n",src->width,src->height);
printf("Channels (%d)\n",src->nChannels);
printf("Depth (%d)\n",src->depth);
cvNamedWindow("Capture",CV_WINDOW_AUTOSIZE);
cvNamedWindow("Canny",CV_WINDOW_AUTOSIZE);
cvMoveWindow("Capture",60,70);
next_time_=ros::Time::now();
count_=0;
}
virtual ~UsbCamNode()
{
cvReleaseCapture(&capture);
cvDestroyWindow("Capture");
cvDestroyWindow("Canny");
}
bool take_and_send_image()
{
int key;
if(NULL==(src=cvQueryFrame(capture)))
{
printf("\nError on cvQueryFrame");
}
else
{
cvShowImage("Capture",src);
src_canny=DoCanny(src);
cvShowImage("Canny",src_canny);
key=cvWaitKey(10);
//fill image message
fillImage(img_,"mono8",src_canny->height,src_canny->width,src_canny->nChannels*src_canny->width,src_canny->imageData);
//publish
image_pub.publish(img_);
return true;
}
}
bool spin()
{
while(node_.ok())
{
if(take_and_send_image())
{
count_++;
ros::Time now_time=ros::Time::now();
if(now_time>next_time_)
{
std::cout<<count_<<"frames/sec at"<<now_time<<std::endl;
count_=0;
next_time_=next_time_+ros::Duration(1,0);
}
} else {
ROS_ERROR("could not take the image");
usleep(1000000);
}
}
return true;
} };
And it will show the results that: 0 Error on cvCaptureFromCAM Segmentation fault
More, I find my problems are similar with dkst. I try all my methods as possible as I can think,but I did solve these problems. So call for help here
Thanks.