code_device/hgdriver/ImageProcess/ImageApplyColorCastCorrect.cpp

127 lines
3.3 KiB
C++
Raw Normal View History

2023-02-24 10:07:24 +00:00
#include "ImageApplyColorCastCorrect.h"
#include <fstream>
#include <iostream>
#define max(a, b) ((a) > (b) ? (a) : (b))
constexpr auto SIZE_OF_TABLE = 256;
2023-02-24 10:07:24 +00:00
CImageApplyColorCastCorrect::CImageApplyColorCastCorrect(const std::vector<double>& points_x, const std::vector<double>& points_y)
: m_table(new uchar[SIZE_OF_TABLE])
2023-02-24 10:07:24 +00:00
{
createTable(points_x, points_y);
}
CImageApplyColorCastCorrect::CImageApplyColorCastCorrect(const std::string& fileName)
: m_table(new uchar[SIZE_OF_TABLE])
2023-02-24 10:07:24 +00:00
{
std::fstream file(fileName, std::ios::in | std::ios::binary);
if (file)
file.read(reinterpret_cast<char*>(m_table), SIZE_OF_TABLE);
2023-02-24 10:07:24 +00:00
file.close();
}
CImageApplyColorCastCorrect::CImageApplyColorCastCorrect(const uchar* table_h)
: m_table(new uchar[SIZE_OF_TABLE])
{
memcpy(m_table, table_h, SIZE_OF_TABLE);
}
CImageApplyColorCastCorrect::CImageApplyColorCastCorrect(const int type)
: m_table(new uchar[SIZE_OF_TABLE])
{
if(type == 1)
memcpy(m_table,CIS_DN_PATCH1,SIZE_OF_TABLE);
else
memcpy(m_table,CIS_DN_PATCH2,SIZE_OF_TABLE);
}
2023-02-24 10:07:24 +00:00
CImageApplyColorCastCorrect::~CImageApplyColorCastCorrect(void)
{
delete[] m_table;
}
void CImageApplyColorCastCorrect::setlutdata(const int type)
{
if(type == 1) memcpy(m_table,CIS_DN_PATCH1,SIZE_OF_TABLE);
else memcpy(m_table,CIS_DN_PATCH2,SIZE_OF_TABLE);
}
2023-02-24 10:07:24 +00:00
void CImageApplyColorCastCorrect::apply(cv::Mat& pDib, int side)
{
if (pDib.channels() != 3)
return;
cv::Mat hsv;
cv::cvtColor(pDib, hsv, cv::COLOR_BGR2HSV_FULL);
cv::Mat hsv_mv[3];
cv::split(hsv, hsv_mv);
cv::Mat lut(256, 1, CV_8UC1, m_table);
cv::LUT(hsv_mv[0], lut, hsv_mv[0]);
cv::merge(hsv_mv, 3, pDib);
cv::cvtColor(pDib, pDib, cv::COLOR_HSV2BGR_FULL);
2023-02-24 10:07:24 +00:00
}
void CImageApplyColorCastCorrect::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++;
}
}
void CImageApplyColorCastCorrect::exportTableData(const std::string& fileName)
{
std::fstream file(fileName, std::ios::out | std::ios::binary);
if (file)
file.write(reinterpret_cast<char*>(m_table), SIZE_OF_TABLE);
2023-02-24 10:07:24 +00:00
file.close();
}
void CImageApplyColorCastCorrect::createTable(const std::vector<double>& points_x, const std::vector<double>& points_y)
2023-02-24 10:07:24 +00:00
{
int table_temp[256]{};
2023-02-24 10:07:24 +00:00
for (size_t i = 0; i < points_x.size(); i++)
2023-02-24 10:07:24 +00:00
{
int current_index = static_cast<int>(points_x[i]);
if (current_index == 255)
current_index = 0;
int next_index = static_cast<int>(points_x[(i + 1) % points_x.size()]);
2023-02-24 10:07:24 +00:00
double low = points_y[i];
double up = points_y[(i + 1) % points_y.size()];
if (low == 255)
low = 0;
if (up < low)
up += 255;
2023-02-24 10:07:24 +00:00
if (next_index < current_index)
next_index += 256;
2023-02-24 10:07:24 +00:00
int length = next_index - current_index + 1;
double step = (up - low) / length;
2023-02-24 10:07:24 +00:00
for (int j = 0; j < length; j++)
2023-02-24 10:07:24 +00:00
{
int temp = (j + current_index) % 256;
table_temp[temp] = step * j + low;
2023-02-24 10:07:24 +00:00
}
for (size_t j = 0; j < 256; j++)
if (table_temp[j] > 255)
m_table[j] = table_temp[j] - 255;
else
m_table[j] = table_temp[j];
}
2023-02-24 10:07:24 +00:00
}