2021SC@SDUSC

catalogue

2, Wavelet threshold denoising

3, Threshold calculation in scanner.c

Linear interpolation algorithm

Application of interpolation in image processing

# 1, Image Scanner

Image Scanner is a functional module of ZBar to scan read images. The core of Image Scanner is mainly composed of img_scanner.c and scanner.c are two files.

Where, img_ The core function in scanner.c is zbar_scan_image(), and the core function in scanner.c is zbar_scan_y(). After simple analysis, zbar_scan_image is mainly responsible for the scanning of the read image by ZBar. The function mainly controls the reading of pixel points according to the set scanning density (read in zigzag, which is also the origin of the name of ZBar), and the ZBar in the scanner.c file_ scan_ Y () to complete filtering, threshold, determine the edge, and convert it into width flow.

The last blog analyzed the memory allocation of ZBar scanner in scanner.c and image boundary judgment (including differential operation and application). This blog continues to analyze the calculation of threshold value.

# 2, Wavelet threshold denoising

## Image noise

Noise can be understood as "factors that hinder people's sensory organs from understanding the received source information".

For example, if the plane luminance distribution of a black-and-white picture is assumed to be f(x, y), the luminance distribution R(x, y) that interferes with its reception can be called image noise.

However, noise can be defined as "unpredictable random error that can only be recognized by probability and statistics". Therefore, it is appropriate to regard image noise as a multi-dimensional random process, so the method of describing noise can borrow the description of random process, that is, its probability distribution function and probability density distribution function.

## Theoretical basis

Image and noise have different characteristics after wavelet transform, because after wavelet decomposition of noisy signal on each scale, the energy of image is mainly concentrated in low-resolution subband, while the energy of noise signal is mainly distributed in each high-frequency subband.

The absolute value of the wavelet coefficients of the original image information is larger than that of the noise information. On this premise, we can set an appropriate threshold and use the threshold method to retain the useful signal coefficients. And this denoising process is actually the process of processing high-frequency wavelet coefficients.

## Denoising process

As shown below:

## Threshold denoising

### Hard threshold denoising

When the wavelet coefficient is less than a critical threshold, it is considered that the wavelet coefficient at that time is mainly caused by noise and should be abandoned; When the wavelet coefficient is greater than this critical threshold, it is considered that the wavelet coefficient is mainly caused by the signal, and the wavelet coefficient should be retained directly.

### Soft threshold denoising

The wavelet coefficients of noisy signals are compared with the selected threshold. The shrinkage of points greater than the threshold is the difference between the point value and the threshold, the shrinkage of points less than the opposite number of the threshold is the sum of the point value and the threshold, and the point whose absolute value is less than or equal to the threshold is 0.

## Threshold selection

The determination of threshold is the most critical in threshold atrophy.

At present, the threshold used can be divided into global threshold and local adaptive threshold.

Among them, the global threshold is unified for all wavelet coefficients in each layer or wavelet coefficients in the same layer; The local adaptation threshold is determined according to the local conditions around the current coefficient. At present, the proposed global thresholds mainly include the following:

(1) Donoho and Johastone unified threshold (DJ threshold for short):

PS: σ Is the noise standard variance, and N is the size or length of the signal

(2) Confidence interval threshold based on zero mean normal distribution:

(3) Bayes Shrink threshold and Map Shrink threshold. Under the assumption that the wavelet coefficients obey the generalized Gaussian distribution, Chang et al. Obtained the threshold:

PS: R is the standard deviation of noise, and RB is the standard deviation of generalized Gaussian distribution

(4) Minimum maximization

Threshold: This is the threshold obtained by Donoho and John Stone in the sense of minimum and maximum. Different from the above threshold, it depends on the signal and has no explicit expression. It needs to know the original signal in advance when calculating.

(5) Ideal

Threshold: the ideal threshold is the optimal threshold under the criterion of mean square deviation. Like the maximum and minimum threshold, there is no explicit expression, and the calculation of this threshold usually needs to know the signal itself first.

# 3, Threshold calculation in scanner.c

static inline unsigned calc_thresh (zbar_scanner_t *scn) { /* threshold 1st to improve noise rejection */ unsigned dx, thresh = scn->y1_thresh; unsigned long t; if((thresh <= scn->y1_min_thresh) || !scn->width) { dbprintf(1, " tmin=%d", scn->y1_min_thresh); return(scn->y1_min_thresh); } /* slowly return threshold to min */ dx = (scn->x << ZBAR_FIXED) - scn->last_edge; t = thresh * dx; t /= scn->width; t /= ZBAR_SCANNER_THRESH_FADE; dbprintf(1, " thr=%d t=%ld x=%d last=%d.%d (%d)", thresh, t, scn->x, scn->last_edge >> ZBAR_FIXED, scn->last_edge & ((1 << ZBAR_FIXED) - 1), dx); if(thresh > t) { thresh -= t; if(thresh > scn->y1_min_thresh) return(thresh); } scn->y1_thresh = scn->y1_min_thresh; return(scn->y1_min_thresh); }

The wavelet transform and other processing are given in the process.c file and analyzed by other team members.

The algorithm adopted by ZBar is the (maximum) minimization threshold mentioned above, that is, the threshold obtained in the sense of minimization (maximization) when the original signal is known in advance.

Firstly, the first-order difference of the signal is carried out. On this basis, the threshold of the first-order difference is calculated, which is conducive to denoising.

