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

[rolling] image_publisher: add field of view parameter #985

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions image_publisher/doc/components.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Published Topics
Parameters
^^^^^^^^^^
* **filename** (string, default: ""): Name of image file to be published.
* **field_of_view** (double, default: 50): Camera field of view (deg) used to calculate focal length for camera info topic.
* **flip_horizontal** (bool, default: false): Flip output image horizontally.
* **flip_vertical** (bool, default: false): Flip output image vertically.
* **frame_id** (string, default: "camera") Frame id inserted in published
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class ImagePublisher : public rclcpp::Node
rclcpp::Node::OnSetParametersCallbackHandle::SharedPtr on_set_parameters_callback_handle_;

std::string filename_;
double field_of_view_;
bool flip_horizontal_;
bool flip_vertical_;
bool retry_; // If enabled will retry loading image from the filename_
Expand Down
14 changes: 12 additions & 2 deletions image_publisher/src/image_publisher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

#include <cmath>
#include <chrono>
#include <string>
#include <thread>
Expand Down Expand Up @@ -60,6 +61,7 @@ ImagePublisher::ImagePublisher(
std::string topic_name = node_base->resolve_topic_or_service_name("image_raw", false);
pub_ = image_transport::create_camera_publisher(this, topic_name);

field_of_view_ = this->declare_parameter("field_of_view", static_cast<double>(50));
flip_horizontal_ = this->declare_parameter("flip_horizontal", false);
flip_vertical_ = this->declare_parameter("flip_vertical", false);
frame_id_ = this->declare_parameter("frame_id", std::string("camera"));
Expand All @@ -79,6 +81,11 @@ ImagePublisher::ImagePublisher(
RCLCPP_INFO(get_logger(), "Reset filename as '%s'", filename_.c_str());
ImagePublisher::onInit();
return result;
} else if (parameter.get_name() == "field_of_view") {
field_of_view_ = parameter.as_double();
RCLCPP_INFO(get_logger(), "Reset field_of_view as '%f'", field_of_view_);
ImagePublisher::onInit();
return result;
} else if (parameter.get_name() == "flip_horizontal") {
flip_horizontal_ = parameter.as_bool();
RCLCPP_INFO(get_logger(), "Reset flip_horizontal as '%d'", flip_horizontal_);
Expand Down Expand Up @@ -218,10 +225,13 @@ void ImagePublisher::onInit()
camera_info_.height = image_.rows;
camera_info_.distortion_model = "plumb_bob";
camera_info_.d = {0, 0, 0, 0, 0};
camera_info_.k = {1, 0, static_cast<float>(camera_info_.width / 2), 0, 1,

// Based on https://learnopencv.com/approximate-focal-length-for-webcams-and-cell-phone-cameras/
double f_approx = (camera_info_.width / 2) / std::tan((field_of_view_ * M_PI / 180) / 2);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the default value 50 returning 1 ? as the current behavior ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't, and I forgot to account for breaking changes. I changed it now so when FOV is 0 or less, it disables the whole feature and uses 1.0 for focal length instead. The default vale is now zero as well, so now the whole thing is not breaking anything.

camera_info_.k = {f_approx, 0, static_cast<float>(camera_info_.width / 2), 0, f_approx,
static_cast<float>(camera_info_.height / 2), 0, 0, 1};
camera_info_.r = {1, 0, 0, 0, 1, 0, 0, 0, 1};
camera_info_.p = {1, 0, static_cast<float>(camera_info_.width / 2), 0, 0, 1,
camera_info_.p = {f_approx, 0, static_cast<float>(camera_info_.width / 2), 0, 0, f_approx,
static_cast<float>(camera_info_.height / 2), 0, 0, 0, 1, 0};

timer_ = this->create_wall_timer(
Expand Down
Loading