I have questions regarding this code:
As the cv::Mat
copy constructor involves only pointer copying due to the reference counting mechanism, in the Apply
method I return by copy instead of explicitly using std::move
. This is because I also wanted to avoid moving the data member from the class.
Also, in the getters, I chose not to return by copy since the client code could modify the internal data by having a pointer. Therefore, I used a const reference
. Additionally, for getters, I used reference qualifiers because returning a reference from instances of the rvalue category could cause dangling in some situations.
Am I right? are designs accurate?
EDIT Regarding why not a stand-alone function:
I considered using a class to encapsulate everything instead of having separate functions. Does that make sense?
load an image Apply CLAHE
converted to grayscale.
apply Gaussian blur.
apply Canny edge detection.
apply Hough Line Transform
List if lines it finds Draw lines
// Header
#ifndef GAUSSIANBLURFILTER_H #define GAUSSIANBLURFILTER_H #include <opencv2/opencv.hpp> #include <optional> class GaussianBlurFilter { public: GaussianBlurFilter(); // Disables copy and move constructors. GaussianBlurFilter(const GaussianBlurFilter&) = delete; cv::Mat getBlurredImage() && { return std::move(m_blurredImage); } const cv::Mat& getBlurredImage() const & { return m_blurredImage; } [[nodiscard]] std::optional<cv::Mat> Apply(const cv::Mat& src); private: constexpr static double SIGMA = 2.5; constexpr static int KERNEL_DIMENSION = 3; inline const static cv::Size KERNEL_SIZE = cv::Size(KERNEL_DIMENSION, KERNEL_DIMENSION); cv::Mat m_blurredImage; };
// source
#include "gaussianblurfilter.h" GaussianBlurFilter::GaussianBlurFilter() : m_blurredImage() {} [[nodiscard]] std::optional<cv::Mat> GaussianBlurFilter::Apply(const cv::Mat& src) { if (src.empty()) { return std::nullopt; } cv::GaussianBlur(src, m_blurredImage, GaussianBlurFilter::KERNEL_SIZE, GaussianBlurFilter::SIGMA); return m_blurredImage; }
class
in my opinion. If you want to do more transforms, then the answer is "maybe", but you should then create a new question on Code Review with the full code you want reviewed.\$\endgroup\$