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

Pointer image multi-channel access

asked 2014-07-24 05:06:38 -0600

ROSkinect gravatar image

updated 2014-07-24 05:06:53 -0600

Using that simple program how can I access to different channels:

void imageCallback(const sensor_msgs::ImageConstPtr& msg)
    {

        cv_bridge::CvImagePtr cv_ptr;
        try
        {
            cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8);
        }

        catch (cv_bridge::Exception& e)
        {
            ROS_ERROR("cv_bridge exception: %s", e.what());
            return;
        }


        cv::imshow("OpenCV viewer uEye RGB", cv_ptr->image);
        cv::waitKey(3);

    }

How can I access to different channel using cv_ptr ?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2014-07-24 05:19:30 -0600

Wolf gravatar image

updated 2014-07-24 05:56:21 -0600

cv_ptr->image is an instance of cv::Mat ( http://docs.opencv.org/modules/core/d... ). There are serveral ways to access the channels, e. g.:

split up to entire image separate images:

std::vector<cv::Mat> channels;
cv::split( cv_ptr->image, channels );

cv::imshow("OpenCV viewer uEye RGB Blue", channels[0] );
cv::imshow("OpenCV viewer uEye RGB Red", channels[1] );
cv::imshow("OpenCV viewer uEye RGB Green", channels[2] );
cv::waitKey(3);

or access one pixel:

cv::Vec3b rgb_pix = cv_ptr->image.at<cv::Vec3b>( 10, 10 );

ROS_INFO_STREAM( "Blue value at pos 10, 10" << static_cast<int>( rgb_pix[0] ) );
ROS_INFO_STREAM( "Green value at pos 10, 10" << static_cast<int>( rgb_pix[1] ) );
ROS_INFO_STREAM( "Red value at pos 10, 10" << static_cast<int>( rgb_pix[2] ) );

or iterate the entire image, manipulating the pixels

cv::MatIterator_<cv::Vec3b> iter = cv_ptr->image.begin<cv::Vec3b>();
cv::MatIterator_<cv::Vec3b> iter_end = cv_ptr->image.end<cv::Vec3b>();

for ( ; iter != iter_end ; ++iter )
{
   (*iter)[2] = 255; // set red channel to 255, i. e. maxium value, for every pixel
}

 cv::imshow("OpenCV viewer uEye RGB manipulated", cv_ptr->image);
 cv::waitKey(3);

See: http://docs.opencv.org/

edit flag offensive delete link more

Comments

ah this is what I'm looking for std::vector<cv::mat> channels; cv::split( cv_ptr->image, channels ); last question how can I combine the 3 channels (after doing some image processing on each channel)

ROSkinect gravatar image ROSkinect  ( 2014-07-24 05:31:45 -0600 )edit
1

After you have done something with the channels in the channels vector you can assemble them back to the image using cv::merge ........ like: cv::merge( channels, cv_ptr->image );

Wolf gravatar image Wolf  ( 2014-07-24 05:36:48 -0600 )edit
1

Thank you. (just add " _ " to cv::MatIterator_<cv::vec3b>)

ROSkinect gravatar image ROSkinect  ( 2014-07-24 05:54:16 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2014-07-24 05:06:38 -0600

Seen: 1,929 times

Last updated: Jul 24 '14