#include "ImageApplyColorCastCorrect.h" #include #include #define JIANG_BAOHE //#define G200 #define G300 #define max(a, b) ((a) > (b) ? (a) : (b)) #define min(a, b) ((a) < (b) ? (a) : (b)) constexpr auto SIZE_OF_TABLE = 256; CImageApplyColorCastCorrect::CImageApplyColorCastCorrect() : m_table(new uchar[SIZE_OF_TABLE]) , m_table2(new uchar[SIZE_OF_TABLE]) , m_table3(new uchar[SIZE_OF_TABLE]) { std::vector points_x, points_y; #ifdef G200 points_x = { 2, 9, 15, 18, 22, 34, 50, 63, 78, 92, 103, 114, 126, 137, 140, 147, 154, 163, 176, 200, 225, 248, 251, 254 }; points_y = { 11, 17, 24, 28, 32, 41, 56, 81, 106, 115, 119, 124, 131, 138, 141, 145, 153, 166, 186, 201, 217, 251, 259, 264 }; #endif #ifdef G300 points_x = { 1, 7, 12, 18, 33, 52, 68, 81, 91, 100, 111, 125, 138, 142, 147, 153, 161, 172, 198, 230, 248, 250, 252, 253 }; points_y = { 26, 31, 33, 36, 40, 44, 56, 92, 104, 114, 126, 135, 141, 143, 146, 151, 169, 198, 218, 227, 252, 266, 272, 276 }; #endif createTable(points_x, points_y); points_x = { 0, 180, 210, 255 }; points_y = { 0, 180, 255, 265 }; createTable2(points_x, points_y); points_x = { 0, 230, 255 }; points_y = { 0, 210, 255 }; createTable3(points_x, points_y); } CImageApplyColorCastCorrect::CImageApplyColorCastCorrect(const std::vector& points_x, const std::vector& points_y) : m_table(new uchar[SIZE_OF_TABLE]) , m_table2(new uchar[SIZE_OF_TABLE]) , m_table3(new uchar[SIZE_OF_TABLE]) { createTable(points_x, points_y); } CImageApplyColorCastCorrect::CImageApplyColorCastCorrect(const std::string& fileName) : m_table(new uchar[SIZE_OF_TABLE]) , m_table2(new uchar[SIZE_OF_TABLE]) , m_table3(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]) , m_table2(new uchar[SIZE_OF_TABLE]) , m_table3(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; delete[] m_table2; delete[] m_table3; } 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_BGR2HLS_FULL); cv::Mat hsv_mv[3]; cv::split(hsv, hsv_mv); cv::imwrite("hsv_0.jpg", hsv_mv[0]); cv::Mat lut(256, 1, CV_8UC1, m_table); cv::LUT(hsv_mv[0], lut, hsv_mv[0]); #ifdef JIANG_BAOHE //hsv_mv[2] -= 20; //cv::imwrite("hsv_0.jpg", hsv_mv[0]); //cv::imwrite("hsv_1.jpg", hsv_mv[1]); //cv::imwrite("hsv_2.jpg", hsv_mv[2]); cv::Mat lut2(256, 1, CV_8UC1, m_table2); cv::LUT(hsv_mv[2], lut2, hsv_mv[2]); //hsv_mv[1] -= 20; //cv::Mat lut3(256, 1, CV_8UC1, m_table3); //cv::LUT(hsv_mv[1], lut3, hsv_mv[1]); #endif cv::merge(hsv_mv, 3, pDib); cv::cvtColor(pDib, pDib, cv::COLOR_HLS2BGR_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]; } } void CImageApplyColorCastCorrect::createTable2(const std::vector& points_x, const std::vector& points_y) { memset(m_table2, 255, 256); memset(m_table2, 0, 127); int s = 1; for (int i = 0; i < points_x.size() - s; i += s) { double low = points_y[i]; double up = points_y[i + s]; int start = points_x[i]; int end = points_x[i + s]; int length = end - start; double step = (up - low) / length; for (int j = 0; j < length; j++) m_table2[start + j] = max(0.0, min(255.0, low + j * step)); } } void CImageApplyColorCastCorrect::createTable3(const std::vector& points_x, const std::vector& points_y) { memset(m_table3, 255, 256); memset(m_table3, 0, 127); int s = 1; for (int i = 0; i < points_x.size() - s; i += s) { double low = points_y[i]; double up = points_y[i + s]; int start = points_x[i]; int end = points_x[i + s]; int length = end - start; double step = (up - low) / length; for (int j = 0; j < length; j++) m_table3[start + j] = max(0.0, min(255.0, low + j * step)); } }