twain3/ImageProcess/ImageApplyCvtColor.cpp

58 lines
1.3 KiB
C++

#include "ImageApplyCvtColor.h"
CImageApplyCvtColor::CImageApplyCvtColor(ConversionCodes type)
: m_code(type)
{
}
CImageApplyCvtColor::~CImageApplyCvtColor()
{
}
void CImageApplyCvtColor::apply(cv::Mat& pDib, int side)
{
(void)side;
if (pDib.channels() == 1) return;
if (m_code > 3)
cv::cvtColor(pDib, pDib, static_cast<cv::ColorConversionCodes>(m_code));
else
{
cv::Mat gray(pDib.size(), CV_8UC1);
if (m_code == BGR_MAX)
{
for (size_t y = 0, rows = pDib.rows; y < rows; y++)
{
uchar* ptr_y = pDib.ptr(y);
uchar* gray_y = gray.ptr(y);
for (size_t x = 0, cols = pDib.cols; x < cols; x++)
gray_y[x] = cv::max(ptr_y[x * 3], cv::max(ptr_y[x * 3 + 1], ptr_y[x * 3 + 2]));
}
}
else if (m_code == BGR_MIN)
{
for (size_t y = 0, rows = pDib.rows; y < rows; y++)
{
uchar* ptr_y = pDib.ptr(y);
uchar* gray_y = gray.ptr(y);
for (size_t x = 0, cols = pDib.cols; x < cols; x++)
gray_y[x] = cv::min(ptr_y[x * 3], cv::min(ptr_y[x * 3 + 1], ptr_y[x * 3 + 2]));
}
}
pDib.release();
pDib = gray;
}
}
void CImageApplyCvtColor::apply(std::vector<cv::Mat>& mats, bool isTwoSide)
{
(void)isTwoSide;
int i = 0;
for (cv::Mat& var : mats) {
if (i != 0 && isTwoSide == false)
break;
if (!var.empty())
apply(var, 0);
i++;
}
}