Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ Ref<Binarizer> HybridBinarizer::createBinarizer(Ref<LuminanceSource> source) {
}

int HybridBinarizer::initBlockIntegral() {
blockIntegral_ = new Array<int>(width * height);
blockIntegralWidth = subWidth_ + 1;
blockIntegralHeight = subHeight_ + 1;
blockIntegral_ = new Array<int>(blockIntegralWidth * blockIntegralHeight);

int* integral = blockIntegral_->data();

Expand All @@ -64,34 +66,27 @@ int HybridBinarizer::initBlockIntegral() {
// first row only
int rs = 0;

for (int j = 0; j < width; j++) {
for (int j = 0; j < blockIntegralWidth; j++) {
integral[j] = 0;
}

for (int i = 0; i < height; i++) {
integral[i * width] = 0;
}

for (int j = 0; j < subWidth_; j++) {
rs += blocks_[j].threshold;
integral[width + j + 1] = rs;
for (int i = 0; i < blockIntegralHeight; i++) {
integral[i * blockIntegralWidth] = 0;
}

// remaining cells are sum above and to the left
int offset = width;
int offsetBlock = 0;
int offsetIntegral = 0;

for (int i = 1; i < subHeight_; ++i) {
for (int i = 0; i < subHeight_; ++i) {
// therow = grayByte_->getByteRow(i);
offsetBlock = i * subWidth_;

offsetIntegral = (i + 1) * blockIntegralWidth;
rs = 0;

offset += width;

for (int j = 0; j < subWidth_; ++j) {
rs += blocks_[offsetBlock + j].threshold;
integral[offset + j + 1] = rs + integral[offset - width + j + 1];
integral[offsetIntegral + j + 1] = rs + integral[offsetIntegral - blockIntegralWidth + j + 1];
}
}

Expand Down Expand Up @@ -201,8 +196,8 @@ void HybridBinarizer::calculateThresholdForBlock(Ref<ByteMatrix>& _luminances, i
int sum = 0;
// int sum2 = 0;

int offset1 = (top - THRES_BLOCKSIZE) * (subWidth + 1) + left - THRES_BLOCKSIZE;
int offset2 = (top + THRES_BLOCKSIZE + 1) * (subWidth + 1) + left - THRES_BLOCKSIZE;
int offset1 = (top - THRES_BLOCKSIZE) * blockIntegralWidth + left - THRES_BLOCKSIZE;
int offset2 = (top + THRES_BLOCKSIZE + 1) * blockIntegralWidth + left - THRES_BLOCKSIZE;

int blocksize = THRES_BLOCKSIZE * 2 + 1;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class HybridBinarizer : public GlobalHistogramBinarizer {

int subWidth_;
int subHeight_;
int blockIntegralWidth;
int blockIntegralHeight;
Copy link
Member

@alalek alalek Mar 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see code changes related to reading of blockIntegral_ with new layout. I see changes in "generator" only.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It took me a while to understand the logic in here due to unclear meaning of left, top, offset1, offset2

int left = cap(x, THRES_BLOCKSIZE, subWidth - THRES_BLOCKSIZE - 1);
int top = cap(y, THRES_BLOCKSIZE, subHeight - THRES_BLOCKSIZE - 1);
int sum = 0;
// int sum2 = 0;
int offset1 = (top - THRES_BLOCKSIZE) * (subWidth + 1) + left - THRES_BLOCKSIZE;
int offset2 = (top + THRES_BLOCKSIZE + 1) * (subWidth + 1) + left - THRES_BLOCKSIZE;
int blocksize = THRES_BLOCKSIZE * 2 + 1;
sum = blockIntegral[offset1] - blockIntegral[offset1 + blocksize] -
blockIntegral[offset2] + blockIntegral[offset2 + blocksize];

As my understanding, the left, top correspond to centerX, centerY of 5 x 5 block square. And offset1, offset2 are A, C in

A - - B
|        |
C - - D

The blockIntegral is already used with new layout so we don't need to modify anything in there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For more clear, we could change subWidth + 1 to blockIntegralWidth in here

int offset1 = (top - THRES_BLOCKSIZE) * (subWidth + 1) + left - THRES_BLOCKSIZE;
int offset2 = (top + THRES_BLOCKSIZE + 1) * (subWidth + 1) + left - THRES_BLOCKSIZE;


public:
explicit HybridBinarizer(Ref<LuminanceSource> source);
Expand Down