Disclosure of Invention
In order to solve the above problems, the present invention provides an image processing method and apparatus with non-uniform illumination, which overcomes the non-uniform illumination phenomenon by adaptively adjusting the binarization threshold of the grayscale image.
In order to achieve the above object, the present invention provides the following technical solutions:
an image processing method of uneven illumination, comprising:
converting the obtained original image into a gray image;
dividing the gray level image into a plurality of gray level sub-images, and calculating a primary threshold value of each gray level sub-image;
generating an adaptive threshold of the gray level image according to the preliminary threshold;
and converting the gray level image into a binary image according to the self-adaptive threshold value.
Further, the converting the acquired original image into a grayscale image includes:
setting the total number of pixels of the original image as N, where N = Nx × Ny, nx is the total number of abscissa pixels of the grayscale image, ny is the total number of ordinate pixels of the grayscale image, and calculating a grayscale value with coordinates of (x, y) pixel points by using the following formula:
I(x,y)=0.3*R(x,y)+0.6*G(x,y)+0.1*B(x,y)
wherein, I (x, y) represents the gray value of the pixel point with the coordinate (x, y), R (x, y) represents the brightness value of the pixel point with the coordinate (x, y) in the red channel, G (x, y) represents the brightness value of the pixel point with the coordinate (x, y) in the green channel, and B (x, y) represents the brightness value of the pixel point with the coordinate (x, y) in the blue channel.
Further, the calculating the preliminary threshold of each gray-scale sub-image specifically includes:
dividing the gray-scale image into n blocks, wherein n = p × q, calculating a gray-scale average value of each block of the gray-scale sub-image by using the following formula, and using the gray-scale average value as a preliminary threshold value of the gray-scale sub-image;
wherein k =1,2.,. N, P, q are positive integers, u ∈ [ x-P/2,x + P/2], v ∈ [ y-q/2,y + q/2], and P (k) represents the gray scale average value of the kth block of gray scale sub-images.
Further, the generating an adaptive threshold for the grayscale image according to the preliminary threshold includes:
calculating an adaptive threshold for each of the grayscale sub-images by:
wherein Q (k) represents the adaptive threshold of the kth gray level sub-image, i belongs to [0, k ], s is an integer, and s is more than or equal to 1 and less than k;
an adaptive threshold Q of the grayscale image is denoted as Q = { Q (1), Q (2),.., Q (k),. Q (n) }.
Further, the converting the grayscale image into a binarized image according to the adaptive threshold includes:
calculating a binarization value of the grayscale image by the following function:
wherein, T (x, y) represents the coordinate as the value of pixel point after binaryzation;
and taking an image matrix formed by binarized values of all pixel points in the gray level image as a binary image.
An image processing apparatus of illumination non-uniformity, the apparatus comprising: a memory, a processor, and a computer program stored in the memory and executable on the processor, the processor executing the computer program to operate in modules of:
the first image conversion module is used for converting the acquired original image into a gray image;
the preliminary threshold calculation module is used for dividing the gray level image into a plurality of gray level sub-images and calculating a preliminary threshold of each gray level sub-image;
the self-adaptive threshold value generation module is used for generating a self-adaptive threshold value of the gray level image according to the preliminary threshold value;
and the second image conversion module is used for converting the gray level image into a binary image according to the self-adaptive threshold value.
The invention has the beneficial effects that: the invention discloses a method and a device for processing an image with uneven illumination, which comprises the steps of firstly converting an acquired original image into a gray image; dividing the gray level image into a plurality of gray level sub-images, and calculating a primary threshold value of each gray level sub-image; then generating an adaptive threshold value of the gray level image according to the preliminary threshold value; and finally, converting the gray level image into a binary image according to the self-adaptive threshold. The invention overcomes the phenomenon of uneven illumination by self-adaptively adjusting the binarization threshold of the gray level image.
Detailed Description
The conception, specific structure and technical effects of the present disclosure will be clearly and completely described below in conjunction with the embodiments and the accompanying drawings to fully understand the objects, aspects and effects of the present disclosure. It should be noted that the embodiments and features of the embodiments in the present application may be combined with each other without conflict.
Fig. 1 shows an image processing method for uneven illumination, which includes the following steps:
s100, converting the acquired original image into a gray image;
step S200, dividing the gray level image into a plurality of gray level sub-images, and calculating a primary threshold value of each gray level sub-image;
step S300, generating an adaptive threshold value of the gray level image according to the preliminary threshold value;
and step S400, converting the gray level image into a binary image according to the adaptive threshold.
In the prior art, a fixed binarization threshold value is adopted for image binarization processing, which can only correspond to one type of image, and if the illumination of the image is not uniform, the image binarization processing effect is extremely great due to the adoption of the fixed binarization threshold value. The self-adaptive binarization can obtain a binarization threshold value suitable for the image according to the gray level histogram of the image.
According to the embodiment, one or more areas with uneven illumination in the image often exist according to the illumination condition, so that the gray level image is firstly divided into a plurality of areas, the image with uneven illumination can be roughly divided by partitioning the image, and the preliminary threshold values of the gray level image under different illumination intensities are calculated; and then generating an adaptive threshold of the gray image according to the initial threshold, smoothing the gray value of an image area with uneven illumination, reasonably correcting the gray value of the image, and overcoming the problem caused by uneven illumination, thereby restoring the image in a real space as much as possible.
In a preferred embodiment, the converting the acquired original image into a grayscale image includes:
setting the total number of pixels of the original image as N, where N = Nx × Ny, nx is the total number of abscissa pixels of the grayscale image, ny is the total number of ordinate pixels of the grayscale image, and calculating a grayscale value with coordinates of (x, y) pixel points by using the following formula:
I(x,y)=0.3*R(x,y)+0.6*G(x,y)+0.1*B(x,y)
wherein, I (x, y) represents the gray value of the pixel point with the coordinate (x, y), R (x, y) represents the brightness value of the pixel point with the coordinate (x, y) in the red channel, G (x, y) represents the brightness value of the pixel point with the coordinate (x, y) in the green channel, and B (x, y) represents the brightness value of the pixel point with the coordinate (x, y) in the blue channel.
In the embodiment, different weights are given to the brightness values of the three color channels, so that the gray value of the gray image is more accurately expressed.
In a preferred embodiment, in step S200, calculating the preliminary threshold of each gray-scale sub-image specifically includes:
dividing the gray-scale image into n blocks, wherein n = p × q, calculating a gray-scale average value of each block of the gray-scale sub-image by using the following formula, and using the gray-scale average value as a preliminary threshold value of the gray-scale sub-image;
wherein k =1,2., n, P, q are positive integers, u belongs to [ x-P/2, x + P/2], v belongs to [ y-q/2, y + q/2], and P (k) represents the gray level average value of the kth block of gray level sub-images.
In a preferred embodiment, said generating an adaptive threshold for said grayscale image according to said preliminary threshold comprises:
calculating an adaptive threshold for each of the grayscale sub-images by:
wherein Q (k) represents an adaptive threshold of the kth grey level sub-image, i belongs to [0, k ], s is an integer, and s is more than or equal to 1 and less than k;
an adaptive threshold Q of the grayscale image is represented as Q = { Q (1), Q (2),. ·, Q (k),.., Q (n) }.
In the embodiment, the gray values of s points before the k point are weighted and summed, so that the gray value smoothing is performed on the image area with uneven illumination, the proportion of the pixel points closer to the k point is gradually increased, and the color of the pixel points is more accurate.
Further, the converting the grayscale image into a binarized image according to the adaptive threshold includes:
calculating a binarization value of the grayscale image by the following function:
wherein, T (x, y) represents the coordinate as the value after the binarization of the (x, y) pixel point;
and then, an image matrix formed by the binarized values of all pixel points in the gray level image is used as a binarized image.
In this embodiment, t is a constant preset manually, and the constant t may be set according to the actual illumination condition to adjust the adaptive threshold, so as to adapt to the actual illumination condition, and more conveniently overcome the phenomenon of uneven illumination.
Referring to fig. 2, the present invention also provides an image processing apparatus with uneven illumination, the apparatus comprising: a memory, a processor, and a computer program stored in the memory and executable on the processor, the processor executing the computer program to operate in modules of:
a first image conversion module 100, configured to convert an acquired original image into a grayscale image;
a preliminary threshold calculation module 200, configured to divide the grayscale image into a plurality of grayscale sub-images, and calculate a preliminary threshold of each grayscale sub-image;
an adaptive threshold generating module 300, configured to generate an adaptive threshold of the grayscale image according to the preliminary threshold;
a second image conversion module 400, configured to convert the grayscale image into a binary image according to the adaptive threshold.
The image processing device with uneven illumination can be operated in computing equipment such as a desktop computer, a notebook computer, a palm computer and a cloud server. The image processing device with uneven illumination can be operated by a device comprising a processor and a memory. It will be understood by those skilled in the art that the example is merely an example of an uneven illumination image processing apparatus, and does not constitute a limitation of an uneven illumination image processing apparatus, and may include more or less components than a certain proportion, or combine some components, or different components, for example, the uneven illumination image processing apparatus may further include an input-output device, a network access device, a bus, etc.
The Processor may be a Central-Processing Unit (CPU), other general-purpose Processor, a Digital Signal Processor (DSP), an Application-Specific-Integrated-Circuit (ASIC), an off-the-shelf Programmable Gate Array (FPGA) or other Programmable logic device, a discrete Gate or transistor logic device, a discrete hardware component, or the like. The general purpose processor may be a microprocessor or the processor may be any conventional processor or the like, the processor is the control center of the non-uniform illumination image processing device operating apparatus, and various interfaces and lines are used to connect various parts of the whole non-uniform illumination image processing device operating apparatus.
The memory can be used for storing the computer program and/or the module, and the processor can realize various functions of the image processing device with uneven illumination by running or executing the computer program and/or the module stored in the memory and calling the data stored in the memory. The memory may mainly include a storage program area and a storage data area, wherein the storage program area may store an operating system, an application program required by at least one function (such as a sound playing function, an image playing function, etc.), and the like; the storage data area may store data (such as audio data, a phonebook, etc.) created according to the use of the cellular phone, and the like. In addition, the memory may include high-speed random access memory, and may also include non-volatile memory, such as a hard disk, a memory, a plug-in hard disk, a Smart-Media-Card (SMC), a Secure-Digital (SD) Card, a Flash memory Card (Flash-Card), at least one magnetic disk storage device, a Flash memory device, or other volatile solid state storage device.
While the present disclosure has been described in considerable detail and with particular reference to a few illustrative embodiments thereof, it is not intended to be limited to any such details or embodiments or any particular embodiments, but it is to be construed as effectively covering the intended scope of the disclosure by providing a broad, potential interpretation of such claims in view of the prior art with reference to the appended claims. Furthermore, the foregoing description of the present disclosure has been presented in terms of embodiments foreseen by the inventors for purposes of providing a useful description, and enabling one of ordinary skill in the art to devise equivalent variations of the present disclosure that are not presently foreseen.