Skip to content
This repository was archived by the owner on Jan 12, 2019. It is now read-only.

reduced usage of magic numbers #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions scan/frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@

DMZ_INTERNAL void scan_card_image(IplImage *y, bool collect_card_number, bool scan_expiry, FrameScanResult *result) {
assert(NULL == y->roi);
assert(y->width == 428);
assert(y->height == 270);
assert(y->width == kCreditCardTargetWidth);
assert(y->height == kCreditCardTargetHeight);
assert(y->depth == IPL_DEPTH_8U);
assert(y->nChannels == 1);

Expand Down
4 changes: 2 additions & 2 deletions scan/n_categorize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "models/generated/modelc_b00bf70c.hpp"

// TODO: gpu for matrix mult?

#include "dmz_constants.h"

typedef Eigen::Matrix<float, 27, 19, Eigen::RowMajor> NumberImage;
typedef Eigen::Matrix<float, 1, 10, Eigen::RowMajor> SingleNumberScores;
Expand Down Expand Up @@ -81,7 +81,7 @@ DMZ_INTERNAL NumberScores number_scores(IplImage *y_strip, NHorizontalSegmentati
uint16_t y_offset = 0;
if(NULL != y_strip->roi) {
y_offset = (uint16_t)y_strip->roi->yOffset;
assert(y_strip->roi->width == 428);
assert(y_strip->roi->width == kCreditCardTargetWidth);
assert(y_strip->roi->xOffset == 0);
}

Expand Down
11 changes: 6 additions & 5 deletions scan/n_hseg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "cv/image_util.h"
#include "cv/morph.h"
#include "opencv2/imgproc/imgproc_c.h"
#include "dmz_constants.h"

static float number_grad_sum_pattern[19] = {
0.26228655f, 0.30289554f, 0.34632607f, 0.38725636f, 0.42745813f, 0.45875135f,
Expand All @@ -33,7 +34,7 @@ typedef struct {

#define SliceU16_MAX UINT16_MAX

typedef Eigen::Matrix<float, 1, 428, Eigen::RowMajor> HorizontalStripPattern;
typedef Eigen::Matrix<float, 1, kCreditCardTargetWidth, Eigen::RowMajor> HorizontalStripPattern;
typedef Eigen::Matrix<float, 1, 19, Eigen::RowMajor> NumberGradSumPattern;

DMZ_INTERNAL NHorizontalSegmentation best_n_hseg_constrained(float *grad_sums, NVerticalSegmentation vseg, NHorizontalSegmentation best, SliceF32 width_slice, SliceU16 offset_slice) {
Expand All @@ -46,7 +47,7 @@ DMZ_INTERNAL NHorizontalSegmentation best_n_hseg_constrained(float *grad_sums, N
for(float width = width_slice.min; width < width_slice.max; width += width_slice.step) {
float pattern_width = vseg.number_pattern_length * width;
uint16_t pattern_offset_max = offset_slice.max;
uint16_t maximum_pattern_offset_max = (uint16_t)(428 - lrintf(pattern_width));
uint16_t maximum_pattern_offset_max = (uint16_t)(kCreditCardTargetWidth - lrintf(pattern_width));
if(pattern_offset_max == SliceU16_MAX || pattern_offset_max > maximum_pattern_offset_max) {
pattern_offset_max = maximum_pattern_offset_max;
}
Expand All @@ -57,7 +58,7 @@ DMZ_INTERNAL NHorizontalSegmentation best_n_hseg_constrained(float *grad_sums, N
for(uint8_t pattern_index = 0; pattern_index < vseg.number_pattern_length; pattern_index++) {
if(vseg.number_pattern[pattern_index]) {
uint16_t center_of_number = (uint16_t)(offset + lrintf(pattern_index * width));
if(center_of_number + 19 < 428) { // shouldn't need this check, just being defensive
if(center_of_number + 19 < kCreditCardTargetWidth) { // shouldn't need this check, just being defensive
pattern.segment<19>(center_of_number) = number_grad_sum_pattern_array;
} else {
in_bounds = false;
Expand Down Expand Up @@ -87,11 +88,11 @@ DMZ_INTERNAL NHorizontalSegmentation best_n_hseg_constrained(float *grad_sums, N

DMZ_INTERNAL NHorizontalSegmentation best_n_hseg(IplImage *y_strip, NVerticalSegmentation vseg) {
// Gradient
IplImage *grad = cvCreateImage(cvSize(428, 27), IPL_DEPTH_8U, 1);
IplImage *grad = cvCreateImage(cvSize(kCreditCardTargetWidth, 27), IPL_DEPTH_8U, 1);
llcv_morph_grad3_2d_cross_u8(y_strip, grad);

// Reduce (sum), normalize
IplImage *grad_sum = cvCreateImage(cvSize(428, 1), IPL_DEPTH_32F, 1); // could sum to IPL_DEPTH_16U and then convert to 32F for normalization, doing it this way for simplicity, will probably get changed during optimization
IplImage *grad_sum = cvCreateImage(cvSize(kCreditCardTargetWidth, 1), IPL_DEPTH_32F, 1); // could sum to IPL_DEPTH_16U and then convert to 32F for normalization, doing it this way for simplicity, will probably get changed during optimization
cvReduce(grad, grad_sum, 0 /* reduce to single row */, CV_REDUCE_SUM);
cvNormalize(grad_sum, grad_sum, 0.0f, 1.0f, CV_MINMAX, NULL);

Expand Down
12 changes: 6 additions & 6 deletions scan/n_vseg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ DMZ_INTERNAL inline void best_segmentation_for_vseg_scores(float *visalike_score
best->pattern_type = NumberPatternUnknown;
best->y_offset = 0;

for(uint16_t y_offset = 0; y_offset < 270; y_offset++) {
for(uint16_t y_offset = 0; y_offset < kCreditCardTargetHeight; y_offset++) {
float visalike_score = visalike_scores[y_offset];
float amexlike_score = amexlike_scores[y_offset];

Expand Down Expand Up @@ -106,14 +106,14 @@ DMZ_INTERNAL NVerticalSegmentation best_n_vseg(IplImage *y) {
IplImage *as_float = cvCreateImage(cvSize(204, 1), IPL_DEPTH_32F, 1);

// Score buffers, to be filled in as needed
float visalike_scores[270];
float visalike_scores[kCreditCardTargetHeight];
memset(visalike_scores, 0, sizeof(visalike_scores));

float amexlike_scores[270];
float amexlike_scores[kCreditCardTargetHeight];
memset(amexlike_scores, 0, sizeof(amexlike_scores));

uint16_t min_y_offset = 0;
uint16_t max_y_offset = 270;
uint16_t max_y_offset = kCreditCardTargetHeight;
uint8_t y_offset_step = 4;

// Initially, calculate every fourth score, to narrow down the area in which we have to work
Expand All @@ -137,8 +137,8 @@ DMZ_INTERNAL NVerticalSegmentation best_n_vseg(IplImage *y) {
// whether it is actually a credit card present or not

// All values must be bounds checked against 270 and (when needed) safely against 0 (using uints!)
min_y_offset = MIN(270, best.y_offset < kFineTuningBuffer ? 0 : best.y_offset - kFineTuningBuffer);
max_y_offset = MIN(270, best.y_offset + kVertSegSumWindowSize + kFineTuningBuffer);
min_y_offset = MIN(kCreditCardTargetHeight, best.y_offset < kFineTuningBuffer ? 0 : best.y_offset - kFineTuningBuffer);
max_y_offset = MIN(kCreditCardTargetHeight, best.y_offset + kVertSegSumWindowSize + kFineTuningBuffer);
y_offset_step = 1;

for(uint16_t y_offset = min_y_offset; y_offset < max_y_offset; y_offset += y_offset_step) {
Expand Down