code_device/hgdriver/ImageProcess/ImageApplyFadeBackGroundCol...

157 lines
4.7 KiB
C++
Raw Normal View History

2022-07-29 08:41:34 +00:00
#include "ImageApplyFadeBackGroundColor.h"
CImageApplyFadeBackGroudColor::CImageApplyFadeBackGroudColor(int threshold, int offset, int range)
: m_threshold(threshold)
, m_offset(offset)
, m_range(range)
{
memset(m_table1, 255, 768);
memset(m_table1, 0, m_threshold * 3);
memset(m_table2, 255, 256 * 3);
for (size_t i = 0; i < 256; i++)
m_table2[i] = i;
}
CImageApplyFadeBackGroudColor::~CImageApplyFadeBackGroudColor()
{
}
void CImageApplyFadeBackGroudColor::apply(cv::Mat& pDib, int side)
{
if (pDib.channels() != 3)
return;
#if 0
cv::Mat mask;
cv::cvtColor(pDib, mask, cv::COLOR_BGR2GRAY);
cv::threshold(mask, mask, m_threshold, 255, cv::THRESH_BINARY);
//cv::imwrite("mask.jpg", mask);
cv::Mat bgr[3];
cv::split(pDib, bgr);
int histSize = 255;
float range[] = { 0, 255 };
const float* histRange = { range };
cv::Mat hist_bgr[3];
cv::Scalar mean_bgr;
for (size_t i = 0; i < 3; i++)
{
cv::calcHist(&bgr[i], 1, 0, mask, hist_bgr[i], 1, &histSize, &histRange);
double maxVal = 0;
cv::Point maxLoc;
cv::minMaxLoc(hist_bgr[i], NULL, &maxVal, NULL, &maxLoc);
mean_bgr[i] = maxLoc.y;
}
cv::add(pDib, cv::Scalar::all(255 + m_offset) - mean_bgr, pDib, mask);
#else
2023-02-22 03:47:55 +00:00
fadeBackground(pDib.data, pDib.cols, pDib.rows, pDib.step, m_threshold, m_offset, m_range);
2022-07-29 08:41:34 +00:00
#endif
}
2023-02-22 03:47:55 +00:00
/// <summary>
/// <20><><EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD>ɫ<EFBFBD><EFBFBD><E3B7A8><EFBFBD><EFBFBD>֧<EFBFBD><D6A7>24λͼ<CEBB><CDBC>
/// </summary>
/// <param name="data">ͼ<><CDBC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷָ<CDB7><D6B8></param>
/// <param name="bytesPerLine">ÿ<><C3BF><EFBFBD><EFBFBD><EFBFBD>ݴ<EFBFBD>С</param>
/// <param name="height">ͼ<><CDBC><EFBFBD>߶<EFBFBD></param>
/// <param name="threshold"><3E><>ֵ<EFBFBD><D6B5><EFBFBD>ο<EFBFBD>ֵΪ100</param>
/// <param name="offset"><3E>ĸ<EFBFBD><C4B8><EFBFBD>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD>ƫ<EFBFBD><C6AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĭ<EFBFBD><C4AC>Ϊ0<CEAA><30>ֵԽ<D6B5>󣬱<EFBFBD><F3A3ACB1><EFBFBD>Խ<EFBFBD>ף<EFBFBD><D7A3><EFBFBD>֮Խ<D6AE><D4BD></param>
void CImageApplyFadeBackGroudColor::fadeBackground(unsigned char* data, int width, int height, int bytesPerLine, int threshold, int offset, int range)
2022-07-29 08:41:34 +00:00
{
2023-02-22 03:47:55 +00:00
memset(m_table1, 255, 768);
memset(m_table1, 0, threshold * 3);
memset(m_table2, 255, 256 * 3);
for (uint i = 0; i < 256; i++)
m_table2[i] = i;
2022-07-29 08:41:34 +00:00
int hist_bgr[3][256] = { 0 };
unsigned char* mask = new unsigned char[width * height];
unsigned char* ptr_data = data;
unsigned char* ptr_mask = mask;
2023-02-22 03:47:55 +00:00
unsigned char b = 0;
2022-07-29 08:41:34 +00:00
2023-02-22 03:47:55 +00:00
for (uint i = 0; i < height; i++, ptr_data += bytesPerLine, ptr_mask += width)
for (uint j = 0, x = 0; j < width; j++, x += 3)
2022-07-29 08:41:34 +00:00
{
b = m_table1[ptr_data[x] + ptr_data[x + 1] + ptr_data[x + 2]];
ptr_mask[j] = b;
2023-02-22 03:47:55 +00:00
for (uint k = 0; k < 3; k++)
2022-07-29 08:41:34 +00:00
hist_bgr[k][ptr_data[x + k] & b]++;
}
int max_vals[3] = { 0 };
int max_indexes[3] = { 0 };
2023-02-22 03:47:55 +00:00
for (uint i = 1; i < 256; i++)
for (uint j = 0; j < 3; j++)
2022-07-29 08:41:34 +00:00
if (hist_bgr[j][i] > max_vals[j])
{
max_vals[j] = hist_bgr[j][i];
max_indexes[j] = i;
}
2023-02-22 03:47:55 +00:00
uchar table_rgb[3][256] = { 0 };
for (uint i = 0; i < 3; i++)
2022-07-29 08:41:34 +00:00
{
int start = cv::max(max_indexes[i] - range, 0);
int end = cv::min(max_indexes[i] + range, 255);
2023-02-22 03:47:55 +00:00
memset(table_rgb[i] + start, 255, cv::min(end - start + 1, 256 - start));
2022-07-29 08:41:34 +00:00
}
ptr_data = data;
ptr_mask = mask;
2023-02-22 03:47:55 +00:00
for (uint i = 0; i < height; i++, ptr_data += bytesPerLine, ptr_mask += width)
for (uint j = 0, x = 0; j < width; j++, x += 3)
ptr_mask[j] &= table_rgb[0][ptr_data[x]] & table_rgb[1][ptr_data[x + 1]] & table_rgb[2][ptr_data[x + 2]];
2022-07-29 08:41:34 +00:00
unsigned char offset_rgb[3];
2023-02-22 03:47:55 +00:00
for (uint i = 0; i < 3; i++)
2022-07-29 08:41:34 +00:00
offset_rgb[i] = 255 + offset - max_indexes[i];
2023-02-22 03:47:55 +00:00
#if 1
2022-07-29 08:41:34 +00:00
ptr_data = data;
ptr_mask = mask;
2023-02-22 03:47:55 +00:00
for (uint i = 0; i < height; i++, ptr_data += bytesPerLine, ptr_mask += width)
for (uint j = 0, x = 0; j < width; j++, x += 3)
for (uint k = 0; k < 3; k++)
ptr_data[x + k] = m_table2[(int)ptr_data[x + k] + (offset_rgb[k] & ptr_mask[j])];
#else
for (uint c = 0; c < 3; c++)
2022-07-29 08:41:34 +00:00
{
2023-02-22 03:47:55 +00:00
ptr_data = data + c;
ptr_mask = mask;
for (uint y = 0; y < height; y++)
2022-07-29 08:41:34 +00:00
{
2023-02-22 03:47:55 +00:00
int ptr_x = 0;
for (uint x = 0; x < width; x++)
{
ptr_data[ptr_x] = m_table2[(int)ptr_data[ptr_x] + (offset_rgb[c] & ptr_mask[x])];
ptr_x += 3;
}
ptr_data += bytesPerLine;
ptr_mask += width;
2022-07-29 08:41:34 +00:00
}
}
2023-02-22 03:47:55 +00:00
#endif
2022-07-29 08:41:34 +00:00
delete[] mask;
}
void CImageApplyFadeBackGroudColor::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++;
}
}