0
$\begingroup$

I am struggling to debug a segmentation fault. Any help is appreciated! The node just crashes whenever I launch it and following is the seg fault I am getting when running through gdb:

Thread 7 "nodelet" received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0xfffff19ac170 (LWP 12693)]
0x0000ffffe1a95808 in cv::findContours(cv::_InputArray const&, cv::_OutputArray const&, int, int, cv::Point_<int>) ()
   from /lib/aarch64-linux-gnu/libopencv_imgproc.so.4.2

Posting snipet of the code where I am getting the error:

#include <poll_detect/poll_detect.h>

PollDetect::PollDetect(const std::string& cameraName, ros::NodeHandle& nh)
    : m_CameraName_(cameraName) {

  image_sub = nh.subscribe("/camera_1/image_raw", 10, &PollDetect::inputImageCb, this);
  image_pub = nh.advertise<sensor_msgs::Image>("/camera_1/pub_image", 10);
}

// Input image callback
void PollDetect::inputImageCb(const sensor_msgs::Image::ConstPtr& msg) {
  cv::Mat input = convertToCvMat(msg);
  cv::Mat filteredImage;

  apply(input, filteredImage);
  std::vector<cv::Rect> detectedPoll = detect(filteredImage);
}

// Convert ROS image to OpenCV image
cv::Mat PollDetect::convertToCvMat(const sensor_msgs::ImageConstPtr& imageMsg) {
  cv_bridge::CvImagePtr cvPtr;
  try {
    cvPtr = cv_bridge::toCvCopy(imageMsg, sensor_msgs::image_encodings::BGR8);
  } catch (cv_bridge::Exception& e) {
    ROS_ERROR("cv_bridge exception: %s", e.what());
    return cv::Mat();
  }
  return cvPtr->image;
}

// Convert OpenCV image to ROS image
sensor_msgs::Image PollDetect::convertToRosImage(const cv::Mat& cvImage) {
  return *cv_bridge::CvImage(std_msgs::Header(), "mono8", cvImage).toImageMsg();
}

// Apply HSV filter
void PollDetect::apply(const cv::Mat& input, cv::Mat& output) {
  cv::Mat hsvImage;
  cv::cvtColor(input, hsvImage, cv::COLOR_BGR2HSV);
  cv::inRange(hsvImage, m_LowerBounds_, m_UpperBounds_, output);
}

// Poll detector
std::vector<cv::Rect> PollDetect::detect(const cv::Mat& input) {
  std::vector<std::vector<cv::Point>> contours;
  std::vector<cv::Rect> detections;
  cv::findContours(input, contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);
  std::transform(contours.cbegin(), contours.cend(), std::back_inserter(detections),
                 [](const std::vector<cv::Point>& contour) { return cv::boundingRect(contour); });

  return detections;
}
$\endgroup$
4
  • $\begingroup$ Hi, it seems you're passing the wrong input image object to the cv::findContours function. You've accidentally passed a member variable of PollDetect class image instead of input $\endgroup$ Commented Sep 30, 2023 at 7:32
  • $\begingroup$ Thanks for pointing that out. It was a copy paste error on my end.. I have updated it to input in my question.. still getting the segmentation fault $\endgroup$
    – nebula
    Commented Sep 30, 2023 at 12:23
  • $\begingroup$ Hmm, I can't seem to be able to reproduce your issue. I've copy pasted the code and did some minor changes to compile for ROS2 Humble, but everything seems to run fine. The error isn't very helpful either, but somehow it leads me to believe the problem is not in the code pasted here. $\endgroup$ Commented Sep 30, 2023 at 20:46
  • $\begingroup$ I figured it was a OpenCV version mismatch on the computer that was crashing the node :( $\endgroup$
    – nebula
    Commented Oct 6, 2023 at 20:45

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.