Passing blackbox tests aztec

This commit is contained in:
Henry Schimke
2022-09-30 15:53:15 -05:00
parent b1051f01df
commit 438ae9f588
4 changed files with 36 additions and 25 deletions

View File

@@ -76,6 +76,7 @@ impl Detector {
* @throws NotFoundException if no Aztec Code can be found * @throws NotFoundException if no Aztec Code can be found
*/ */
pub fn detect(&mut self, is_mirror: bool) -> Result<AztecDetectorRXingResult, Exceptions> { pub fn detect(&mut self, is_mirror: bool) -> Result<AztecDetectorRXingResult, Exceptions> {
// dbg!(self.image.to_string());
// 1. Get the center of the aztec matrix // 1. Get the center of the aztec matrix
let p_center = self.get_matrix_center(); let p_center = self.get_matrix_center();

View File

@@ -97,7 +97,6 @@ impl BufferedImageLuminanceSource {
// } // }
// } // }
Self { Self {
image: DynamicImage::from(image.to_luma8()), image: DynamicImage::from(image.to_luma8()),
@@ -191,7 +190,7 @@ impl LuminanceSource for BufferedImageLuminanceSource {
&self.image.to_luma8(), &self.image.to_luma8(),
MINUS_45_IN_RADIANS, MINUS_45_IN_RADIANS,
imageproc::geometric_transformations::Interpolation::Nearest, imageproc::geometric_transformations::Interpolation::Nearest,
Luma([255; 1]), Luma([u8::MAX/2; 1]),
); );
let new_img = DynamicImage::from(img); let new_img = DynamicImage::from(img);

View File

@@ -3899,33 +3899,33 @@ impl HybridBinarizer {
*/ */
fn calculateThresholdForBlock( fn calculateThresholdForBlock(
luminances: &[u8], luminances: &[u8],
subWidth: u32, sub_width: u32,
subHeight: u32, sub_height: u32,
width: u32, width: u32,
height: u32, height: u32,
blackPoints: &Vec<Vec<u32>>, black_points: &Vec<Vec<u32>>,
matrix: &mut BitMatrix, matrix: &mut BitMatrix,
) { ) {
let maxYOffset = height - HybridBinarizer::BLOCK_SIZE as u32; let maxYOffset = height - HybridBinarizer::BLOCK_SIZE as u32;
let maxXOffset = width - HybridBinarizer::BLOCK_SIZE as u32; let maxXOffset = width - HybridBinarizer::BLOCK_SIZE as u32;
for y in 0..subHeight { for y in 0..sub_height {
// for (int y = 0; y < subHeight; y++) { // for (int y = 0; y < subHeight; y++) {
let mut yoffset = y << HybridBinarizer::BLOCK_SIZE_POWER; let mut yoffset = y << HybridBinarizer::BLOCK_SIZE_POWER;
if yoffset > maxYOffset { if yoffset > maxYOffset {
yoffset = maxYOffset; yoffset = maxYOffset;
} }
let top = Self::cap(y, subHeight - 3); let top = Self::cap(y, sub_height - 3);
for x in 0..subWidth { for x in 0..sub_width {
// for (int x = 0; x < subWidth; x++) { // for (int x = 0; x < subWidth; x++) {
let mut xoffset = x << HybridBinarizer::BLOCK_SIZE_POWER; let mut xoffset = x << HybridBinarizer::BLOCK_SIZE_POWER;
if xoffset > maxXOffset { if xoffset > maxXOffset {
xoffset = maxXOffset; xoffset = maxXOffset;
} }
let left = Self::cap(x, subWidth - 3); let left = Self::cap(x, sub_width - 3);
let mut sum = 0; let mut sum = 0;
for z in -2i32..=2 { for z in -2i32..=2 {
// for (int z = -2; z <= 2; z++) { // for (int z = -2; z <= 2; z++) {
let blackRow = &blackPoints[(top as i32 + z) as usize]; let blackRow = &black_points[(top as i32 + z) as usize];
sum += blackRow[(left - 2) as usize] sum += blackRow[(left - 2) as usize]
+ blackRow[(left - 1) as usize] + blackRow[(left - 1) as usize]
+ blackRow[left as usize] + blackRow[left as usize]
@@ -3999,10 +3999,12 @@ impl HybridBinarizer {
xoffset = maxXOffset as u32; xoffset = maxXOffset as u32;
} }
let mut sum = 0u32; let mut sum = 0u32;
let mut min = 0; let mut min = 0xff;
let mut max = 0xFF; let mut max = 0;
let mut offset = yoffset * width + xoffset; let mut offset = yoffset * width + xoffset;
for yy in 0..HybridBinarizer::BLOCK_SIZE { let mut yy = 0;
while yy < HybridBinarizer::BLOCK_SIZE {
// for (int yy = 0, offset = yoffset * width + xoffset; yy < HybridBinarizer::BLOCK_SIZE; yy++, offset += width) { // for (int yy = 0, offset = yoffset * width + xoffset; yy < HybridBinarizer::BLOCK_SIZE; yy++, offset += width) {
for xx in 0..HybridBinarizer::BLOCK_SIZE { for xx in 0..HybridBinarizer::BLOCK_SIZE {
// for (int xx = 0; xx < HybridBinarizer::BLOCK_SIZE; xx++) { // for (int xx = 0; xx < HybridBinarizer::BLOCK_SIZE; xx++) {
@@ -4020,16 +4022,19 @@ impl HybridBinarizer {
if (max - min) as usize > HybridBinarizer::MIN_DYNAMIC_RANGE { if (max - min) as usize > HybridBinarizer::MIN_DYNAMIC_RANGE {
// finish the rest of the rows quickly // finish the rest of the rows quickly
offset += width; offset += width;
for _yy_s in yy + 1..HybridBinarizer::BLOCK_SIZE { yy+=1;
while yy < HybridBinarizer::BLOCK_SIZE {
// for (yy++, offset += width; yy < HybridBinarizer::BLOCK_SIZE; yy++, offset += width) { // for (yy++, offset += width; yy < HybridBinarizer::BLOCK_SIZE; yy++, offset += width) {
for xx in 0..HybridBinarizer::BLOCK_SIZE { for xx in 0..HybridBinarizer::BLOCK_SIZE {
// for (int xx = 0; xx < BLOCK_SIZE; xx++) { // for (int xx = 0; xx < BLOCK_SIZE; xx++) {
sum += luminances[offset as usize + xx] as u32; sum += luminances[offset as usize + xx] as u32;
} }
yy+=1;
offset += width; offset += width;
} }
break; break;
} }
yy+=1;
offset += width; offset += width;
} }
@@ -4052,21 +4057,18 @@ impl HybridBinarizer {
// the boundaries is used for the interior. // the boundaries is used for the interior.
// The (min < bp) is arbitrary but works better than other heuristics that were tried. // The (min < bp) is arbitrary but works better than other heuristics that were tried.
let average_neighbor_black_point = (blackPoints[y as usize - 1][x as usize] let average_neighbor_black_point:u32 = (blackPoints[y as usize - 1][x as usize]
+ (2 * blackPoints[y as usize][x as usize - 1]) + (2 * blackPoints[y as usize][x as usize - 1])
+ blackPoints[y as usize - 1][x as usize - 1]) + blackPoints[y as usize - 1][x as usize - 1])
/ 4; / 4;
if (min < average_neighbor_black_point) { if (min as u32) < average_neighbor_black_point {
average = average_neighbor_black_point as u32; average = average_neighbor_black_point;
} }
} }
} }
blackPoints[y as usize][x as usize] = average as u8; blackPoints[y as usize][x as usize] = average;
} }
} }
return blackPoints blackPoints
.into_iter()
.map(|x| x.iter().map(|y| *y as u32).collect())
.collect();
} }
} }

View File

@@ -16,8 +16,8 @@
use std::{ use std::{
collections::HashMap, collections::HashMap,
fs::{read_dir, read_to_string}, fs::{read_dir, read_to_string, File},
path::{Path, PathBuf}, path::{Path, PathBuf}, io::Write,
}; };
use rxing::{ use rxing::{
@@ -231,6 +231,15 @@ impl AbstractBlackBoxTestCase {
let rotated_image = Self::rotate_image(&image, rotation); let rotated_image = Self::rotate_image(&image, rotation);
let source = BufferedImageLuminanceSource::new(rotated_image); let source = BufferedImageLuminanceSource::new(rotated_image);
let bitmap = BinaryBitmap::new(Box::new(HybridBinarizer::new(Box::new(source)))); let bitmap = BinaryBitmap::new(Box::new(HybridBinarizer::new(Box::new(source))));
// if file_base_name == "09" {
// let mut f = File::create("test_file_output.txt").unwrap();
// dbg!("dumb");
// write!(f,"{}", bitmap.getBlackMatrix().unwrap());
// drop(f);
// Self::rotate_image(&image, rotation).save("test_image.png").unwrap();
// }
if let Ok(decoded) = if let Ok(decoded) =
self.decode(&bitmap, rotation, &expected_text, &expected_metadata, false) self.decode(&bitmap, rotation, &expected_text, &expected_metadata, false)
{ {
@@ -499,7 +508,7 @@ impl AbstractBlackBoxTestCase {
&original.to_luma8(), &original.to_luma8(),
radians, radians,
Interpolation::Nearest, Interpolation::Nearest,
Luma([255; 1]), Luma([u8::MAX/2; 1]),
); );
DynamicImage::from(i) DynamicImage::from(i)