|
|
@@ -0,0 +1,98 @@
|
|
|
+#include "imageprocessor.h"
|
|
|
+
|
|
|
+// Grayscale conversion
|
|
|
+cv::Mat ImageProcessor::grayscale(const cv::Mat& image)
|
|
|
+{
|
|
|
+ cv::Mat grayImage;
|
|
|
+ cv::cvtColor(image, grayImage, cv::COLOR_BGR2GRAY);
|
|
|
+ return grayImage;
|
|
|
+}
|
|
|
+
|
|
|
+// Gaussian blur
|
|
|
+cv::Mat ImageProcessor::gaussianBlur(const cv::Mat& image, const GaussianBlurParams& params)
|
|
|
+{
|
|
|
+ cv::Mat blurredImage;
|
|
|
+ cv::GaussianBlur(image, blurredImage, cv::Size(params.kernelSize, params.kernelSize), params.sigma);
|
|
|
+ return blurredImage;
|
|
|
+}
|
|
|
+
|
|
|
+// Canny edge detection
|
|
|
+cv::Mat ImageProcessor::cannyEdgeDetection(const cv::Mat& image, const CannyEdgeDetectionParams& params)
|
|
|
+{
|
|
|
+ cv::Mat grayImage;
|
|
|
+ cv::cvtColor(image, grayImage, cv::COLOR_BGR2GRAY);
|
|
|
+
|
|
|
+ cv::Mat edgeImage;
|
|
|
+ cv::Canny(grayImage, edgeImage, params.threshold1, params.threshold2);
|
|
|
+ return edgeImage;
|
|
|
+}
|
|
|
+
|
|
|
+// Thresholding
|
|
|
+cv::Mat ImageProcessor::thresholding(const cv::Mat& image, const ThresholdingParams& params)
|
|
|
+{
|
|
|
+ cv::Mat grayImage;
|
|
|
+ cv::cvtColor(image, grayImage, cv::COLOR_BGR2GRAY);
|
|
|
+
|
|
|
+ cv::Mat thresholdedImage;
|
|
|
+ cv::threshold(grayImage, thresholdedImage, static_cast<double>(params.threshold), static_cast<double>(params.maxValue), params.thresholdType);
|
|
|
+ return thresholdedImage;
|
|
|
+}
|
|
|
+
|
|
|
+// Histogram equalization
|
|
|
+cv::Mat ImageProcessor::histogramEqualization(const cv::Mat& image)
|
|
|
+{
|
|
|
+ cv::Mat grayImage;
|
|
|
+ cv::cvtColor(image, grayImage, cv::COLOR_BGR2GRAY);
|
|
|
+
|
|
|
+ cv::Mat equalizedImage;
|
|
|
+ cv::equalizeHist(grayImage, equalizedImage);
|
|
|
+ return equalizedImage;
|
|
|
+}
|
|
|
+
|
|
|
+// Dilate
|
|
|
+cv::Mat ImageProcessor::dilate(const cv::Mat& image, const DilateErodeParams& params)
|
|
|
+{
|
|
|
+ cv::Mat kernel = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(params.kernelSize, params.kernelSize));
|
|
|
+ cv::Mat dilatedImage;
|
|
|
+ cv::dilate(image, dilatedImage, kernel);
|
|
|
+ return dilatedImage;
|
|
|
+}
|
|
|
+
|
|
|
+// Erode
|
|
|
+cv::Mat ImageProcessor::erode(const cv::Mat& image, const DilateErodeParams& params)
|
|
|
+{
|
|
|
+ cv::Mat kernel = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(params.kernelSize, params.kernelSize));
|
|
|
+ cv::Mat erodedImage;
|
|
|
+ cv::erode(image, erodedImage, kernel);
|
|
|
+ return erodedImage;
|
|
|
+}
|
|
|
+
|
|
|
+// Find contours
|
|
|
+cv::Mat ImageProcessor::findContours(const cv::Mat& image, const FindContoursParams& params)
|
|
|
+{
|
|
|
+ // Convert to grayscale if needed
|
|
|
+ cv::Mat grayImage;
|
|
|
+ if (image.channels() > 1)
|
|
|
+ {
|
|
|
+ cv::cvtColor(image, grayImage, cv::COLOR_BGR2GRAY);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ grayImage = image.clone();
|
|
|
+ }
|
|
|
+
|
|
|
+ // Apply threshold to get binary image
|
|
|
+ cv::Mat binaryImage;
|
|
|
+ cv::threshold(grayImage, binaryImage, 128, 255, cv::THRESH_BINARY);
|
|
|
+
|
|
|
+ // Find contours
|
|
|
+ std::vector<std::vector<cv::Point>> contours;
|
|
|
+ std::vector<cv::Vec4i> hierarchy;
|
|
|
+ cv::findContours(binaryImage, contours, hierarchy, params.mode, params.method);
|
|
|
+
|
|
|
+ // Draw contours on a copy of the original image
|
|
|
+ cv::Mat resultImage = image.clone();
|
|
|
+ cv::drawContours(resultImage, contours, -1, cv::Scalar(0, 255, 0), 2);
|
|
|
+
|
|
|
+ return resultImage;
|
|
|
+}
|