Ask Your Question
0

open cv functions

asked 2013-01-09 07:46:32 -0600

kala gravatar image

Hi one which is seen below is my code in this i have converted it into inverted image & now i need to convert it into grayscale image i have tried but i didn't get it so will u please help me

#include <ros/ros.h>
#include <image_transport/image_transport.h>
#include <cv_bridge/cv_bridge.h>
#include <sensor_msgs/image_encodings.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
namespace enc = sensor_msgs::image_encodings;
static const char WINDOW[] = "Image Processed";
    image_transport::Publisher pub;
void imageCallback(const sensor_msgs::ImageConstPtr& original_image)
{
    cv_bridge::CvImagePtr cv_ptr;
    try
    {
        cv_ptr = cv_bridge::toCvCopy(original_image, enc::BGR8);
    }
    catch (cv_bridge::Exception& e)
    {
        ROS_ERROR("pub_node::main.cpp::cv_bridge exception: %s", e.what());
        return;
    }
    for(int i=0; i<cv_ptr->image.rows; i++)
    {
        for(int j=0; j<cv_ptr->image.cols; j++)
        {
          for(int k=0; k<cv_ptr->image.channels(); k++)
            {           
                cv_ptr->image.data[i*cv_ptr->image.rows*4+j*3 + k] = 255-cv_ptr->image.data[i*cv_ptr->image.rows*4+j*3 + k];
            }
        }
    }
        cv::imshow(WINDOW, cv_ptr->image);
            cv::waitKey(3);
            pub.publish(cv_ptr->toImageMsg());
}

int main(int argc, char **argv)
{
            ros::init(argc, argv, "image_processor");
                ros::NodeHandle nh;
            image_transport::ImageTransport it(nh);
         cv::namedWindow(WINDOW, CV_WINDOW_AUTOSIZE);
                image_transport::Subscriber sub = it.subscribe("camera/image_raw", 1, imageCallback);
        cv::destroyWindow(WINDOW);

        pub = it.advertise("camera/image_processed", 1);
            ros::spin();
        ROS_INFO("pub_node::main.cpp::No error.");

}
edit retag flag offensive close merge delete

Comments

You don't need all the 'for' loops for creation of negative image. Just write 'image = 255 - image;'

Michael Burdinov gravatar imageMichael Burdinov ( 2013-01-09 08:41:27 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2013-01-09 08:23:49 -0600

rics gravatar image

You do not have to write your own conversion function as you can convert an image with the built-in cvtColor function.

So for converting an RGB image to grayscale you can call cvtColor(src, dst, CV_RGB2GRAY).

edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-01-09 07:46:32 -0600

Seen: 389 times

Last updated: Jan 09 '13