ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Image looks open exposed while using imwrite on opencv

asked 2016-08-12 11:58:10 -0500

SpacemanSPIFF gravatar image

updated 2016-08-12 12:01:49 -0500

I am trying to save 16 bit images from ROS bag as .jpg, but where i save them, my images look over exposed, but when I use matlab its saved correctly. I'm not sure where I am screwing things up. My function is shown below.

void img_listener::callbackirimage(const sensor_msgs::Image::ConstPtr& msg)
{
        ir_img_ptr=cv_bridge::toCvCopy(msg,msg->encoding);
        rep_ros_ir_time[ir_image_count] = msg->header.stamp.toSec();
    #ifdef _DEBUG
        ROS_INFO("The reported color image time :%15.15f",rep_ros_clr_time[color_image_count]);
    #endif
        //ir_img_ptr->image.convertTo(ir_img[ir_image_count],CV_8UC1,(double)(1.0/256),256);
        //ir_img_ptr->image.convertTo(ir_img[ir_image_count],CV_8UC1);
        ir_img[ir_image_count] = ir_img_ptr->image;
        if (ir_image_count >= FRAME_RATE_COLOR_IMG)
            ir_image_count=0;
        else
            ir_image_count++;
}

where

 cv::Mat ir_img[FRAME_RATE_COLOR_IMG];

When I perform a imshow or a imwrite, the resultant image looks over exposed .image description

as compared to

image description

Setup: Ubuntu, ros jade, opencv 2.4.8. Any Help is greatly Appreciated.

Thanks Akshay

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2016-08-15 04:02:44 -0500

Dimitri Schachmann gravatar image

JPG does not support 16 bit images and with OpenCV you apparently need to convert your images explicitly to 8-bit before saving (information will be lost!)

You have commented out code that does almost that. It needs to be:

ir_img_ptr->image.convertTo(ir_img[ir_image_count],CV_8UC1,(double)(1.0/256), 0);

Notice the last parameter. Look here for convertTo for details.

edit flag offensive delete link more
0

answered 2016-08-18 10:39:33 -0500

SpacemanSPIFF gravatar image

@Dimitri: Thanks for letting me know that JPG doesn't support 16 bit images on OpenCV. I tried the convertTo function as you mentioned, but for some reason gave a completely dark image. I guess the image was normalized between 0-1. Instead i tried cv::normalize() which worked. Here is the modified code.

void img_listener::callbackirimage(const sensor_msgs::Image::ConstPtr& msg)
{
        ir_img_ptr=cv_bridge::toCvCopy(msg,sensor_msgs::image_encodings::MONO16);
        rep_ros_ir_time[ir_image_count] = msg->header.stamp.toSec();
    #ifdef _DEBUG
        ROS_INFO("The reported color image time :%15.15f",rep_ros_clr_time[color_image_count]);
    #endif
        //ir_img_ptr->image.convertTo(ir_img[ir_image_count],CV_8UC1,(double)(1.0/256.0),0);
        cv::normalize(ir_img_ptr->image,ir_img[ir_image_count],0,255,cv::NORM_MINMAX,CV_8UC1);
        //ir_img[ir_image_count].convertTo(ir_img[ir_image_count],CV_8UC1,256,0);
        //ir_img[ir_image_count] = ir_img_ptr->image;
        if (ir_image_count >= FRAME_RATE_COLOR_IMG)
            ir_image_count=0;
        else
            ir_image_count++;
}

Thanks for the help. I really appreciate it.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-08-12 11:58:10 -0500

Seen: 335 times

Last updated: Aug 18 '16