![]() |
Leptonica 1.85.0
Image processing and image analysis suite
|
#include <string.h>#include "allheaders.h"#include "readbarcode.h"Go to the source code of this file.
Macros | |
| #define | DEBUG_DESKEW 0 |
| #define | DEBUG_WIDTHS 0 |
Functions | |
| static PIX * | pixGenerateBarcodeMask (PIX *pixs, l_int32 maxspace, l_int32 nwidth, l_int32 nheight) |
| static NUMA * | pixAverageRasterScans (PIX *pixs, l_int32 nscans) |
| static l_int32 | numaGetCrossingDistances (NUMA *nas, NUMA **pnaedist, NUMA **pnaodist, l_float32 *pmindist, l_float32 *pmaxdist) |
| static NUMA * | numaLocatePeakRanges (NUMA *nas, l_float32 minfirst, l_float32 minsep, l_float32 maxmin) |
| static NUMA * | numaGetPeakCentroids (NUMA *nahist, NUMA *narange) |
| static NUMA * | numaGetPeakWidthLUT (NUMA *narange, NUMA *nacent) |
| static l_int32 | numaEvalBestWidthAndShift (NUMA *nas, l_int32 nwidth, l_int32 nshift, l_float32 minwidth, l_float32 maxwidth, l_float32 *pbestwidth, l_float32 *pbestshift, l_float32 *pbestscore) |
| static l_int32 | numaEvalSyncError (NUMA *nas, l_int32 ifirst, l_int32 ilast, l_float32 width, l_float32 shift, l_float32 *pscore, NUMA **pnad) |
| SARRAY * | pixProcessBarcodes (PIX *pixs, l_int32 format, l_int32 method, SARRAY **psaw, l_int32 debugflag) |
| PIXA * | pixExtractBarcodes (PIX *pixs, l_int32 debugflag) |
| SARRAY * | pixReadBarcodes (PIXA *pixa, l_int32 format, l_int32 method, SARRAY **psaw, l_int32 debugflag) |
| NUMA * | pixReadBarcodeWidths (PIX *pixs, l_int32 method, l_int32 debugflag) |
| BOXA * | pixLocateBarcodes (PIX *pixs, l_int32 thresh, PIX **ppixb, PIX **ppixm) |
| PIX * | pixDeskewBarcode (PIX *pixs, PIX *pixb, BOX *box, l_int32 margin, l_int32 threshold, l_float32 *pangle, l_float32 *pconf) |
| NUMA * | pixExtractBarcodeWidths1 (PIX *pixs, l_float32 thresh, l_float32 binfract, NUMA **pnaehist, NUMA **pnaohist, l_int32 debugflag) |
| NUMA * | pixExtractBarcodeWidths2 (PIX *pixs, l_float32 thresh, l_float32 *pwidth, NUMA **pnac, l_int32 debugflag) |
| NUMA * | pixExtractBarcodeCrossings (PIX *pixs, l_float32 thresh, l_int32 debugflag) |
| NUMA * | numaQuantizeCrossingsByWidth (NUMA *nas, l_float32 binfract, NUMA **pnaehist, NUMA **pnaohist, l_int32 debugflag) |
| NUMA * | numaQuantizeCrossingsByWindow (NUMA *nas, l_float32 ratio, l_float32 *pwidth, l_float32 *pfirstloc, NUMA **pnac, l_int32 debugflag) |
Variables | |
| static const l_int32 | MAX_SPACE_WIDTH = 19 |
| static const l_int32 | MAX_NOISE_WIDTH = 50 |
| static const l_int32 | MAX_NOISE_HEIGHT = 30 |
| static const l_int32 | MIN_BC_WIDTH = 50 |
| static const l_int32 | MIN_BC_HEIGHT = 50 |
Basic operations to locate and identify the line widths
in 1D barcodes.
Top level
SARRAY *pixProcessBarcodes()
Next levels
PIXA *pixExtractBarcodes()
SARRAY *pixReadBarcodes()
l_int32 pixReadBarcodeWidths()
Location
BOXA *pixLocateBarcodes()
static PIX *pixGenerateBarcodeMask()
Extraction and deskew
PIXA *pixDeskewBarcodes()
Process to get line widths
NUMA *pixExtractBarcodeWidths1()
NUMA *pixExtractBarcodeWidths2()
NUMA *pixExtractBarcodeCrossings()
Average adjacent rasters
static NUMA *pixAverageRasterScans()
Signal processing for barcode widths
NUMA *numaQuantizeCrossingsByWidth()
static l_int32 numaGetCrossingDistances()
static NUMA *numaLocatePeakRanges()
static NUMA *numaGetPeakCentroids()
static NUMA *numaGetPeakWidthLUT()
NUMA *numaQuantizeCrossingsByWindow()
static l_int32 numaEvalBestWidthAndShift()
static l_int32 numaEvalSyncError()
NOTE CAREFULLY: This is "early beta" code. It has not been tuned
to work robustly on a large database of barcode images. I'm putting
it out so that people can play with it, find out how it breaks, and
contribute decoders for other barcode formats. Both the functional
interfaces and ABI will almost certainly change in the coming
few months. The actual decoder, in bardecode.c, at present only
works on the following codes: Code I2of5, Code 2of5, Code 39, Code 93
Codabar and UPCA. To add another barcode format, it is necessary
to make changes in readbarcode.h and bardecode.c.
The program prog/barcodetest shows how to run from the top level
(image --> decoded data).
Definition in file readbarcode.c.
| #define DEBUG_DESKEW 0 |
Definition at line 123 of file readbarcode.c.
| #define DEBUG_WIDTHS 0 |
Definition at line 124 of file readbarcode.c.
|
static |
| [in] | nas | numa of crossing locations |
| [in] | nwidth | number of widths to consider |
| [in] | nshift | number of shifts to consider for each width |
| [in] | minwidth | smallest width to consider |
| [in] | maxwidth | largest width to consider |
| [out] | pbestwidth | best size of window |
| [out] | pbestshift | best shift for the window |
| [out] | pbestscore | [optional] average squared error of dist of crossing signal from the center of the window |
Notes:
(1) This does a linear sweep of widths, evaluating at nshift
shifts for each width, finding the (width, shift) pair that
gives the minimum score.
Definition at line 1379 of file readbarcode.c.
References numaEvalSyncError().
Referenced by numaQuantizeCrossingsByWindow().
|
static |
| [in] | nas | numa of crossing locations |
| [in] | ifirst | first crossing to use |
| [in] | ilast | last crossing to use; use 0 for all crossings |
| [in] | width | size of window |
| [in] | shift | of center of window w/rt first crossing |
| [out] | pscore | [optional] average squared error of dist of crossing signal from the center of the window |
| [out] | pnad | [optional] numa of 1s and 0s for crossings |
Notes:
(1) The score is computed only on the part of the signal from the
ifirst to ilast crossings. Use 0 for both of these to
use all the crossings. The score is normalized for
the number of crossings and with half-width of the window.
(2) The optional return nad is a sequence of 0s and 1s, where a '1'
indicates a crossing in the window.
Definition at line 1451 of file readbarcode.c.
Referenced by numaEvalBestWidthAndShift(), and numaQuantizeCrossingsByWindow().
|
static |
| [in] | nas | numa of crossing locations |
| [out] | pnaedist | [optional] even distances between crossings |
| [out] | pnaodist | [optional] odd distances between crossings |
| [out] | pmindist | [optional] min distance between crossings |
| [out] | pmaxdist | [optional] max distance between crossings |
Definition at line 1001 of file readbarcode.c.
Referenced by numaQuantizeCrossingsByWidth(), and numaQuantizeCrossingsByWindow().
| [in] | nahist | numa of histogram of crossing widths |
| [in] | narange | numa of ranges of x-values for the peaks in nahist |
Definition at line 1140 of file readbarcode.c.
Referenced by numaQuantizeCrossingsByWidth().
| [in] | narange | numa of x-val ranges for the histogram width peaks |
| [in] | nacent | numa of centroids of each peak – up to 4 |
Notes:
(1) This generates the lookup table that maps from a sequence of widths
(in some units) to the integerized barcode units (1, 2, 3, 4),
which are the output integer widths between transitions.
(2) The smallest width can be lost in float roundoff. To avoid
losing it, we expand the peak range of the smallest width.
Definition at line 1189 of file readbarcode.c.
References L_NOCOPY.
Referenced by numaQuantizeCrossingsByWidth().
|
static |
| [in] | nas | numa of histogram of crossing widths |
| [in] | minfirst | min location of center of first peak |
| [in] | minsep | min separation between peak range centers |
| [in] | maxmin | max allowed value for min histo value between peaks |
Notes:
(1) Units of minsep are the index into nas.
This puts useful constraints on peak-finding.
(2) If maxmin == 0.0, the value of nas[i] must go to 0.0 (or less)
between peaks.
(3) All calculations are done in units of the index into nas.
The resulting ranges are therefore integers.
(4) The output nad gives pairs of range values for successive peaks.
Any location [i] for which maxmin = nas[i] = 0.0 will NOT be
included in a peak range. This works fine for histograms where
if nas[i] == 0.0, it means that there are no samples at [i].
(5) For barcodes, when this is used on a histogram of barcode
widths, use maxmin = 0.0. This requires that there is at
least one histogram bin corresponding to a width value between
adjacent peak ranges that is unpopulated, making the separation
of the histogram peaks unambiguous.
Definition at line 1089 of file readbarcode.c.
Referenced by numaQuantizeCrossingsByWidth().
| NUMA * numaQuantizeCrossingsByWidth | ( | NUMA * | nas, |
| l_float32 | binfract, | ||
| NUMA ** | pnaehist, | ||
| NUMA ** | pnaohist, | ||
| l_int32 | debugflag ) |
numaQuantizeCrossingsByWidth()
| [in] | nas | numa of crossing locations, in pixel units |
| [in] | binfract | histo binsize as a fraction of minsize; e.g., 0.25 |
| [out] | pnaehist | [optional] histo of even (black) bar widths |
| [out] | pnaohist | [optional] histo of odd (white) bar widths |
| [in] | debugflag | 1 to generate plots of histograms of bar widths |
Notes:
(1) This first computes the histogram of black and white bar widths,
binned in appropriate units. There should be well-defined
peaks, each corresponding to a specific width. The sequence
of barcode widths (namely, the integers from the set {1,2,3,4})
is returned.
(2) The optional returned histograms are binned in width units
that are inversely proportional to binfract. For example,
if binfract = 0.25, there are 4.0 bins in the distance of
the width of the narrowest bar.
Definition at line 848 of file readbarcode.c.
References numaGetCrossingDistances(), numaGetPeakCentroids(), numaGetPeakWidthLUT(), and numaLocatePeakRanges().
Referenced by pixExtractBarcodeWidths1().
| NUMA * numaQuantizeCrossingsByWindow | ( | NUMA * | nas, |
| l_float32 | ratio, | ||
| l_float32 * | pwidth, | ||
| l_float32 * | pfirstloc, | ||
| NUMA ** | pnac, | ||
| l_int32 | debugflag ) |
numaQuantizeCrossingsByWindow()
| [in] | nas | numa of crossing locations |
| [in] | ratio | of max window size over min window size in search; typ. 2.0 |
| [out] | pwidth | [optional] best window width |
| [out] | pfirstloc | [optional] center of window for first xing |
| [out] | pnac | [optional] array of window crossings (0, 1, 2) |
| [in] | debugflag | 1 to generate various plots of intermediate results |
Notes:
(1) The minimum size of the window is set by the minimum
distance between zero crossings.
(2) The optional return signal nac is a sequence of 0s, 1s,
and perhaps a few 2s, giving the number of crossings in each window.
On the occasion where there is a '2', it is interpreted as
ending two runs: the previous one and another one that has length 1.
Definition at line 1279 of file readbarcode.c.
References numaEvalBestWidthAndShift(), numaEvalSyncError(), and numaGetCrossingDistances().
Referenced by pixExtractBarcodeWidths2().
| [in] | pixs | input image; 8 bpp |
| [in] | nscans | number of adjacent scans, about the center vertically |
Definition at line 782 of file readbarcode.c.
References GET_DATA_BYTE, and L_NOCOPY.
Referenced by pixExtractBarcodeCrossings().
| PIX * pixDeskewBarcode | ( | PIX * | pixs, |
| PIX * | pixb, | ||
| BOX * | box, | ||
| l_int32 | margin, | ||
| l_int32 | threshold, | ||
| l_float32 * | pangle, | ||
| l_float32 * | pconf ) |
| [in] | pixs | input image; 8 bpp |
| [in] | pixb | binarized edge-filtered input image |
| [in] | box | identified region containing barcode |
| [in] | margin | of extra pixels around box to extract |
| [in] | threshold | for binarization; ~20 |
| [out] | pangle | [optional] in degrees, clockwise is positive |
| [out] | pconf | [optional] confidence |
Notes:
(1) The (optional) angle returned is the angle in degrees (cw positive)
necessary to rotate the image so that it is deskewed.
Definition at line 500 of file readbarcode.c.
References L_BRING_IN_WHITE, L_CLONE, L_ROTATE_AREA_MAP, L_SORT_BY_AREA, L_SORT_DECREASING, and pixLocateBarcodes().
Referenced by pixExtractBarcodes().
| [in] | pixs | input image; 8 bpp |
| [in] | thresh | estimated pixel threshold for crossing white <--> black; typ. ~120 |
| [in] | debugflag | use 1 to generate debug output |
Notes:
(1) Require at least 10 crossings.
Definition at line 722 of file readbarcode.c.
References L_QUADRATIC_INTERP, and pixAverageRasterScans().
Referenced by pixExtractBarcodeWidths1(), and pixExtractBarcodeWidths2().
| [in] | pixs | 8 bpp, no colormap |
| [in] | debugflag | use 1 to generate debug output |
Definition at line 186 of file readbarcode.c.
References L_CLONE, L_INSERT, pixDeskewBarcode(), and pixLocateBarcodes().
Referenced by pixProcessBarcodes().
| NUMA * pixExtractBarcodeWidths1 | ( | PIX * | pixs, |
| l_float32 | thresh, | ||
| l_float32 | binfract, | ||
| NUMA ** | pnaehist, | ||
| NUMA ** | pnaohist, | ||
| l_int32 | debugflag ) |
| [in] | pixs | input image; 8 bpp |
| [in] | thresh | estimated pixel threshold for crossing white <--> black; typ. ~120 |
| [in] | binfract | histo binsize as a fraction of minsize; e.g., 0.25 |
| [out] | pnaehist | [optional] histogram of black widths; NULL ok |
| [out] | pnaohist | [optional] histogram of white widths; NULL ok |
| [in] | debugflag | use 1 to generate debug output |
Notes:
(1) The widths are alternating black/white, starting with black
and ending with black.
(2) This method uses the widths of the bars directly, in terms
of the (float) number of pixels between transitions.
The histograms of these widths for black and white bars is
generated and interpreted.
Definition at line 628 of file readbarcode.c.
References numaQuantizeCrossingsByWidth(), and pixExtractBarcodeCrossings().
Referenced by pixReadBarcodeWidths().
| NUMA * pixExtractBarcodeWidths2 | ( | PIX * | pixs, |
| l_float32 | thresh, | ||
| l_float32 * | pwidth, | ||
| NUMA ** | pnac, | ||
| l_int32 | debugflag ) |
| [in] | pixs | input image; 8 bpp |
| [in] | thresh | estimated pixel threshold for crossing white <--> black; typ. ~120 |
| [out] | pwidth | [optional] best decoding window width, in pixels |
| [out] | pnac | [optional] number of transitions in each window |
| [in] | debugflag | use 1 to generate debug output |
Notes:
(1) The widths are alternating black/white, starting with black
and ending with black.
(2) The optional best decoding window width is the width of the window
that is used to make a decision about whether a transition occurs.
It is approximately the average width in pixels of the narrowest
white and black bars (i.e., those corresponding to unit width).
(3) The optional return signal nac is a sequence of 0s, 1s,
and perhaps a few 2s, giving the number of crossings in each window.
On the occasion where there is a '2', it is interpreted as
as ending two runs: the previous one and another one that has length 1.
Definition at line 682 of file readbarcode.c.
References numaQuantizeCrossingsByWindow(), and pixExtractBarcodeCrossings().
Referenced by pixReadBarcodeWidths().
|
static |
| [in] | pixs | 1 bpp |
| [in] | maxspace | largest space in the barcode, in pixels |
| [in] | nwidth | opening 'width' to remove noise |
| [in] | nheight | opening 'height' to remove noise |
Notes:
(1) For noise removal, 'width' and 'height' are referred to the
barcode orientation.
(2) If there is skew, the mask will not cover the barcode corners.
Definition at line 446 of file readbarcode.c.
Referenced by pixLocateBarcodes().
| [in] | pixs | any depth |
| [in] | thresh | for binarization of edge filter output; typ. 20 |
| [out] | ppixb | [optional] binarized edge filtered input image |
| [out] | ppixm | [optional] mask over barcodes |
Definition at line 388 of file readbarcode.c.
References L_ALL_EDGES, and pixGenerateBarcodeMask().
Referenced by pixDeskewBarcode(), and pixExtractBarcodes().
| SARRAY * pixProcessBarcodes | ( | PIX * | pixs, |
| l_int32 | format, | ||
| l_int32 | method, | ||
| SARRAY ** | psaw, | ||
| l_int32 | debugflag ) |
| [in] | pixs | any depth |
| [in] | format | L_BF_ANY, L_BF_CODEI2OF5, L_BF_CODE93, ... |
| [in] | method | L_USE_WIDTHS, L_USE_WINDOWS |
| [out] | psaw | [optional] sarray of bar widths |
| [in] | debugflag | use 1 to generate debug output |
Definition at line 142 of file readbarcode.c.
References pixExtractBarcodes(), and pixReadBarcodes().
| SARRAY * pixReadBarcodes | ( | PIXA * | pixa, |
| l_int32 | format, | ||
| l_int32 | method, | ||
| SARRAY ** | psaw, | ||
| l_int32 | debugflag ) |
| [in] | pixa | of 8 bpp deskewed and cropped barcodes |
| [in] | format | L_BF_ANY, L_BF_CODEI2OF5, L_BF_CODE93, ... |
| [in] | method | L_USE_WIDTHS, L_USE_WINDOWS; |
| [out] | psaw | [optional] sarray of bar widths |
| [in] | debugflag | use 1 to generate debug output |
Definition at line 257 of file readbarcode.c.
References L_CLONE, L_COPY, L_INSERT, and pixReadBarcodeWidths().
Referenced by pixProcessBarcodes().
| [in] | pixs | of 8 bpp deskewed and cropped barcode |
| [in] | method | L_USE_WIDTHS, L_USE_WINDOWS; |
| [in] | debugflag | use 1 to generate debug output |
Definition at line 341 of file readbarcode.c.
References pixExtractBarcodeWidths1(), and pixExtractBarcodeWidths2().
Referenced by pixReadBarcodes().
|
static |
Definition at line 94 of file readbarcode.c.
|
static |
Definition at line 93 of file readbarcode.c.
|
static |
Definition at line 92 of file readbarcode.c.
|
static |
Definition at line 98 of file readbarcode.c.
|
static |
Definition at line 97 of file readbarcode.c.