139 lines
3.1 KiB
C++
139 lines
3.1 KiB
C++
#include "ImageApplyRotation.h"
|
||
|
||
#ifdef _WIN32
|
||
#ifdef USE_HANWANG
|
||
#include<sstream>
|
||
#endif
|
||
#endif
|
||
|
||
CImageApplyRotation::CImageApplyRotation(RotationType rotation, bool isBackTransposed, int dpi, const char* tessdataPath)
|
||
: m_rotation(rotation)
|
||
, m_backTranspose(isBackTransposed)
|
||
, m_dpi(dpi)
|
||
#ifdef _WIN32
|
||
#ifdef USE_HANWANG
|
||
, m_ocr(nullptr)
|
||
#endif
|
||
#endif
|
||
{
|
||
#ifdef _WIN32
|
||
#ifdef USE_HANWANG
|
||
if (tessdataPath != nullptr)
|
||
{
|
||
std::wstringstream wss;
|
||
wss << tessdataPath;
|
||
m_ocr = new HG_OCR(wss.str().c_str());
|
||
}
|
||
#endif
|
||
#endif
|
||
}
|
||
|
||
CImageApplyRotation::~CImageApplyRotation()
|
||
{
|
||
#ifdef _WIN32
|
||
#ifdef USE_HANWANG
|
||
if (m_ocr)
|
||
delete m_ocr;
|
||
#endif
|
||
#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) //<2F>Զ<EFBFBD><D4B6>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD>ʶ<EFBFBD><CAB6>
|
||
{
|
||
#ifdef USE_HANWANG
|
||
cv::Mat temp;
|
||
if (m_dpi != 200)
|
||
{
|
||
double scale = 200 / static_cast<double>(m_dpi);
|
||
int new_w = static_cast<int>(pDib.cols * scale) / 4 * 4;
|
||
int new_h = pDib.rows * scale;
|
||
cv::resize(pDib, temp, cv::Size(new_w, new_h));
|
||
}
|
||
else
|
||
temp = pDib(cv::Rect(0, 0, pDib.cols / 4 * 4, pDib.rows)).clone();
|
||
|
||
if (temp.channels() == 3)
|
||
cv::cvtColor(temp, temp, cv::COLOR_BGR2GRAY);
|
||
cv::threshold(temp, temp, 180, 255, cv::THRESH_OTSU);
|
||
|
||
int orientation = 0;
|
||
if (m_ocr)
|
||
orientation = m_ocr->orientation(temp.data, temp.cols, temp.rows, temp.channels());
|
||
|
||
switch (orientation)
|
||
{
|
||
case 90:
|
||
cv::transpose(pDib, pDib);
|
||
cv::flip(pDib, pDib, 0);
|
||
break;
|
||
case 180:
|
||
cv::flip(pDib, pDib, 0);
|
||
cv::flip(pDib, pDib, 1);
|
||
break;
|
||
case 270:
|
||
cv::transpose(pDib, pDib);
|
||
cv::flip(pDib, pDib, 1);
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
#endif
|
||
}
|
||
else if (m_backTranspose && side == 1) //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת180
|
||
{
|
||
if (m_rotation != RotationType::Rotate_180) //<2F><>ת180<38><30>
|
||
{
|
||
if (m_rotation == RotationType::Rotate_90_clockwise || m_rotation == RotationType::Rotate_90_anti_clockwise) //90<39><30> -90<39><30>
|
||
{
|
||
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<39><30> -90<39><30>
|
||
{
|
||
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)
|
||
{
|
||
(void)isTwoSide;
|
||
int i = 0;
|
||
for (cv::Mat& var : mats) {
|
||
if (!var.empty()) {
|
||
apply(var, i);
|
||
i++;
|
||
}
|
||
}
|
||
} |