METHOD AND APPARATUS FOR PROCESSING GRAPHIC SYMBOL AND COMPUTER-READABLE STORAGE MEDIUM
Embodiments of this application disclose a method and apparatus for processing graphic symbol and a computer-readable storage medium. The method may include: obtaining a luminance component image of a to-be-processed image, performing binarization processing based on the luminance component image of the to-be-processed image to obtain a binarized image, and performing a grayscale morphological operation based on the binarized image to obtain a target image.
1 . A method for processing graphic symbol, comprising:
obtaining a luminance component image of a to-be-processed image;
performing binarization processing based on the luminance component image of the to-be-processed image to obtain a binarized image; and
performing a grayscale morphological operation based on the binarized image to obtain a target image;
wherein the to-be-processed image has a graphic symbol.
2 . The method according to claim 1 , wherein the obtaining a luminance component image of a to-be-processed image comprises:
converting a color space of the to-be-processed image to a target color space according to a mapping relationship between the color space of the to-be-processed image and the target color space; and
extracting a luminance component image of the target color space to obtain the luminance component image of the to-be-processed image.
3 . The method according to claim 2 , wherein the color space of the to-be-processed image is RGB color space or BGR color space, and the target color space is YCbCr color space, YCrCb color space, or YUV color space.
4 . The method according to claim 2 , wherein the mapping relationship between the color space of the to-be-processed image and the target color space is shown in the following formula:
Y=k r R+k g G+k b B;
wherein Y is the luminance value of a pixel in the luminance component image of the target color space, and R, G, and B are the red chromaticity value, green chromaticity value, and blue chromaticity value of the pixel in the to-be-processed image, respectively; and k r , k g , and k b are weighting factors and satisfy the following relationship:
k r +k g +k b =1.
5 . The method according to claim 1 , wherein before the performing binarization processing based on the luminance component image of the to-be-processed image, the method further comprises:
performing contrast stretching on the luminance component image of the to-be-processed image.
6 . The method according to claim 5 , wherein the performing contrast stretching on the luminance component image comprises:
traversing pixels in the luminance component image of the to-be-processed image, and determining grayscale values of all of the pixels in the luminance component image of the to-be-processed image;
determining a contrast stretching function based on a grayscale range in which the grayscale values of all of the pixels in the luminance component image of the to-be-processed image fall; and
performing grayscale transformation on the pixels in the luminance component image of the to-be-processed image based on the contrast stretching function.
7 . The method according to claim 1 , wherein the performing binarization processing based on the luminance component image comprises:
performing binarization processing on the luminance component image of the to-be-processed image using a local adaptive binarization algorithm.
8 . The method according to claim 7 , wherein the performing binarization processing on the luminance component image using a local adaptive binarization algorithm comprises:
determining a size of a binarization processing window;
traversing all pixels in the luminance component image of the to-be-processed image using the binarization processing window;
determining a grayscale threshold to be covered by the binarization processing window; and
under the condition that a sum of the pixel values of all of the pixels in the luminance component image of the to-be-processed image is greater than or equal to the grayscale threshold, setting a pixel value of a pixel corresponding to the center of the binarization processing window to 1; otherwise setting the pixel value of the pixel corresponding to the center of the binarization processing window to 0.
9 . The method according to claim 8 , wherein the grayscale threshold is determined according to the following formula:
T
=
1
n
2
∑
i
=
0
i
=
n
-
1
∑
j
=
0
j
=
n
-
1
v
ij
-
C
wherein T is the grayscale threshold, n is a length of a side of the binarization processing window, v ij is the grayscale value of the pixel in the i-th row and j-th column in the binarization processing window, and C is a constant term.
10 . The method according to claim 1 , wherein before the performing a grayscale morphological operation based on the binarized image, the method further comprises:
performing edge-preserving filtering on the binarized image.
11 . The method according to claim 1 , wherein the performing a grayscale morphological operation based on the binarized image comprises:
performing morphological closing and opening operations on the binarized image in sequence.
12 . The method according to claim 11 , wherein the closing operation comprises:
selecting a first structural element based on the binarized image; and
performing expansion processing and erosion processing on a closing operation region in the binarized image sequentially according to the first structural element and a predetermined closing operation rule; and
the opening operation comprises:
selecting a second structural element based on the closing-operated image; and
performing erosion processing and expansion processing on an opening operation region in the binarized image sequentially according to the second structural element and a predetermined opening operation rule.
13 . The method according to claim 1 , wherein the graphic symbol in the to-be-processed image is a quick code or a bar code.
14 . An apparatus for processing graphic symbol, comprising:
an obtaining module, configured to obtain a luminance component image of a to-be-processed image;
a binarization processing module, configured to perform binarization processing based on the luminance component image of the to-be-processed image to output a binarized image; and
an operation module, configured to perform a grayscale morphological operation based on the binarized image to output a target image;
wherein the to-be-processed image has a graphic symbol.
15 . An apparatus for processing graphic symbol, comprising a processor and a memory, wherein the memory is configured to store a program, and the processor is configured to call and run the program from the memory so as to perform the method for processing graphic symbol according to claim 1 .
16 . A computer-readable storage medium, wherein a computer program is stored in the medium; and when the computer program runs on a computer, the computer is caused to perform the method for processing graphic symbol according to claim 1 .