twain3.0/huagao/ImageProcess/ImageApplySharpen.cpp

63 lines
1.4 KiB
C++

#include "stdafx.h"
#include "ImageApplySharpen.h"
using namespace cv;
CImageApplySharpen::CImageApplySharpen(int sharpentype)
{
sharpenType = sharpentype;
kSize = (sharpentype == FilterMethod::Sharpen || sharpentype == FilterMethod::AverBlur) ? 5 : 9;
}
CImageApplySharpen::~CImageApplySharpen()
{
}
void CImageApplySharpen::apply(cv::Mat & pDib, int side)
{
#ifdef LOG
FileTools::write_log("imgprc.txt", "enter CImageApplySharpen apply");
#endif // LOG
switch (sharpenType)
{
case CImageApplySharpen::Sharpen:
case CImageApplySharpen::Sharpen_More:
sharpen(pDib, kSize);
break;
case CImageApplySharpen::AverBlur:
case CImageApplySharpen::AverBlur_More:
averblur(pDib, static_cast<int>(kSize));
break;
}
#ifdef LOG
FileTools::write_log("imgprc.txt", "exit CImageApplySharpen apply");
#endif // LOG
}
void CImageApplySharpen::apply(std::vector<cv::Mat>& mats, bool isTwoSide)
{
if (mats.empty()) return;
if (!mats[0].empty())
apply(mats[0], 0);
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);
}