67 lines
1.4 KiB
C++
67 lines
1.4 KiB
C++
#include "ContaminationDetection.h"
|
|
#include <vector>
|
|
#include <cmath>
|
|
#include <cstring>
|
|
|
|
#define MIN(a, b) ((a) < (b) ? (a) : (b))
|
|
|
|
ContaminationDetection::ContaminationDetection()
|
|
{
|
|
}
|
|
|
|
int ContaminationDetection::detect1(const uchar *data1, const uchar *data2, int length, uchar threshold1, uchar threshold2, int width)
|
|
{
|
|
int width_temp = 0;
|
|
for (size_t i = 0; i < length; i++)
|
|
{
|
|
if (MIN(data1[i], data2[i]) > threshold1)
|
|
return 1;
|
|
|
|
if (MIN(data1[i], data2[i]) > threshold2)
|
|
{
|
|
width_temp++;
|
|
if (width_temp > width)
|
|
return 2;
|
|
}
|
|
else
|
|
width_temp = 0;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int ContaminationDetection::detect2(const uchar *data, int width, int height, int channels, uchar color, int length)
|
|
{
|
|
uchar *ptr = const_cast<uchar *>(data);
|
|
std::vector<int> sum_threshold;
|
|
sum_threshold.resize(height);
|
|
|
|
uchar table[256];
|
|
memset(table, 1, 256);
|
|
memset(table, 0, color);
|
|
|
|
int sum, bit_sum;
|
|
for (size_t y = 0, bytePerLine = width * channels; y < height; y++)
|
|
{
|
|
sum = 0;
|
|
for (size_t x = 0; x < bytePerLine; x += channels)
|
|
{
|
|
bit_sum = 0;
|
|
for (size_t i = 0; i < channels; i++)
|
|
bit_sum |= table[ptr[x + i]];
|
|
|
|
sum += bit_sum;
|
|
}
|
|
sum_threshold[y] = sum;
|
|
ptr += bytePerLine;
|
|
}
|
|
|
|
if (sum_threshold[0] > length || sum_threshold[height - 1] > length)
|
|
{
|
|
if (std::abs(sum_threshold[0] - sum_threshold[height - 1]) > 10)
|
|
return 1;
|
|
else
|
|
return 2;
|
|
}
|
|
|
|
return 0;
|
|
} |