Ask Your Question
1

how to publish and sub camera in opencv

asked 2014-09-18 08:36:32 -0500

turtle gravatar image

updated 2014-09-21 03:46:18 -0500

i've finished my opencv program ! I want to publish to pc other (LAN) What should I do

Here my program opencv

/*
 * roscolor.cpp
 *
 *  Created on: Sep 5, 2014
 *      Author: dell
 */
#include <ros/ros.h>
#include <vector>
#include <stdio.h>
#include <math.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>
#include <sensor_msgs/Image.h>
#include <opencv2/opencv.hpp>
using namespace cv;
int minR = 0;
int minG = 0;
int minB = 0;
int maxR = 255;
int maxG = 255;
int maxB = 255;
void imageCallback(const sensor_msgs::ImageConstPtr& color_img)
{
    cv_bridge::CvImagePtr img_ptr;
    cv::Mat img_rgb;
    try
    {
        img_ptr = cv_bridge::toCvCopy(color_img,
        sensor_msgs::image_encodings::BGR8);
        img_rgb = img_ptr->image;
    }
    catch (cv_bridge::Exception& e)
    {
        ROS_ERROR("cv_bridge exception: \%s", e.what());
        return;
    }
    cvCreateTrackbar("min R:","trackbar",&minR, 255);
    cvCreateTrackbar("min G:","trackbar",&minG, 255);
    cvCreateTrackbar("min B:","trackbar",&minB, 255);
    cvCreateTrackbar("max R:","trackbar",&maxR, 255);
    cvCreateTrackbar("max G:","trackbar",&maxG, 255);
    cvCreateTrackbar("max B:","trackbar",&maxB, 255);
    //cv::Mat img_hsv;
    cv::Mat img_binary;
    CvMoments colorMoment;
    cv::Scalar min_vals(minR, minG, minB);
    cv::Scalar max_vals(maxR, maxG, maxB);
    //cv::cvtColor(img_rgb, img_hsv, CV_BGR2HSV);
    cv::inRange(img_rgb, min_vals, max_vals, img_binary);
    dilate( img_binary, img_binary, getStructuringElement(MORPH_ELLIPSE, Size(10, 10)) );
    /*======================= TOA DO ================================*/
    colorMoment = moments(img_binary);
    double moment10 = cvGetSpatialMoment(&colorMoment, 1, 0);
    double moment01 = cvGetSpatialMoment(&colorMoment, 0, 1);
    double area = cvGetCentralMoment(&colorMoment, 0, 0);
    float posX = (moment10/area);
    float posY = (moment01/area);
    /*================= HIEN THI =================================*/
    printf("1. x-Axis  %f  y-Axis  %f  Area  %f\n", moment10, moment01, area);
    printf("2. x  %f  y %f \n\n", posX , posY);

    cv::imshow("TRACKING COLOR", img_binary);
    cv::imshow("RGB image", img_rgb);
    cv::waitKey(3);

}
int main(int argc, char **argv)
{
    ros::init(argc, argv, "HSV_image");
    ros::NodeHandle nh;
    cvNamedWindow("TRACKING COLOR", 2 );
    cvNamedWindow("RGB image", 2 );
    cvNamedWindow ("trackbar", 2 );
    cvStartWindowThread();
    image_transport::ImageTransport it(nh);
    ros::Subscriber cam_img_sub =nh.subscribe("/gscam/image_raw", 1, &imageCallback);
    ros::spin();
    cvDestroyWindow("TRACKING COLOR");
    cvDestroyWindow("RGB image");
}

can you help me, please ? Thank you!

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
3

answered 2014-09-22 01:11:36 -0500

Wolf gravatar image

To publish cv::Mat to ROS topic do tjhe following:

1) Include cv_bridge and sensor_msgs Image (You already did that=) )

#include <cv_bridge/cv_bridge.h>
#include <sensor_msgs/image_encodings.h>
#include <sensor_msgs/Image.h>

2) Set up image publisher for sensor_msgs/Image somewhere:

ros::Publisher out_img_pub =nh.advertise<sensor_msgs::Image>("image_out", 2, false);

3) In your conde where you have filled your cv::Mat convert it to sensor_msgs/image and publish:

// assuming you have declared and filled cv::Mat your_mat with data in colorspace "bgr8"
  cv_bridge::CvImage lo_img;

  lo_img.encoding = "bgr8";                        // or which enconding your data has
  lo_img.header.stamp = ros::Time::now();          //  or whatever timestamp suits here;
  lo_img.header.frame_id = "whatever_frame";       // frame id as neededby you
  lo_img.image = your_mat;                          // point cv_bridge to your object

// publishing data
out_img_pub.publish( lo_img.toImageMsg() );
edit flag offensive delete link more

Comments

thank you ! how to subscribe !

turtle gravatar image turtle  ( 2014-09-22 05:56:15 -0500 )edit

Subscribing is shown in the code in your answer; basically the definition of imageCallback and its binding to the Subscriber cam_img_sub and the stuff before cvCreateTrackbar in imageCallback is what you have to do for subscribing ros image topic data into your cv::Mat img_rgb;

Wolf gravatar image Wolf  ( 2014-09-22 06:05:19 -0500 )edit
1

answered 2014-09-18 10:00:20 -0500

joq gravatar image

The usual ROS solution is cv_bridge. See the tutorials linked from that wiki page.

edit flag offensive delete link more

Comments

I do not know how to use it . can you repeat clearly ?

turtle gravatar image turtle  ( 2014-09-18 22:49:37 -0500 )edit
0

answered 2014-09-21 08:37:51 -0500

Hamid Didari gravatar image

if you want to publish a msg and subscribe from another pc please read this page that describe how to use network in ros . if your problem in change from mat to ros image msg please read this page

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2014-09-18 08:36:32 -0500

Seen: 1,810 times

Last updated: Sep 22 '14