twain3.0/huagao/ImageProcess/ImageApplyRotation.cpp

133 lines
3.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "ImageApplyRotation.h"
#define USE_TESSERCAT
#ifdef USE_TESSERCAT
#define HG_OCR4_BUILD
#include "hg_ocr4.h"
#endif
CImageApplyRotation::CImageApplyRotation(RotationType rotation, bool isBackTransposed, int dpi, const char* tessdataPath)
: m_rotation(rotation)
, m_backTranspose(isBackTransposed)
, m_dpi(dpi)
, osd(nullptr)
{
if (rotation == RotationType::AutoTextOrientation)
{
#ifdef USE_TESSERCAT
osd = new HG_OCR4();
reinterpret_cast<HG_OCR4*>(osd)->init(tessdataPath, HG_OCR4::Orientation);
#endif
}
}
CImageApplyRotation::~CImageApplyRotation()
{
#ifdef USE_TESSERCAT
if (osd) delete reinterpret_cast<HG_OCR4*>(osd);
#endif
}
void CImageApplyRotation::apply(cv::Mat & pDib, int side)
{
#ifdef LOG
FileTools::write_log("imgprc.txt", "enter CImageApplyRotation apply");
#endif // LOG
if (pDib.empty())
{
#ifdef LOG
FileTools::write_log("imgprc.txt", "exit CImageApplyRotation apply");
#endif // LOG
return;
}
if (m_rotation == RotationType::AutoTextOrientation) //×Ô¶¯Îı¾·½Ïòʶ±ð
{
#ifdef USE_TESSERCAT
if (osd)
{
cv::Mat temp;
if (m_dpi != 200)
{
double scale = 200 / static_cast<double>(m_dpi);
cv::resize(pDib, temp, cv::Size(), scale, scale);
}
else
temp = pDib.clone();
if (temp.channels() == 3)
cv::cvtColor(temp, temp, cv::COLOR_BGR2GRAY);
cv::threshold(temp, temp, 180, 255, cv::THRESH_OTSU);
HG_OCR4* ptr_osd = reinterpret_cast<HG_OCR4*>(osd);
int orientation = ptr_osd->getOrientation(temp.data, temp.cols, temp.rows, temp.channels(), temp.step);
switch (orientation)
{
case 1:
transpose(pDib, pDib);
flip(pDib, pDib, 0);
break;
case 2:
flip(pDib, pDib, 0);
flip(pDib, pDib, 1);
break;
case 3:
transpose(pDib, pDib);
flip(pDib, pDib, 1);
break;
default:
break;
}
}
#endif
}
else if (m_backTranspose && side == 1) //±³ÃæÐýת180
{
if (m_rotation != RotationType::Rotate_180) //Ðýת180¶È
{
if (m_rotation == RotationType::Rotate_90_clockwise || m_rotation == RotationType::Rotate_90_anti_clockwise) //90¡ã -90¡ã
{
transpose(pDib, pDib);
flip(pDib, pDib, m_rotation == RotationType::Rotate_90_clockwise ? 0 : 1);
}
else
{
flip(pDib, pDib, 0);
flip(pDib, pDib, 1);
}
}
}
else //zh
{
if (m_rotation == RotationType::Rotate_90_clockwise || m_rotation == RotationType::Rotate_90_anti_clockwise) //90¡ã -90¡ã
{
transpose(pDib, pDib);
flip(pDib, pDib, m_rotation == RotationType::Rotate_90_clockwise ? 1 : 0);
}
else if (m_rotation == RotationType::Rotate_180)
{
flip(pDib, pDib, 0);
flip(pDib, pDib, 1);
}
}
#ifdef LOG
FileTools::write_log("imgprc.txt", "exit CImageApplyRotation apply");
#endif // LOG
}
void CImageApplyRotation::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);
}
}