Skip to content

Commit

Permalink
Code to use BST to try to have a gamma value which would produce a we…
Browse files Browse the repository at this point in the history
…ll defined image in terms of contours / image gradient.
  • Loading branch information
s-trinh committed Oct 20, 2024
1 parent 0533dbf commit c942dcd
Show file tree
Hide file tree
Showing 4 changed files with 202 additions and 35 deletions.
25 changes: 16 additions & 9 deletions modules/core/include/visp3/core/vpImageFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ class VISP_EXPORT vpImageFilter
const bool &computeDx = true, const bool &computeDy = true, const bool &normalize = true,
const unsigned int &gaussianKernelSize = 5, const float &gaussianStdev = 2.f,
const unsigned int &apertureGradient = 3,
const vpCannyFilteringAndGradientType &filteringType = CANNY_GBLUR_SOBEL_FILTERING);
const vpCannyFilteringAndGradientType &filteringType = CANNY_GBLUR_SOBEL_FILTERING,
int nb_iters = 1);
#endif

/**
Expand Down Expand Up @@ -157,14 +158,14 @@ class VISP_EXPORT vpImageFilter
const unsigned int &apertureGradient = 3,
const vpCannyFilteringAndGradientType &filteringType = CANNY_GBLUR_SOBEL_FILTERING,
const vpCannyBackendType &backend = CANNY_VISP_BACKEND,
const vpImage<bool> *p_mask = nullptr)
const vpImage<bool> *p_mask = nullptr, int nb_iters = 1)
{
if (backend == CANNY_OPENCV_BACKEND) {
#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC)
cv::Mat cv_I, cv_dIx, cv_dIy;
vpImageConvert::convert(I, cv_I);
computePartialDerivatives(cv_I, cv_dIx, cv_dIy, computeDx, computeDy, normalize, gaussianKernelSize,
static_cast<float>(gaussianStdev), apertureGradient, filteringType);
static_cast<float>(gaussianStdev), apertureGradient, filteringType, nb_iters);
if (computeDx) {
vpImageConvert::convert(cv_dIx, dIx);
}
Expand Down Expand Up @@ -259,7 +260,8 @@ class VISP_EXPORT vpImageFilter
const unsigned int &gaussianKernelSize = 5, const FilterType &gaussianStdev = 2.f,
const unsigned int &apertureGradient = 3,
const vpCannyFilteringAndGradientType &filteringType = CANNY_GBLUR_SOBEL_FILTERING,
const vpCannyBackendType &backend = CANNY_VISP_BACKEND, const vpImage<bool> *p_mask = nullptr) = delete;
const vpCannyBackendType &backend = CANNY_VISP_BACKEND, const vpImage<bool> *p_mask = nullptr,
int nb_iters = 1) = delete;

template <typename ImageType>
inline static void computePartialDerivatives(const vpImage<ImageType> &I,
Expand All @@ -268,7 +270,8 @@ class VISP_EXPORT vpImageFilter
const unsigned int &gaussianKernelSize = 5, const unsigned char &gaussianStdev = 2.f,
const unsigned int &apertureGradient = 3,
const vpCannyFilteringAndGradientType &filteringType = CANNY_GBLUR_SOBEL_FILTERING,
const vpCannyBackendType &backend = CANNY_VISP_BACKEND, const vpImage<bool> *p_mask = nullptr) = delete;
const vpCannyBackendType &backend = CANNY_VISP_BACKEND, const vpImage<bool> *p_mask = nullptr,
int nb_iters = 1) = delete;

template <typename ImageType>
inline static void computePartialDerivatives(const vpImage<ImageType> &I,
Expand All @@ -277,7 +280,8 @@ class VISP_EXPORT vpImageFilter
const unsigned int gaussianKernelSize = 5, const vpRGBa gaussianStdev = vpRGBa(),
const unsigned int apertureGradient = 3,
const vpCannyFilteringAndGradientType &filteringType = CANNY_GBLUR_SOBEL_FILTERING,
const vpCannyBackendType &backend = CANNY_VISP_BACKEND, const vpImage<bool> *p_mask = nullptr) = delete;
const vpCannyBackendType &backend = CANNY_VISP_BACKEND, const vpImage<bool> *p_mask = nullptr,
int nb_iters = 1) = delete;
#else
template <typename FilterType>
inline static void computePartialDerivatives(const vpImage<vpRGBa> &I,
Expand All @@ -286,7 +290,8 @@ class VISP_EXPORT vpImageFilter
const unsigned int &gaussianKernelSize = 5, const FilterType &gaussianStdev = 2.f,
const unsigned int &apertureGradient = 3,
const vpCannyFilteringAndGradientType &filteringType = CANNY_GBLUR_SOBEL_FILTERING,
const vpCannyBackendType &backend = CANNY_VISP_BACKEND, const vpImage<bool> *p_mask = nullptr);
const vpCannyBackendType &backend = CANNY_VISP_BACKEND, const vpImage<bool> *p_mask = nullptr,
int nb_iters = 1);

template <typename ImageType>
inline static void computePartialDerivatives(const vpImage<ImageType> &I,
Expand All @@ -295,7 +300,8 @@ class VISP_EXPORT vpImageFilter
const unsigned int &gaussianKernelSize = 5, const unsigned char &gaussianStdev = 2.f,
const unsigned int &apertureGradient = 3,
const vpCannyFilteringAndGradientType &filteringType = CANNY_GBLUR_SOBEL_FILTERING,
const vpCannyBackendType &backend = CANNY_VISP_BACKEND, const vpImage<bool> *p_mask = nullptr);
const vpCannyBackendType &backend = CANNY_VISP_BACKEND, const vpImage<bool> *p_mask = nullptr,
int nb_iters = 1);

template <typename ImageType>
inline static void computePartialDerivatives(const vpImage<ImageType> &I,
Expand All @@ -304,7 +310,8 @@ class VISP_EXPORT vpImageFilter
const unsigned int gaussianKernelSize = 5, const vpRGBa gaussianStdev = vpRGBa(),
const unsigned int apertureGradient = 3,
const vpCannyFilteringAndGradientType &filteringType = CANNY_GBLUR_SOBEL_FILTERING,
const vpCannyBackendType &backend = CANNY_VISP_BACKEND, const vpImage<bool> *p_mask = nullptr);
const vpCannyBackendType &backend = CANNY_VISP_BACKEND, const vpImage<bool> *p_mask = nullptr,
int nb_iters = 1);
#endif

/**
Expand Down
4 changes: 3 additions & 1 deletion modules/core/src/image/vpImageConvert_opencv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,9 @@ void vpImageConvert::convert(const cv::Mat &src, vpImage<float> &dest, bool flip
}
}
else {
throw vpException(vpException::badValue, "cv::Mat type is not supported!");
std::ostringstream oss;
oss << "cv::Mat type: " << src.type() << " is not supported!";
throw vpException(vpException::badValue, oss.str());
}
}

Expand Down
37 changes: 24 additions & 13 deletions modules/core/src/image/vpImageFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -577,14 +577,19 @@ void vpImageFilter::computePartialDerivatives(const cv::Mat &cv_I,
const bool &computeDx, const bool &computeDy, const bool &normalize,
const unsigned int &gaussianKernelSize, const float &gaussianStdev,
const unsigned int &apertureGradient,
const vpImageFilter::vpCannyFilteringAndGradientType &filteringType)
const vpImageFilter::vpCannyFilteringAndGradientType &filteringType,
int nb_iters)
{
if ((filteringType == vpImageFilter::CANNY_GBLUR_SCHARR_FILTERING)
|| (filteringType == vpImageFilter::CANNY_GBLUR_SOBEL_FILTERING)) {
cv::Mat img_blur;
cv::Mat img_blur, img_blur_tmp;
// Apply Gaussian blur to the image
cv::Size gsz(gaussianKernelSize, gaussianKernelSize);
cv::GaussianBlur(cv_I, img_blur, gsz, gaussianStdev);
img_blur_tmp = cv_I.clone();
for (int iter = 0; iter < nb_iters; iter++) {
cv::GaussianBlur(img_blur_tmp, img_blur, gsz, gaussianStdev);
img_blur_tmp = img_blur.clone();
}

// Compute the gradient of the blurred image
if (filteringType == vpImageFilter::CANNY_GBLUR_SOBEL_FILTERING) {
Expand All @@ -596,10 +601,10 @@ void vpImageFilter::computePartialDerivatives(const cv::Mat &cv_I,
}
}
if (computeDx) {
cv::Sobel(img_blur, cv_dIx, CV_16S, 1, 0, apertureGradient, scale, 0., cv::BORDER_REPLICATE);
cv::Sobel(img_blur, cv_dIx, CV_32F, 1, 0, apertureGradient, scale, 0., cv::BORDER_REPLICATE);
}
if (computeDy) {
cv::Sobel(img_blur, cv_dIy, CV_16S, 0, 1, apertureGradient, scale, 0., cv::BORDER_REPLICATE);
cv::Sobel(img_blur, cv_dIy, CV_32F, 0, 1, apertureGradient, scale, 0., cv::BORDER_REPLICATE);
}
}
else if (filteringType == vpImageFilter::CANNY_GBLUR_SCHARR_FILTERING) {
Expand All @@ -608,10 +613,10 @@ void vpImageFilter::computePartialDerivatives(const cv::Mat &cv_I,
scale = 1. / 32.;
}
if (computeDx) {
cv::Scharr(img_blur, cv_dIx, CV_16S, 1, 0, scale);
cv::Scharr(img_blur, cv_dIx, CV_32F, 1, 0, scale);
}
if (computeDy) {
cv::Scharr(img_blur, cv_dIy, CV_16S, 0, 1, scale);
cv::Scharr(img_blur, cv_dIy, CV_32F, 0, 1, scale);
}
}
}
Expand All @@ -628,7 +633,8 @@ void vpImageFilter::computePartialDerivatives<unsigned char, float>(const vpImag
const unsigned int &gaussianKernelSize, const float &gaussianStdev,
const unsigned int &apertureGradient,
const vpCannyFilteringAndGradientType &filteringType,
const vpCannyBackendType &backend, const vpImage<bool> *p_mask);
const vpCannyBackendType &backend, const vpImage<bool> *p_mask,
int nb_iters);

template
void vpImageFilter::computePartialDerivatives<unsigned char, double>(const vpImage<unsigned char> &I,
Expand All @@ -637,7 +643,8 @@ void vpImageFilter::computePartialDerivatives<unsigned char, double>(const vpIma
const unsigned int &gaussianKernelSize, const double &gaussianStdev,
const unsigned int &apertureGradient,
const vpCannyFilteringAndGradientType &filteringType,
const vpCannyBackendType &backend, const vpImage<bool> *p_mask);
const vpCannyBackendType &backend, const vpImage<bool> *p_mask,
int nb_iters);

template
void vpImageFilter::computePartialDerivatives<float, float>(const vpImage<float> &I,
Expand All @@ -646,7 +653,8 @@ void vpImageFilter::computePartialDerivatives<float, float>(const vpImage<float>
const unsigned int &gaussianKernelSize, const float &gaussianStdev,
const unsigned int &apertureGradient,
const vpCannyFilteringAndGradientType &filteringType,
const vpCannyBackendType &backend, const vpImage<bool> *p_mask);
const vpCannyBackendType &backend, const vpImage<bool> *p_mask,
int nb_iters);

template
void vpImageFilter::computePartialDerivatives<float, double>(const vpImage<float> &I,
Expand All @@ -655,7 +663,8 @@ void vpImageFilter::computePartialDerivatives<float, double>(const vpImage<float
const unsigned int &gaussianKernelSize, const double &gaussianStdev,
const unsigned int &apertureGradient,
const vpCannyFilteringAndGradientType &filteringType,
const vpCannyBackendType &backend, const vpImage<bool> *p_mask);
const vpCannyBackendType &backend, const vpImage<bool> *p_mask,
int nb_iters);

template
void vpImageFilter::computePartialDerivatives<double, float>(const vpImage<double> &I,
Expand All @@ -664,7 +673,8 @@ void vpImageFilter::computePartialDerivatives<double, float>(const vpImage<doubl
const unsigned int &gaussianKernelSize, const float &gaussianStdev,
const unsigned int &apertureGradient,
const vpCannyFilteringAndGradientType &filteringType,
const vpCannyBackendType &backend, const vpImage<bool> *p_mask);
const vpCannyBackendType &backend, const vpImage<bool> *p_mask,
int nb_iters);

template
void vpImageFilter::computePartialDerivatives<double, double>(const vpImage<double> &I,
Expand All @@ -673,7 +683,8 @@ void vpImageFilter::computePartialDerivatives<double, double>(const vpImage<doub
const unsigned int &gaussianKernelSize, const double &gaussianStdev,
const unsigned int &apertureGradient,
const vpCannyFilteringAndGradientType &filteringType,
const vpCannyBackendType &backend, const vpImage<bool> *p_mask);
const vpCannyBackendType &backend, const vpImage<bool> *p_mask,
int nb_iters);

template
float vpImageFilter::computeCannyThreshold<double>(const vpImage<unsigned char> &I, float &lowerThresh,
Expand Down
Loading

0 comments on commit c942dcd

Please sign in to comment.