Binarizer
in package
This class implements a local thresholding algorithm, which while slower than the GlobalHistogramBinarizer, is fairly efficient for what it does. It is designed for high frequency images of barcodes with black data on white backgrounds. For this application, it does a much better job than a global blackpoint with severe shadows and gradients.
However, it tends to produce artifacts on lower frequency images and is therefore not a good general purpose binarizer for uses outside ZXing.
This class extends GlobalHistogramBinarizer, using the older histogram approach for 1D readers, and the newer local approach for 2D readers. 1D decoding using a per-row histogram is already inherently local, and only fails for horizontal gradients. We can revisit that problem later, but for now it was not a win to use local blocks for 1D.
This Binarizer is the default for the unit tests and the recommended class for library users.
Tags
Table of Contents
Constants
- BLOCK_SIZE = 8
- BLOCK_SIZE_MASK = 7
- BLOCK_SIZE_POWER = 3
- LUMINANCE_BUCKETS = 32
- LUMINANCE_SHIFT = 3
- MIN_DYNAMIC_RANGE = 24
- MINIMUM_DIMENSION = 40
Properties
- $luminances : array<string|int, int>
- $source : LuminanceSourceInterface
Methods
- __construct() : mixed
- getBlackMatrix() : BitMatrix
- Calculates the final BitMatrix once for all requests. This could be called once from the constructor instead, but there are some advantages to doing it lazily, such as making profiling easier, and not doing heavy lifting when callers don't expect it.
- calculateBlackPoints() : array<string|int, array<string|int, float>>
- Calculates a single black point for each block of pixels and saves it away.
- calculateThresholdForBlock() : BitMatrix
- For each block in the image, calculate the average black point using a 5x5 grid of the surrounding blocks. Also handles the corner cases (fractional blocks are computed based on the last pixels in the row/column which are also used in the previous block).
- cap() : int
- estimateBlackPoint() : int
- getHistogramBlackMatrix() : BitMatrix
Constants
BLOCK_SIZE
private
mixed
BLOCK_SIZE
= 8
BLOCK_SIZE_MASK
private
mixed
BLOCK_SIZE_MASK
= 7
BLOCK_SIZE_POWER
private
mixed
BLOCK_SIZE_POWER
= 3
LUMINANCE_BUCKETS
private
mixed
LUMINANCE_BUCKETS
= 32
LUMINANCE_SHIFT
private
mixed
LUMINANCE_SHIFT
= 3
MIN_DYNAMIC_RANGE
private
mixed
MIN_DYNAMIC_RANGE
= 24
MINIMUM_DIMENSION
private
mixed
MINIMUM_DIMENSION
= 40
Properties
$luminances
private
array<string|int, int>
$luminances
$source
private
LuminanceSourceInterface
$source
Methods
__construct()
public
__construct(LuminanceSourceInterface $source) : mixed
Parameters
- $source : LuminanceSourceInterface
getBlackMatrix()
Calculates the final BitMatrix once for all requests. This could be called once from the constructor instead, but there are some advantages to doing it lazily, such as making profiling easier, and not doing heavy lifting when callers don't expect it.
public
getBlackMatrix() : BitMatrix
Converts a 2D array of luminance data to 1 bit data. As above, assume this method is expensive and do not call it repeatedly. This method is intended for decoding 2D barcodes and may or may not apply sharpening. Therefore, a row from this matrix may not be identical to one fetched using getBlackRow(), so don't mix and match between them.
Return values
BitMatrix —The 2D array of bits for the image (true means black).
calculateBlackPoints()
Calculates a single black point for each block of pixels and saves it away.
private
calculateBlackPoints(int $subWidth, int $subHeight, int $width, int $height) : array<string|int, array<string|int, float>>
See the following thread for a discussion of this algorithm:
Parameters
- $subWidth : int
- $subHeight : int
- $width : int
- $height : int
Tags
Return values
array<string|int, array<string|int, float>>calculateThresholdForBlock()
For each block in the image, calculate the average black point using a 5x5 grid of the surrounding blocks. Also handles the corner cases (fractional blocks are computed based on the last pixels in the row/column which are also used in the previous block).
private
calculateThresholdForBlock(int $subWidth, int $subHeight, int $width, int $height) : BitMatrix
Parameters
- $subWidth : int
- $subHeight : int
- $width : int
- $height : int
Return values
BitMatrixcap()
private
cap(int $value, int $min, int $max) : int
Parameters
- $value : int
- $min : int
- $max : int
Tags
Return values
intestimateBlackPoint()
private
estimateBlackPoint(array<string|int, int> $buckets) : int
Parameters
- $buckets : array<string|int, int>
Tags
Return values
intgetHistogramBlackMatrix()
private
getHistogramBlackMatrix(int $width, int $height) : BitMatrix
Parameters
- $width : int
- $height : int