Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] add info to how to contribute new features #110

Open
wants to merge 1 commit into
base: indigo
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ generate_dynamic_reconfigure_options(
cfg/HLSColorFilter.cfg
cfg/HSVColorFilter.cfg
cfg/WatershedSegmentation.cfg
#
cfg/Sample.cfg
)

## Generate messages in the 'msg' folder
Expand Down Expand Up @@ -342,6 +344,9 @@ endif()
# https://github.com/opencv/opencv/blob/2.4/samples/cpp/video_homography.cpp
# https://github.com/opencv/opencv/blob/2.4/samples/cpp/videocapture_pvapi.cpp

# SAMPLE
opencv_apps_add_nodelet(sample src/nodelet/sample_nodelet.cpp)

add_library(${PROJECT_NAME} SHARED
src/nodelet/nodelet.cpp
${_opencv_apps_nodelet_cppfiles}
Expand Down
28 changes: 28 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Contributing to opencv_apps
===========================

Thanks for getting involved! We always welcome your contributions. The `opencv_apps` is the collection of image processing ROS nodes based on [OpenCV(Open Source Computer Vision Library)](https://github.com/opencv/opencv)

How to contribute
=================

If you find a missing feature, please find corresponding OpenCV sample program. For example, if you willing to add histogram examples, find [tutorial_code/Histograms_Matching](https://github.com/opencv/opencv/tree/master/samples/cpp/tutorial_code/Histograms_Matching).
Then look for [CMakeLists.txt](https://github.com/ros-perception/opencv_apps/blob/cbafaa05dc32495b9aa6d487cbd411a405ad14bc/CMakeLists.txt#L167) file.

Then add source and cfg file. See [sample_nodelet](https://github.com/ros-perception/opencv_apps/blob/contrib/src/nodelet/sample_nodelet.cpp) and [Sample.cfg](https://github.com/ros-perception/opencv_apps/blob/contrib/cfg/Sample.cfg) for example.
Please see @TODO section and modify to your contributed node.
Note that all C++ code is validated by `clang-format` on [TravisCI](https://travis-ci.org/ros-perception/opencv_apps). So run following command before you commit source code.
```
find . -name '*.h' -or -name '*.hpp' -or -name '*.cpp' | xargs clang-format-3.9 -i -style=file
```

Then use `opencv_apps_add_nodelet` macro to add your source code. This magic macro will generate nodelet as well as a standalone executable.
```
opencv_apps_add_nodelet(equalize_histogram src/nodelet/equalize_histogram_nodelet.cpp) # ./tutorial_code/Histograms_Matching/EqualizeHist_Demo.cpp
```

Nodelet also requires to add your contributed code to [nodelet_plugins.xml](https://github.com/ros-perception/opencv_apps/blob/contrib/nodelet_plugins.xml#L118-L120).

We also require sample launch file and test launch file. See [sample.launch](https://github.com/ros-perception/opencv_apps/blob/contrib/launch/sample.launch) and [Sample.cfg](https://github.com/ros-perception/opencv_apps/blob/contrib/test/test-sample.test) for example. Please see @TODO section and modify to your contributed node.


11 changes: 11 additions & 0 deletions cfg/Sample.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env python

PACKAGE = "opencv_apps"

from dynamic_reconfigure.parameter_generator_catkin import *

gen = ParameterGenerator()

gen.add("use_camera_info", bool_t, 0, "Indicates that the camera_info topic should be subscribed to to get the default input_frame_id. Otherwise the frame from the image message will be used.", False)

exit(gen.generate(PACKAGE, "sample", "Sample"))
19 changes: 19 additions & 0 deletions launch/sample.launch
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<launch>
<arg name="node_name" default="sample" /> <!-- @TODO change node name -->

<arg name="image" default="image" doc="The image topic. Should be remapped to the name of the real image topic." />

<arg name="use_camera_info" default="false" doc="Indicates that the camera_info topic should be subscribed to to get the default input_frame_id. Otherwise the frame from the image message will be used." />
<arg name="debug_view" default="true" doc="Specify whether the node displays a window to show edge image" />
<arg name="queue_size" default="3" doc="Specigy queue_size of input image subscribers" />
<!-- @TODO add your parameters here -->

<!-- sample.cpp -->
<node name="$(arg node_name)" pkg="opencv_apps" type="sample" > <!-- @TODO change type name -->
<remap from="image" to="$(arg image)" />
<param name="use_camera_info" value="$(arg use_camera_info)" />
<param name="debug_view" value="$(arg debug_view)" />
<param name="queue_size" value="$(arg queue_size)" />
<!-- @TODO add your parameters here -->
</node>
</launch>
4 changes: 4 additions & 0 deletions nodelet_plugins.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@
<description>Nodelet to demonstrate the famous watershed segmentation algorithm in OpenCV: watershed()</description>
</class>

<class name="opencv_apps/sample" type="opencv_apps::SampleNodelet" base_class_type="nodelet::Nodelet">
<description>Nodelet to demonstrate the simple image processing</description>
</class>

<!--
for backward compatibility, can be removed in M-turtle
-->
Expand Down
237 changes: 237 additions & 0 deletions src/nodelet/sample_nodelet.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
/*********************************************************************
* Software License Agreement (BSD License)
*
* Copyright (c) @TODO Please fill your name here
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of the Kei Okada nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*********************************************************************/

// @TODO Please add link to original sample program
/**
* This is a demo of @TODO
*/

#include <ros/ros.h>
#include "opencv_apps/nodelet.h"
#include <image_transport/image_transport.h>
#include <sensor_msgs/image_encodings.h>
#include <cv_bridge/cv_bridge.h>
#include <sensor_msgs/image_encodings.h>

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

// @TODO Fill more header files hHere
#include <dynamic_reconfigure/server.h>

// @TODO Note that please try to use existing opencv_apps/msg. They refrect opencv data classes.
#include "opencv_apps/Point2DStamped.h"

//
#include "opencv_apps/SampleConfig.h"
#include "opencv_apps/nodelet.h"

namespace opencv_apps
{
class SampleNodelet : public opencv_apps::Nodelet
{
image_transport::Publisher img_pub_;
image_transport::Subscriber img_sub_;
image_transport::CameraSubscriber cam_sub_;
ros::Publisher msg_pub_;

boost::shared_ptr<image_transport::ImageTransport> it_;

typedef opencv_apps::SampleConfig Config;
typedef dynamic_reconfigure::Server<Config> ReconfigureServer;
Config config_;
boost::shared_ptr<ReconfigureServer> reconfigure_server_;

int queue_size_;
bool debug_view_;
ros::Time prev_stamp_;

std::string window_name_;
static bool need_config_update_;

bool needToInit;

// @TODO Fill more vairables here
static cv::Point2f point_;

// @TODO Define callback functions as static
static void onMouse(int event, int x, int y, int /*flags*/, void* /*param*/)
{
ROS_INFO("onMouse %d %d %d", event, x, y); // @TODO static funcion needs ROS_INFO, but others can use NODELET_INFO
if (event == CV_EVENT_LBUTTONDOWN)
{
point_ = cv::Point2f((float)x, (float)y);
}
}

void reconfigureCallback(Config& new_config, uint32_t level)
{
config_ = new_config;
// @TODO Fill your code here
}

void imageCallbackWithInfo(const sensor_msgs::ImageConstPtr& msg, const sensor_msgs::CameraInfoConstPtr& cam_info)
{
doWork(msg, cam_info->header.frame_id);
}

void imageCallback(const sensor_msgs::ImageConstPtr& msg)
{
doWork(msg, msg->header.frame_id);
}

void doWork(const sensor_msgs::ImageConstPtr& msg, const std::string& input_frame_from_msg)
{
// Work on the image.
try
{
// Convert the image into something opencv can handle.
cv::Mat image = cv_bridge::toCvShare(msg, sensor_msgs::image_encodings::BGR8)->image;

// Messages
opencv_apps::Point2DStamped output_msg;
output_msg.header = msg->header;

if (debug_view_)
{
// Create windows
cv::namedWindow(window_name_, cv::WINDOW_AUTOSIZE);

// @TODO Create other GUI tools
cv::setMouseCallback(window_name_, onMouse, 0);

if (need_config_update_)
{
reconfigure_server_->updateConfig(config_);
// @TODO Update parameters
need_config_update_ = false;
}
}

// Do the work
if (needToInit)
{
// @TODO Do initial process
}

// @TODO Do the work
output_msg.point.x = point_.x;
output_msg.point.y = point_.y;

needToInit = false;
if (debug_view_)
{
cv::imshow(window_name_, image);

char c = (char)cv::waitKey(1);
// if( c == 27 )
// break;
switch (c)
{
case 'r':
needToInit = true;
break;
}
}

// Publish the image.
sensor_msgs::Image::Ptr out_img = cv_bridge::CvImage(msg->header, msg->encoding, image).toImageMsg();
img_pub_.publish(out_img);
msg_pub_.publish(output_msg);
}
catch (cv::Exception& e)
{
NODELET_ERROR("Image processing error: %s %s %s %i", e.err.c_str(), e.func.c_str(), e.file.c_str(), e.line);
}

prev_stamp_ = msg->header.stamp;
}

void subscribe() // NOLINT(modernize-use-override)
{
NODELET_DEBUG("Subscribing to image topic.");
if (config_.use_camera_info)
cam_sub_ = it_->subscribeCamera("image", queue_size_, &SampleNodelet::imageCallbackWithInfo, this);
else
img_sub_ = it_->subscribe("image", queue_size_, &SampleNodelet::imageCallback, this);
}

void unsubscribe() // NOLINT(modernize-use-override)
{
NODELET_DEBUG("Unsubscribing from image topic.");
img_sub_.shutdown();
cam_sub_.shutdown();
}

public:
virtual void onInit() // NOLINT(modernize-use-override)
{
Nodelet::onInit();
it_ = boost::shared_ptr<image_transport::ImageTransport>(new image_transport::ImageTransport(*nh_));

pnh_->param("queue_size", queue_size_, 3);
pnh_->param("debug_view", debug_view_, false);
if (debug_view_)
{
always_subscribe_ = true;
}
prev_stamp_ = ros::Time(0, 0);

window_name_ = "Sample"; // @TODO Add program name
needToInit = true;

reconfigure_server_ = boost::make_shared<dynamic_reconfigure::Server<Config> >(*pnh_);
dynamic_reconfigure::Server<Config>::CallbackType f =
boost::bind(&SampleNodelet::reconfigureCallback, this, _1, _2);
reconfigure_server_->setCallback(f);

img_pub_ = advertiseImage(*pnh_, "image", 1);
msg_pub_ = advertise<opencv_apps::Point2DStamped>(*pnh_, "output", 1);
// @TODO add more advertise/services

NODELET_INFO("Hot keys: ");
NODELET_INFO("\tESC - quit the program");
NODELET_INFO("\tr - auto-initialize tracking");
// @ TODO Add more messages

onInitPostProcess();
}
};
bool SampleNodelet::need_config_update_ = false;
// @TODO static variable must initialized here
cv::Point2f SampleNodelet::point_ = cv::Point2f(0, 0);
} // namespace opencv_apps

#include <pluginlib/class_list_macros.h>
PLUGINLIB_EXPORT_CLASS(opencv_apps::SampleNodelet, nodelet::Nodelet);
26 changes: 26 additions & 0 deletions test/test-sample.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<launch>
<arg name="gui" default="true" />
<node name="play_face_bag" pkg="rosbag" type="play" args="-l $(find opencv_apps)/test/face_detector_withface_test_diamondback.bag" />

<group ns="wide_stereo/left" >
<node name="image_proc" pkg="image_proc" type="image_proc" />
<node name="image_view" pkg="image_view" type="image_view" args="image:=image_rect_color" if="$(arg gui)" />

<!-- sample.cpp -->
<include file="$(find opencv_apps)/launch/sample.launch" > <!-- @TODO include your launch file -->
<arg name="debug_view" value="$(arg gui)" />
<arg name="image" value="image_rect_color" />
</include>

<!-- Test Codes -->
<node name="sample_saver_result" pkg="image_view" type="image_saver" args="image:=sample/image" > <!-- @TODO change node name -->
<param name="filename_format" value="$(find opencv_apps)/test/sample_result.png"/> <!-- @TODO change file name -->
</node>
<param name="sample_test/topic" value="sample/output" /> <!-- @TODO topic name to check -->
<test test-name="sample_test" pkg="rostest" type="hztest" name="sample_test" > <!-- @TODO change test name -->
<param name="hz" value="20" />
<param name="hzerror" value="15" />
<param name="test_duration" value="5.0" />
</test>
</group>
</launch>