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

Add short support with vpImageConvert::convert() cv::Mat to vpImage<float> #1488

Conversation

s-trinh
Copy link
Contributor

@s-trinh s-trinh commented Oct 21, 2024

Without this quick fix, the following code cannot run:

#include <iostream>
#include <visp3/core/vpConfig.h>
#include <visp3/core/vpIoTools.h>
#include <visp3/core/vpCannyEdgeDetection.h>
#include <visp3/io/vpImageIo.h>

int main(int argc, const char **argv)
{
  std::cout << "getBuildInformation()\n" << vpIoTools::getBuildInformation() << std::endl;

  int gaussianKernelSize = 3;
  float gaussianStdev = 0.5f;
  int apertureSize = 3;
  vpImageFilter::vpCannyFilteringAndGradientType filteringType = vpImageFilter::CANNY_GBLUR_SOBEL_FILTERING;
  // Canny parameters
  float lowerThresh = -1.;
  float upperThresh = -1.;
  float lowerThreshRatio = 0.6f;
  float upperThreshRatio = 1.5f;
  vpImageFilter::vpCannyBackendType backend = vpImageFilter::vpCannyBackendType::CANNY_OPENCV_BACKEND;
  // vpImageFilter::vpCannyBackendType backend = vpImageFilter::vpCannyBackendType::CANNY_VISP_BACKEND;

  vpCannyEdgeDetection cannyDetector(gaussianKernelSize, gaussianStdev, apertureSize,
                                    lowerThresh, upperThresh, lowerThreshRatio, upperThreshRatio,
                                    filteringType);

  vpImage<unsigned char> I_gray, I_canny_visp;
  vpImageIo::read(I_gray, "/tmp/Klimt.png");
  std::cout << "I_gray=" << I_gray.getWidth() << "x" << I_gray.getHeight() << std::endl;

  vpImageFilter::canny(I_gray, I_canny_visp, gaussianKernelSize, lowerThresh, upperThresh, apertureSize,
        gaussianStdev, lowerThreshRatio, upperThreshRatio, false,
        backend,
        vpImageFilter::CANNY_GBLUR_SOBEL_FILTERING);

  vpImage<float> dIx, dIy;
  std::cout << "vpImageFilter::computePartialDerivatives" << std::endl;
  vpImageFilter::computePartialDerivatives(I_gray, dIx, dIy, true, true, true, gaussianKernelSize, gaussianStdev,
      apertureSize, filteringType, backend);

  // Set the gradients of the vpCannyEdgeDetection
  std::cout << "cannyDetector.setGradients(dIx, dIy)" << std::endl;
  cannyDetector.setGradients(dIx, dIy);
  std::cout << "cannyDetector.detect(I_gray)" << std::endl;
  I_canny_visp = cannyDetector.detect(I_gray);

  vpImageIo::write(I_canny_visp, "/tmp/I_res.png");

  return EXIT_SUCCESS;
}

This is because

cv::Sobel(img_blur, cv_dIx, CV_16S, 1, 0, apertureGradient, scale, 0., cv::BORDER_REPLICATE);
returns a CV_16S mat but there is no conversion from CV_16S to vpImage<float> needed here
vpImageConvert::convert(cv_dIx, dIx);

Computing Sobel with CV_32F datatype would produce an issue here

cv::Canny(cv_dx, cv_dy, edges_cvmat, lowerCannyThresh, upperCannyThresh, false);
since cv::Canny expects CV_16S type.


Maybe extend void vpImageConvert::convert(const cv::Mat &src, vpImage<float> &dest, bool flip) to not only float type but also CV_8U, CV_8S, etc?

Another possibility should be to compute the image gradient with CV_32F precision and add this conversion before calling cv::Canny():

    cv::Mat cv_dx_2 = cv_dx, cv_dy_2 = cv_dy;
    if (cv_dx.type() == CV_32F) {
      double min_val = 0, max_val = 0;
      cv::minMaxLoc(cv_dx, &min_val, &max_val);
      double alpha = 65535 / (max_val - min_val);
      double beta = -alpha * min_val;
      cv_dx.convertTo(cv_dx_2, CV_16S, alpha, beta);
    }
    if (cv_dy.type() == CV_32F) {
      double min_val = 0, max_val = 0;
      cv::minMaxLoc(cv_dy, &min_val, &max_val);
      double alpha = 65535 / (max_val - min_val);
      double beta = -alpha * min_val;
      cv_dx.convertTo(cv_dy_2, CV_16S, alpha, beta);
    }

…Image<float> &dest, bool flip) function. This allows using the OpenCV backend with Canny detection.
Copy link

codecov bot commented Oct 21, 2024

Codecov Report

Attention: Patch coverage is 0% with 7 lines in your changes missing coverage. Please review.

Project coverage is 53.87%. Comparing base (520efed) to head (29b54e8).
Report is 16 commits behind head on master.

Files with missing lines Patch % Lines
modules/core/src/image/vpImageConvert_opencv.cpp 0.00% 7 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1488      +/-   ##
==========================================
- Coverage   53.88%   53.87%   -0.01%     
==========================================
  Files         442      442              
  Lines       53778    53784       +6     
==========================================
  Hits        28978    28978              
- Misses      24800    24806       +6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@fspindle fspindle merged commit b4ad052 into lagadic:master Nov 4, 2024
76 of 78 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants