int ColorDefinition int color const MinImg preprocessed_image const En

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
int ColorDefinition(int& color,
const MinImg* preprocessed_image,
const EngineSettings& set) {
/*
DECLARE_GUARDED_MINIMG(green);
PROPAGATE_ERROR(CloneDimensionedMinImagePrototype(
&green, preprocessed_image, 1));
int src_channels1[1] = {1};
int dst_channels1[1] = {0};
PROPAGATE_ERROR(CopyMinImageChannels(
&green, preprocessed_image, dst_channels1, src_channels1, 1));
int hist[256];
::memset(hist, 0, 256 * sizeof(*hist));
for (int y = 0; y < green.height; y++) {
uint8_t *pLine = green.pScan0 + y * green.stride;
for (int x = 0; x < green.width; x++) {
hist[pLine[x]]++;
}
}
int threshold = green.width * green.height / 2 + 1;
int cnt = 0;
int median = 0;
for (; median < 256 && cnt < threshold; ++median) {
cnt += hist[median];
}
if (median < set.median_treshold)
color = 0; // red
else
color = 1; // yellow
*/
DECLARE_GUARDED_MINIMG(green);
PROPAGATE_ERROR(CloneDimensionedMinImagePrototype(
&green, preprocessed_image, 1));
int gr_src_channels[1] = {1};
int gr_dst_channels[1] = {0};
PROPAGATE_ERROR(CopyMinImageChannels(
&green, preprocessed_image, gr_dst_channels, gr_src_channels, 1));
DECLARE_GUARDED_MINIMG(red);
PROPAGATE_ERROR(CloneDimensionedMinImagePrototype(
&red, preprocessed_image, 1));
int red_src_channels[1] = {0};
if (preprocessed_image->channels == 4) { // for IPhone BGRA pictures
red_src_channels[0] = 2;
}
int red_dst_channels[1] = {0};
PROPAGATE_ERROR(CopyMinImageChannels(
&red, preprocessed_image, red_dst_channels, red_src_channels, 1));
DECLARE_GUARDED_MINIMG(red_green_diff);
PROPAGATE_ERROR(CloneMinImagePrototype(&red_green_diff, &red));
PROPAGATE_ERROR(MergeImage(&red_green_diff,
&red,
&green,
BIOP_DIF));
int hist[256];
::memset(hist, 0, 256 * sizeof(*hist));
static const uint8_t REFACTOR_ME_T = 127;
int pixel_count = 0;
int x_from = red_green_diff.width / 4;
int y_from = red_green_diff.height / 4;
for (int y = y_from; y < red_green_diff.height - y_from; y++) {
uint8_t *pLine = red_green_diff.pScan0 + y * red_green_diff.stride;
uint8_t *pRedLine = red.pScan0 + y * red.stride;
for (int x = x_from; x < red_green_diff.width - x_from; x++) {
if (pRedLine[x] > REFACTOR_ME_T) {
hist[pLine[x]]++;
pixel_count++;
}
}
}
int threshold = pixel_count / 2 + 1;
int cnt = 0;
int median = 0;
for (; median < 256 && cnt < threshold; ++median) {
cnt += hist[median];
}
double average = 0.0;
for (int i = 0; i < 256; ++i) {
average += 1.0 * i * hist[i];
}
average /= pixel_count;
//////////////
DECLARE_GUARDED_MINIMG(scaled_image);
PROPAGATE_ERROR(CloneResizedMinImagePrototype(&scaled_image, &red_green_diff, 1, 1));
PROPAGATE_ERROR(ScaleImage(&scaled_image, &red_green_diff, QO_PIXEL));
uint8_t *pix = scaled_image.pScan0;
/*
int k = (int)(average + 0.5);
::fprintf(stderr, "%d", k);
////////////*/
if (pix[0] > set.median_treshold)
color = 0; // red
else
color = 1; // yellow
return NO_ERRORS;
}