7 #include <opencv2/opencv.hpp> 8 #include <opencv2/core/core.hpp> 9 #include <opencv2/highgui/highgui.hpp> 10 #include <opencv2/imgproc/imgproc.hpp> 11 #include <openni2/OpenNI.h> 13 #include <std_msgs/String.h> 17 #include <libavutil/mathematics.h> 18 #include <libavutil/avassert.h> 19 #include <libavutil/channel_layout.h> 20 #include <libavutil/opt.h> 21 #include <libavutil/imgutils.h> 22 #include <libswscale/swscale.h> 23 #include <libavformat/avformat.h> 24 #include <libavutil/time.h> 25 #include <libavcodec/avcodec.h> 26 #include <libavutil/pixfmt.h> 82 int main(
int argc,
char** argv ){
85 info_publisher = nh.
advertise<std_msgs::String>(
"/camera/streamer_info",100);
91 cout<<
"Ros Error "<<endl;
100 t = avformat_network_init();
103 encoder = avcodec_find_encoder(AV_CODEC_ID_H264);
159 std_msgs::String
msg;
160 string str =
"Could not create output format context";
171 std_msgs::String
msg;
172 string str =
"Could not create OutputFormat";
181 std_msgs::String
msg;
182 string str =
"Could not create output stream ";
210 cout<<
"---------------Stream Info----------------------"<<endl;
213 cout<<
"------------------------------------------------"<<endl;
237 result = openni::OpenNI::initialize();
241 result =
any_device.open(openni::ANY_DEVICE );
250 openni::VideoMode depth_mode;
252 depth_mode.setResolution( 640, 480 );
253 depth_mode.setFps( 30 );
255 depth_mode.setPixelFormat( openni::PIXEL_FORMAT_DEPTH_1_MM );
259 openni::VideoMode color_mode;
260 color_mode.setResolution( 640, 480 );
261 color_mode.setFps( 30 );
262 color_mode.setPixelFormat( openni::PIXEL_FORMAT_RGB888 );
265 if(
any_device.isImageRegistrationModeSupported(
266 openni::IMAGE_REGISTRATION_DEPTH_TO_COLOR ) ){
267 any_device.setImageRegistrationMode( openni::IMAGE_REGISTRATION_DEPTH_TO_COLOR );
279 openni::VideoFrameRef frame_depth;
280 openni::VideoFrameRef frame_color;
292 const cv::Mat mImageRGB(frame_color.getHeight(), frame_color.getWidth(), CV_8UC3, (
void*)frame_color.getData());
294 cv::Mat imageBGRSrc , imageBGRDest;
295 cv::cvtColor( mImageRGB, imageBGRSrc, CV_RGB2BGR );
296 imageBGRDest .create(imageBGRSrc.size(),imageBGRSrc.type());
301 map_x.create(imageBGRSrc.size(),CV_32FC1);
302 map_y.create(imageBGRSrc.size(),CV_32FC1);
303 for(
int i=0;i<imageBGRSrc.rows;i++){
304 for(
int j=0;j<imageBGRSrc.cols;j++){
305 map_x.at<
float>(i,j) =(
float) (imageBGRSrc.cols-j);
306 map_y.at<
float>(i,j) = (
float) i;
309 remap(imageBGRSrc,imageBGRDest,map_x,map_y,CV_INTER_LINEAR);
317 cout<<
"read color stream error"<<endl;
322 Status readFrameStatus ;
325 if(readFrameStatus ==STATUS_OK){
326 const cv::Mat mImageDepth( frame_depth.getHeight(), frame_depth.getWidth(), CV_16UC1, (
void*)frame_depth.getData());
328 cv::Mat mScaledDepth;
331 mImageDepth.convertTo( mScaledDepth, CV_8U, 255.0 / iMaxDepth );
332 flip(mScaledDepth,mScaledDepth,1);
334 cv::Mat imageDepthSrc ;
335 cv::cvtColor( mScaledDepth, imageDepthSrc, CV_GRAY2BGR );
340 cout<<
"read depth stream error"<<endl;
343 cout<<
"depth stream is not valid"<<endl;
359 const int strides[] = {mat->step[0]};
362 pkt = av_packet_alloc();
368 AVFrame *pFrameYUV =av_frame_alloc();
369 AVFrame *pFrameBGR =av_frame_alloc();
370 int width = mat->cols;
371 int height = mat->rows;
372 pFrameBGR->width = width;
373 pFrameBGR->height = height;
376 avpicture_fill((AVPicture *)pFrameBGR, mat->data, AV_PIX_FMT_BGR24,width, height);
379 int numBytesYUV=av_image_get_buffer_size(AV_PIX_FMT_YUV420P, width,height,1);
380 uint8_t* bufferYUV=(uint8_t *)av_malloc(numBytesYUV*
sizeof(uint8_t));
381 avpicture_fill((AVPicture *)pFrameYUV, bufferYUV, AV_PIX_FMT_YUV420P,width, height);
384 SwsContext *sws_ctx = sws_getContext(width,height,
391 pFrameYUV->width = width;
392 pFrameYUV->height = height;
393 pFrameYUV->format = AV_PIX_FMT_YUV420P;
396 pFrameYUV->width = width;
397 pFrameYUV->height = height;
398 pFrameYUV->format = AV_PIX_FMT_YUV420P;
403 sws_scale(sws_ctx, (uint8_t
const *
const *)pFrameBGR->data,
404 pFrameBGR->linesize, 0, height,
405 pFrameYUV->data, pFrameYUV->linesize);
408 t1 = avcodec_encode_video2(
format_ctx_rgb->streams[0]->codec,pkt,pFrameYUV,&got_pkt);
410 sprintf(info,
"%s %d",
"encode RGB video frame number: ",
frame_count_rgb);
413 t2 = avcodec_encode_video2(
format_ctx_depth->streams[0]->codec,pkt,pFrameYUV,&got_pkt);
421 pkt->stream_index = 0;
425 AVRational time_base_q = {1,AV_TIME_BASE};
428 int64_t interval_duration = (double)(AV_TIME_BASE)*(1 / av_q2d(framerate));;
431 pkt->pts = av_rescale_q(
frame_count_rgb *interval_duration, time_base_q, time_base);
433 pkt->duration = av_rescale_q(interval_duration, time_base_q, time_base);
437 int64_t pts_time = av_rescale_q(pkt->pts, time_base, time_base_q);
439 if (pts_time > now_time){
440 av_usleep(pts_time - now_time);
448 AVRational time_base_q = {1,AV_TIME_BASE};
451 int64_t interval_duration = (double)(AV_TIME_BASE)*(1 / av_q2d(framerate));;
454 pkt->pts = av_rescale_q(
frame_count_depth *interval_duration, time_base_q, time_base);
456 pkt->duration = av_rescale_q(interval_duration, time_base_q, time_base);
460 int64_t pts_time = av_rescale_q(pkt->pts, time_base, time_base_q);
462 if (pts_time > now_time){
463 av_usleep(pts_time - now_time);
473 av_frame_free(&pFrameBGR);
474 av_frame_free(&pFrameYUV);
476 sws_freeContext(sws_ctx);
480 std_msgs::String
msg ;
481 string str =
"caught interrupt signal , program exited";
489 openni::OpenNI::shutdown();
498 std_msgs::String
msg;
500 if( result != STATUS_OK ) {
501 cerr <<
"[Error] -- "<< status <<endl;
502 cout<<OpenNI::getExtendedError() << endl;
503 str.append(
"[Error] -- ");
506 cout<<
"[OK] -- "<<status<<endl;
507 str.append(
"[OK] -- ");
515 std_msgs::String
msg;
518 cerr<<
"[Error] -- "<<info<<endl;
522 cout<<
"[OK] -- "<<info<<endl;
AVCodecContext * codec_ctx_rgb
void checkOpenNIError(Status result, char *status)
void publish(const boost::shared_ptr< M > &message) const
AVOutputFormat * outputfmt_rgb
void checkFFmpegError(char *info, int ret)
const char * rtmp_addr_depth
int64_t frame_count_depth
void send_mat_to_server(cv::Mat *mat, bool is_rgb)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
AVFormatContext * format_ctx_depth
const char * rtmp_addr_rgb
AVCodecContext * codec_ctx_depth
ROSCPP_DECL std::string remap(const std::string &name)
ros::Publisher info_publisher
AVFormatContext * format_ctx_rgb
openni::VideoStream video_stream_depth
void signal_handler(int s)
int main(int argc, char **argv)
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
openni::VideoStream video_stream_color
AVStream * out_stream_rgb
AVStream * out_stream_depth
openni::Device any_device
AVOutputFormat * outputfmt_depth