From 841728fda1b4d222af1922e50e57c9e12b2bc008 Mon Sep 17 00:00:00 2001 From: lovelyyoung <1002639516@qq.com> Date: Thu, 12 Mar 2020 14:37:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=BF=E6=8D=A2=E5=9B=BE=E5=83=8F=E5=A4=84?= =?UTF-8?q?=E7=90=86=E9=94=90=E5=8C=96=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- huagao/ImageProcess/ImageApplySharpen.cpp | 45 ++++++++++++++++++----- huagao/ImageProcess/ImageApplySharpen.h | 20 ++++++++-- 2 files changed, 51 insertions(+), 14 deletions(-) diff --git a/huagao/ImageProcess/ImageApplySharpen.cpp b/huagao/ImageProcess/ImageApplySharpen.cpp index d847c34c..22876ff7 100644 --- a/huagao/ImageProcess/ImageApplySharpen.cpp +++ b/huagao/ImageProcess/ImageApplySharpen.cpp @@ -1,10 +1,9 @@ #include "ImageApplySharpen.h" - -CImageApplySharpen::CImageApplySharpen() - : kernel(5, 5, CV_32FC1) +using namespace cv; +CImageApplySharpen::CImageApplySharpen(int sharpentype) { - float kernel_data[] = { -0.1f, 0, 0, 0, -0.1f, 0, 0, 0, 0, 0, 0, 0, 1.5f, 0, 0, 0, 0, 0, 0, 0, -0.1f, 0, 0, 0, -0.1f }; - memcpy(kernel.data, kernel_data, sizeof(float) * 25); + sharpenType = sharpentype; + kSize = (sharpentype == FilterMethod::Sharpen || sharpentype == FilterMethod::AverBlur) ? 5 : 9; } CImageApplySharpen::~CImageApplySharpen() @@ -17,9 +16,18 @@ void CImageApplySharpen::apply(cv::Mat & pDib, int side) FileTools::write_log("imgprc.txt", "enter CImageApplySharpen apply"); #endif // LOG - if (pDib.empty()) return; + switch (sharpenType) + { + case CImageApplySharpen::Sharpen: + case CImageApplySharpen::Sharpen_More: + sharpen(pDib, kSize); + break; + case CImageApplySharpen::AverBlur: + case CImageApplySharpen::AverBlur_More: + averblur(pDib, static_cast(kSize)); + break; + } - cv::filter2D(pDib, pDib, pDib.depth(), kernel); #ifdef LOG FileTools::write_log("imgprc.txt", "exit CImageApplySharpen apply"); #endif // LOG @@ -29,8 +37,25 @@ void CImageApplySharpen::apply(std::vector& mats, bool isTwoSide) { if (mats.empty()) return; - apply(mats[0], 0); + if (!mats[0].empty()) + apply(mats[0], 0); - if (isTwoSide && mats.size() > 1) - apply(mats[1], 1); + if (isTwoSide && mats.size() > 1) { + if (!mats[1].empty()) + apply(mats[1], 1); + } +} + +void CImageApplySharpen::averblur(Mat& src, int kSize) +{ + blur(src, src, Size(kSize, kSize)); +} + +void CImageApplySharpen::sharpen(Mat& src, float kSize) +{ + float other = (1.0f - kSize) / 4; + float kernel_data[] = { 0, other, 0, other, kSize, other, 0, other, 0 }; + Mat kernel(3, 3, CV_32FC1, kernel_data); + + filter2D(src, src, src.depth(), kernel); } diff --git a/huagao/ImageProcess/ImageApplySharpen.h b/huagao/ImageProcess/ImageApplySharpen.h index 5a2de22c..9401b9ef 100644 --- a/huagao/ImageProcess/ImageApplySharpen.h +++ b/huagao/ImageProcess/ImageApplySharpen.h @@ -3,18 +3,30 @@ #include "ImageApply.h" -class CImageApplySharpen : public CImageApply +class CImageApplySharpen : + public CImageApply { public: - CImageApplySharpen(); - + enum FilterMethod + { + None, + Sharpen, + Sharpen_More, + AverBlur, + AverBlur_More + }; + CImageApplySharpen(int sharpentype); virtual ~CImageApplySharpen(); virtual void apply(cv::Mat& pDib, int side); virtual void apply(std::vector& mats, bool isTwoSide); private: - cv::Mat kernel; + void averblur(cv::Mat& src, int kSize); + void sharpen(cv::Mat& src, float kSize); +private: + int sharpenType; + float kSize; }; #endif // !IMAGE_APPLY_SHARPEN_H