Gets the threshold in the scanner structure. If the current threshold is less than the minimum threshold or the edge width is 0, the minimum threshold is returned.

Next, find the relative threshold.

Relative threshold = previous threshold * distance between current edge and previous edge/ The ratio of the last distance

If the previous threshold is greater than the relative threshold, the relative threshold is subtracted from the previous threshold. If the result is greater than the minimum threshold, this result is returned. Otherwise, the minimum threshold is returned.

# 4, Boundary treatment

static inline zbar_symbol_type_t process_edge (zbar_scanner_t *scn, int y1) { if(!scn->y1_sign) scn->last_edge = scn->cur_edge = (1 << ZBAR_FIXED) + ROUND; else if(!scn->last_edge) scn->last_edge = scn->cur_edge; scn->width = scn->cur_edge - scn->last_edge; dbprintf(1, " sgn=%d cur=%d.%d w=%d (%s)\n", scn->y1_sign, scn->cur_edge >> ZBAR_FIXED, scn->cur_edge & ((1 << ZBAR_FIXED) - 1), scn->width, ((y1 > 0) ? "SPACE" : "BAR")); scn->last_edge = scn->cur_edge; #if DEBUG_SVG > 1 svg_path_moveto(SVG_ABS, scn->last_edge - (1 << ZBAR_FIXED) - ROUND, 0); #endif /* pass to decoder */ if(scn->decoder) return(zbar_decode_width(scn->decoder, scn->width)); return(ZBAR_PARTIAL); }

process_ The edge function performs edge processing on the points that meet the boundary determination rules.

As mentioned in the previous blog, ZBar edge determination rule is considered as an edge point according to the position where the second derivative (y1_sign, i.e. the slope of the last slope) is zero, which is the maximum or minimum value when it is the first order, and last_edge is the interpolation position of the last located edge.

ZBar scans the image. For each element, if the current scanning position is not the edge point of the element, continue to scan downward (Z-shaped scanning); if an edge point is encountered, judge whether it is at the outermost edge of the current scanning element (according to whether the last_edge is at the interpolation position of the last positioned edge). Continue to scan downward until the position meeting the above two conditions is found.

After finding the boundary, subtract the outermost position from the other end position to obtain the width of the element. Then assign cur_edge to last_edge to prepare to scan the next element.

Finally, the element width needs to be transmitted to the decoder for decoding.

if(scn->cur_edge != x || scn->y1_sign > 0) { zbar_symbol_type_t edge = process_edge(scn, -scn->y1_sign); dbprintf(1, "flush0:"); scn->cur_edge = x; scn->y1_sign = -scn->y1_sign; return(edge); }

On the other hand, in the zbar_scanner_flush() function, the boundary handling function is called.

Every time the scanner is refreshed, it is necessary to initialize the elements that have processed the boundary. Here, a very clever way is to assign the opposite number of y1_sign to y1_sign, that is, take the opposite number of the second derivative.

# 5, Update boundary

## Linear interpolation algorithm

### Algorithm content

Linear interpolation means that the interpolation function is a polynomial, and the interpolation error on the interpolation node is zero. Linear interpolation can be used to approximate the original function, or to calculate the values that are not in the table in the process of looking up the table.

### Application of interpolation in image processing

When playing video, it is often encountered that the video size is inconsistent with the canvas size. In order to make the video fill the canvas proportionally, it is necessary to scale each frame of image in the video.

Suppose the size of the source image is mxn and the target image is axb. Then the side length ratios of the two images are m/a and n/b respectively. Note that usually this ratio is not an integer, and floating-point type should be used when programming and storage. The (i,j) th pixel (i row and j column) of the target image can be returned to the source image through the side length ratio. The corresponding coordinates are (i*m/a,j*n/b).

Obviously, this corresponding coordinate is generally not an integer, and non integer coordinates cannot be used on discrete data such as images. Bilinear interpolation calculates the value of the point (gray value or RGB value) by finding the four pixel points closest to the corresponding coordinate. If your corresponding coordinates are (2.5,4.5), the nearest four pixels are (2,4), (2,5), (3,4), (3,5).

If the image is a gray image, the gray value of point (i, j) can be calculated by the following formula:

f(i,j)=w1*p1+w2*p2+w3*p3+w4*p4;

Among them, pi(i=1,2,3,4) is the nearest four pixels, and wi(i=1,2,3,4) is the corresponding weight of each point.

inline zbar_symbol_type_t zbar_scanner_flush (zbar_scanner_t *scn) { unsigned x; if(!scn->y1_sign) return(ZBAR_NONE); x = (scn->x << ZBAR_FIXED) + ROUND; if(scn->cur_edge != x || scn->y1_sign > 0) { zbar_symbol_type_t edge = process_edge(scn, -scn->y1_sign); dbprintf(1, "flush0:"); scn->cur_edge = x; scn->y1_sign = -scn->y1_sign; return(edge); } scn->y1_sign = scn->width = 0; if(scn->decoder) return(zbar_decode_width(scn->decoder, 0)); return(ZBAR_PARTIAL); }

ZBar obtains new boundary by linear interpolation algorithm:

Firstly, the threshold is updated, and then the first-order difference of the image after the motion mean is multiplied by a constant to obtain the interpolation result, that is, the new boundary.

# 6, Summary

This blog analyzes the image threshold calculation, boundary judgment, boundary reset and other algorithms of ZBar scanner. Please correct any deficiencies.