include algorithm include cmath include vector include minimgapi minim

  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
#include <algorithm>
#include <cmath>
#include <vector>
#include <minimgapi/minimgapi.h>
#include <minimgapi/imgguard.hpp>
#include <minutils/mingeo.h>
#include <minutils/minerr.h>
#include <se-utils/se-utils.h>
#include "segmentation.h"
#include <minimgio/minimgio.h>
namespace prodmark {
int DrawRects (const MinImg *image, const std::vector<MinRect> &rects, uint8_t *color)
{
for (size_t i = 0; i < rects.size (); i++)
{
if (i == 3 || i == 7) {
continue;
}
const MinRect &rect = rects[i];
uint8_t *top_line = image->pScan0 + rect.y * image->stride;
uint8_t *bottom_line = image->pScan0 + (rect.y + rect.height) * image->stride;
for (int x = rect.x; x < rect.x + rect.width; x++)
{
memcpy (top_line + x * image->channels, color, image->channels);
memcpy (bottom_line + x * image->channels, color, image->channels);
}
for (int y = rect.y; y < rect.y + rect.height - 5; y++)
{
uint8_t *line = image->pScan0 + y * image->stride;
memcpy (line + rect.x * image->channels, color, image->channels);
memcpy (line + (rect.x + rect.width) * image->channels, color, image->channels);
}
}
return NO_ERRORS;
}
int Segmentation(SegmentationResult &segmentation_result, const MinImg* zone_image) {
std::vector<int> col(zone_image->height, 0),
row(zone_image->width, 0);
int sum = 0, digit_length = 42, mn = 1e9, base_bot, base_top;
for (int i = 0; i < zone_image->height; ++i) {
for (int j = 0; j < zone_image->width; ++j) {
uint8_t* pix = zone_image->pScan0 + i * zone_image->stride + j;
col[i] += *pix;
}
sum += col[i];
if (i > digit_length - 1) {
sum -= col[i - digit_length];
if (mn > sum) {
mn = sum;
base_bot = i;
}
}
}
base_top = std::max(base_bot - digit_length - 5, 0);
digit_length += 10;
digit_length = std::min(digit_length, zone_image->height - base_top);
MinImg reg = se::utils::ImageRegion(*zone_image, MinRect(0, base_top, zone_image->width, digit_length));
DECLARE_GUARDED_MINIMG(zone);
PROPAGATE_ERROR(CloneMinImagePrototype(&zone, &reg));
PROPAGATE_ERROR(DilateImage(&zone, &reg, 1, 1, BO_VOID));
for (int i = 0; i < digit_length; ++i) {
for (int j = 0; j < 330; ++j) {
uint8_t* pix = zone.pScan0 + i * zone.stride + j;
row[j] += *pix;
}
}
static int d[11] = { 25, 58, 88, 120, 135, 166, 198, 230, 244, 272, 305 };
int mx = 0, good_i;
for (int i = -10; i <= 10; ++i) {
sum = 0;
for (int j = 0; j < 11; ++j) {
sum += row[d[j] + i];
}
if (sum > mx) {
mx = sum;
good_i = i;
}
}
std::vector <MinRect> rects(10);
for (int i = 0; i < 10; ++i) {
rects[i] = MinRect(d[i] + good_i, base_top, 33, digit_length);
}
uint8_t black = 0;
DrawRects(&zone, rects, &black);
/*for (int i = 0; i < 11; ++i) {
MinImg stripe = se::utils::ImageRegion(zone, MinRect(d[i] + good_i, 0, 1, zone.height));
uint8_t black = 0;
FillMinImage(&stripe, &black);
}*/
for (int i = 0; i < 10; ++i) {
if (i == 3 || i == 7)
continue;
segmentation_result.segments.push_back(MinRect(d[i] + good_i, base_top, 33, digit_length));
}
return NO_ERRORS;
}
} //namespace prodmark