From d800cb41fc1f8575ecf3df75afab19275f55a8e0 Mon Sep 17 00:00:00 2001 From: econsys10 Date: Fri, 24 Jan 2020 16:19:12 +0530 Subject: [PATCH] Bug fix in FPS enumeration and ReadMe Update --- README.md | 1 + source/ecam_v4l2/src/camera.cpp | 10 +++- source/rqt_cam/src/rqt_cam/controls.cpp | 3 + source/rqt_cam/src/rqt_cam/image_view.cpp | 69 ++++++++++++++--------- 4 files changed, 56 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index a9d8378..3c4f70a 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,7 @@ It also provides UI for changing V4l2 controls and to switch Color space/Compres ### Supported Color space/Compression: * UYVY +* YUYV * MJPG * Y8 * Y12 diff --git a/source/ecam_v4l2/src/camera.cpp b/source/ecam_v4l2/src/camera.cpp index 888c6fa..665f397 100644 --- a/source/ecam_v4l2/src/camera.cpp +++ b/source/ecam_v4l2/src/camera.cpp @@ -320,7 +320,7 @@ namespace ecam_v4l2 image->data.resize(2 * frmt.stream_fmt.fmt.pix.width * frmt.stream_fmt.fmt.pix.height); memcpy(&image->data[0],buffers[buffer.index].start,2*frmt.stream_fmt.fmt.pix.width*frmt.stream_fmt.fmt.pix.height); - }else if(frmt.stream_fmt.fmt.pix.pixelformat==V4L2_PIX_FMT_UYVY){ + }else if(frmt.stream_fmt.fmt.pix.pixelformat==V4L2_PIX_FMT_UYVY ||frmt.stream_fmt.fmt.pix.pixelformat==V4L2_PIX_FMT_YUYV){ image->data.resize(2 * frmt.stream_fmt.fmt.pix.width * frmt.stream_fmt.fmt.pix.height); memcpy(&image->data[0],buffers[buffer.index].start,2*frmt.stream_fmt.fmt.pix.width*frmt.stream_fmt.fmt.pix.height); @@ -371,6 +371,11 @@ namespace ecam_v4l2 image->format = "uyvy"; image->data.reserve(2 * frmt.stream_fmt.fmt.pix.height * frmt.stream_fmt.fmt.pix.width); + }else if(frmt.stream_fmt.fmt.pix.pixelformat==V4L2_PIX_FMT_YUYV){ + + image->format = "yuyv"; + image->data.reserve(2 * frmt.stream_fmt.fmt.pix.height * frmt.stream_fmt.fmt.pix.width); + }else if(frmt.stream_fmt.fmt.pix.pixelformat==V4L2_PIX_FMT_Y12){ image->format = "mono12"; @@ -524,6 +529,9 @@ namespace ecam_v4l2 case V4L2_PIX_FMT_UYVY: *pixelformat = "UYVY"; break; + case V4L2_PIX_FMT_YUYV: + *pixelformat = "YUYV"; + break; case V4L2_PIX_FMT_GREY: *pixelformat = "GREY"; break; diff --git a/source/rqt_cam/src/rqt_cam/controls.cpp b/source/rqt_cam/src/rqt_cam/controls.cpp index a1bd724..84a3960 100644 --- a/source/rqt_cam/src/rqt_cam/controls.cpp +++ b/source/rqt_cam/src/rqt_cam/controls.cpp @@ -140,6 +140,7 @@ namespace rqt_cam{ int Controls::enum_format(int type,std::string pix_fmt,int width,int height) { int index; + char dummy_char; ros::NodeHandle node_handle; ros::ServiceClient enum_format_client = node_handle.serviceClient("EnumerateFormat"); rqt_cam::enum_format fmt_msg; @@ -166,6 +167,8 @@ namespace rqt_cam{ for (index=0 ;index< fmt_msg.response.str.size(); index++){ resolutions.push_back(fmt_msg.response.str[index]); } + std::istringstream temp1_stream(resolutions[0]); + temp1_stream >>width >> dummy_char >> height; // Getting currently enumerated height and width. }else { ROS_ERROR("Failed to call service Format_setting"); break; diff --git a/source/rqt_cam/src/rqt_cam/image_view.cpp b/source/rqt_cam/src/rqt_cam/image_view.cpp index dbdf694..6c333e2 100644 --- a/source/rqt_cam/src/rqt_cam/image_view.cpp +++ b/source/rqt_cam/src/rqt_cam/image_view.cpp @@ -647,33 +647,50 @@ namespace rqt_cam { ************************************************************************************************************/ void ImageView::callbackImage(const rqt_cam::image::ConstPtr& msg) { - if(msg->format=="uyvy"){ - cv::Mat mat_src = cv::Mat(msg->height, msg->width, CV_8UC2,(void*)&msg->data[0]); - cv::cvtColor(mat_src, conversion_mat_, cv::COLOR_YUV2RGB_UYVY); - }else if(msg->format=="mjpg"){ - cv::Mat Bgr_data; - imdecode(cv::Mat(1, msg->length, CV_8U, (void*)&msg->data[0]),cv::IMREAD_COLOR, &Bgr_data); - cv::cvtColor(Bgr_data,conversion_mat_, cv::COLOR_BGR2RGB); - }else if(msg->format=="mono8"){ - cv::Mat gray8; - cv::Mat(msg->height, msg->width, CV_8UC1, (void*)&msg->data[0]).convertTo(gray8, CV_8U); - cv::cvtColor(gray8,conversion_mat_, cv::COLOR_GRAY2BGR); - - }else if(msg->format=="mono16"){ - cv::Mat gray8; - ConvertY16toY8((uint16_t*)&msg->data[0],msg->height,msg->width, gray8); - cv::cvtColor(gray8,conversion_mat_, cv::COLOR_GRAY2BGR); - }else if(msg->format=="mono12"){ - cv::Mat gray8; - ConvertY12toY8((uint8_t*)&msg->data[0],msg->height,msg->width, gray8); - cv::cvtColor(gray8,conversion_mat_, cv::COLOR_GRAY2BGR); + try{ + + if(msg->format=="uyvy"){ + cv::Mat mat_src = cv::Mat(msg->height, msg->width, CV_8UC2,(void*)&msg->data[0]); + cv::cvtColor(mat_src, conversion_mat_, cv::COLOR_YUV2RGB_UYVY); + }else if (msg->format=="yuyv") { + cv::Mat mat_src = cv::Mat(msg->height, msg->width, CV_8UC2,(void*)&msg->data[0]); + cv::cvtColor(mat_src, conversion_mat_, cv::COLOR_YUV2RGB_YUYV); + }else if(msg->format=="mjpg"){ + cv::Mat Bgr_data; + // Checking whether mjpeg header is valid. + if( ((uint8_t*)&msg->data[0])[0] == 0xFF && ((uint8_t*)&msg->data[0])[1] == 0xD8){ + imdecode(cv::Mat(1, msg->length, CV_8U, (void*)&msg->data[0]),cv::IMREAD_COLOR, &Bgr_data); + cv::cvtColor(Bgr_data,conversion_mat_, cv::COLOR_BGR2RGB); + }else{ + return; + } + }else if(msg->format=="mono8"){ + cv::Mat gray8; + cv::Mat(msg->height, msg->width, CV_8UC1, (void*)&msg->data[0]).convertTo(gray8, CV_8U); + cv::cvtColor(gray8,conversion_mat_, cv::COLOR_GRAY2BGR); + + }else if(msg->format=="mono16"){ + cv::Mat gray8; + ConvertY16toY8((uint16_t*)&msg->data[0],msg->height,msg->width, gray8); + cv::cvtColor(gray8,conversion_mat_, cv::COLOR_GRAY2BGR); + }else if(msg->format=="mono12"){ + cv::Mat gray8; + ConvertY12toY8((uint8_t*)&msg->data[0],msg->height,msg->width, gray8); + cv::cvtColor(gray8,conversion_mat_, cv::COLOR_GRAY2BGR); + } + // image must be copied since it uses the conversion_mat_ for storage which is asynchronously overwritten in the next callback invocation + QImage image(conversion_mat_.data,msg->width,msg->height, 3*msg->width, QImage::Format_RGB888); + ui_.image_frame->setImage(image); + if(save_img){ + save_img=false; + save_image(&msg->data[0],msg->format,msg->length,msg->width,msg->height); + } } - // image must be copied since it uses the conversion_mat_ for storage which is asynchronously overwritten in the next callback invocation - QImage image(conversion_mat_.data,msg->width,msg->height, 3*msg->width, QImage::Format_RGB888); - ui_.image_frame->setImage(image); - if(save_img){ - save_img=false; - save_image(&msg->data[0],msg->format,msg->length,msg->width,msg->height); + catch(cv::Exception& e) + { + qWarning("ImageView.callback_image() while trying to convert image to 'rgb8' an exception was thrown (%s)", e.what()); + ui_.image_frame->setImage(QImage()); + return; } }