#include "ImageApplyColorCastCorrect.h" #include #include #define max(a, b) ((a) > (b) ? (a) : (b)) constexpr auto SIZE_OF_TABLE = 256; CImageApplyColorCastCorrect::CImageApplyColorCastCorrect(const std::vector& points_x, const std::vector& points_y) : m_table(new uchar[SIZE_OF_TABLE]) { createTable(points_x, points_y); } CImageApplyColorCastCorrect::CImageApplyColorCastCorrect(const std::string& fileName) : m_table(new uchar[SIZE_OF_TABLE]) { std::fstream file(fileName, std::ios::in | std::ios::binary); if (file) file.read(reinterpret_cast(m_table), SIZE_OF_TABLE); 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); } 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); } 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); } void CImageApplyColorCastCorrect::apply(std::vector& 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(m_table), SIZE_OF_TABLE); file.close(); } void CImageApplyColorCastCorrect::createTable(const std::vector& points_x, const std::vector& points_y) { int table_temp[256]{}; for (size_t i = 0; i < points_x.size(); i++) { int current_index = static_cast(points_x[i]); if (current_index == 255) current_index = 0; int next_index = static_cast(points_x[(i + 1) % points_x.size()]); double low = points_y[i]; double up = points_y[(i + 1) % points_y.size()]; if (low == 255) low = 0; if (up < low) up += 255; if (next_index < current_index) next_index += 256; int length = next_index - current_index + 1; double step = (up - low) / length; for (int j = 0; j < length; j++) { int temp = (j + current_index) % 256; table_temp[temp] = step * j + low; } 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]; } }