Focus evaluation method and device
Technical Field
The invention relates to the field of image processing, in particular to a focus evaluation method and a focus evaluation device.
Background
The auto-focus process typically includes a focus search process and a focus evaluation process. When the lens group is automatically focused, the lens group is moved to shoot a plurality of images, and then the clearest image is calculated through a focusing evaluation algorithm. The focus evaluation process represented by the focus evaluation algorithm occupies a core position in the automatic focusing process, and is a basis for evaluating whether an image is clear or not.
The traditional focus evaluation algorithm simply utilizes the sharpness value of an image to represent the sharpness, and the higher the sharpness value is, the clearer the image is represented; the definition is also represented by using the information entropy of the image, and the larger the entropy value is, the clearer the image is. Practice proves that when focusing is carried out in a large range, images which are close to and far away from a real focusing position have different characteristics in definition evaluation, and the definition evaluation of the images can be made to be wrong only by adopting a traditional focusing evaluation algorithm in the whole automatic focusing process.
Disclosure of Invention
The embodiment of the invention provides a focusing evaluation method, and aims to solve the problem of misjudgment of image definition evaluation in the prior art.
In a first aspect, a focus evaluation method is provided, including:
determining a first focusing position according to a first focusing evaluation method, wherein the first focusing evaluation method evaluates the definition of an image through the state of the image and a first K-means clustering algorithm;
and determining a second focusing position as a final focusing position according to a second focusing evaluation method in a preset interval with the first focusing position as a center, wherein the second focusing evaluation method evaluates the definition of the image through a sharpening operator and a second K-means clustering algorithm.
In a second aspect, there is provided a focus evaluation apparatus comprising:
the first evaluation unit is used for determining a first focusing position according to a first focusing evaluation method, and the first focusing evaluation method is used for evaluating the definition of an image through the state of the image and a first K-means clustering algorithm;
and the second evaluation unit is used for determining a second focusing position as a final focusing position according to a second focusing evaluation method in a preset interval with the first focusing position as a center, and the second focusing evaluation method is used for evaluating the definition of the image through a sharpening operator and a second K-means clustering algorithm.
When the wide-range focusing is carried out, the first focusing position is determined through the state of the image and the first K-means clustering algorithm, the first focusing position can be called coarse focusing evaluation, then the second focusing position is determined through the sharpening operator and the second K-means clustering algorithm in a preset interval with the first focusing position as the center, the second focusing position is the final focusing position and can be called fine focusing evaluation, different focusing evaluation modes are adopted according to the characteristics of the images which are far away from the final focusing position and are near to the final focusing position in the definition evaluation, and the accuracy of the image definition evaluation is improved.
Drawings
The foregoing and/or additional aspects and advantages of the present invention will become apparent and readily appreciated from the following description of the embodiments, taken in conjunction with the accompanying drawings of which:
FIG. 1 is a flow chart of a focus evaluation method according to an embodiment of the present invention;
FIG. 2 is a flow chart of a first focus evaluation method provided by an embodiment of the invention;
FIG. 3 is a flow chart of a second focus assessment method provided by embodiments of the present invention;
fig. 4 is a block diagram of a focus evaluation apparatus according to a second embodiment of the present invention;
fig. 5 is a block diagram of a first evaluation unit according to an embodiment of the present invention;
fig. 6 is a block diagram of a second evaluation unit according to an embodiment of the present invention.
Detailed Description
Reference will now be made in detail to embodiments of the present invention, examples of which are illustrated in the accompanying drawings, wherein like or similar reference numerals refer to the same or similar modules or modules having the same or similar functionality throughout. The embodiments described below with reference to the accompanying drawings are illustrative only for the purpose of explaining the present invention, and are not to be construed as limiting the present invention. On the contrary, the embodiments of the invention include all changes, modifications and equivalents coming within the spirit and terms of the claims appended hereto.
Optical imaging technology is inseparable from our lives, and relates to optical imaging from cameras, video cameras, telescopes and projectors in daily life, to microscopes and laser speckle imaging systems in laboratories. In optical imaging, a focus evaluation algorithm is an important component. Due to the fact that application scenes are more and more, the demand for a focus evaluation algorithm which is suitable for large-range focusing and high in accuracy is stronger and stronger.
When the wide-range focusing is carried out, the first focusing position is determined through the state of the image and the first K-means clustering algorithm, the first focusing position can be called coarse focusing evaluation, then the second focusing position is determined through the sharpening operator and the second K-means clustering algorithm in a preset interval with the first focusing position as the center, the second focusing position is the final focusing position and can be called fine focusing evaluation, different focusing evaluation modes are adopted according to the characteristics of the images which are far away from the final focusing position and are near to the final focusing position in the definition evaluation, and the accuracy of the image definition evaluation is improved.
Example one
Fig. 1 is a flowchart of a focus evaluation method according to an embodiment of the present invention. As shown in fig. 1, the method includes:
step S101: and determining a first focusing position according to a first focusing evaluation method, wherein the first focusing evaluation method evaluates the definition of the image through the state of the image and a first K-means clustering algorithm.
In an embodiment of the present invention, a first focus position is first determined using a first focus evaluation method, which may also be referred to as a coarse focus evaluation.
As an embodiment of the present invention, a first focus evaluation method is shown in fig. 2, the method including:
step S201: and performing first K-means clustering on the gray values of all pixel points in the gray image to obtain a clustering center of each type.
Briefly, K-means clustering is an iterative solution classifier that focuses data to various cluster centers according to some distance function, dividing the data into K sets of clusters. The number K of cluster sets required and the initial value of each cluster center are set by the user. In the embodiment of the invention, the gray values of all pixel points in the gray image are subjected to first K-means clustering. Wherein K1 is 2, that is, dividing the gray values of all the pixels in the gray image into a first cluster set and a second cluster set; the initial value of the first clustering center of the first clustering set is set as the minimum gray value of all pixel points in the gray image, and the initial value of the second clustering center of the second clustering set is set as the maximum gray value of all pixel points in the gray image, which can also be called as dividing the gray values of all pixel points in the gray image into a low gray value pixel clustering set and a high gray value pixel clustering set.
As an embodiment of the present invention, the first K-means clustering process specifically includes: calculating a modulus of a difference value between the gray value and the first clustering center as a first parameter and a modulus of a difference value between the gray value and the second clustering center as a second parameter for each pixel point; if the first parameter is smaller than the second parameter, the pixel point belongs to a first clustering set, and if the first parameter is larger than the second parameter, the pixel point belongs to a second clustering set; and averaging the first cluster set and the second cluster set to update the first cluster center and the second cluster center respectively.
In the embodiment of the present invention, the first cluster center is Smin, the second cluster center is Smax, and the gray value of each pixel point is S, then the first parameter = abs (S-Smin) and the second parameter = abs (S-Smax), where the first parameter represents the distance from the pixel point to the first cluster center, and the second parameter represents the distance from the pixel point to the second cluster center. If the first parameter is smaller than the second parameter, namely the pixel point is closer to the first clustering center, dividing the pixel point into a first clustering set; and if the first parameter is greater than the second parameter, namely the pixel point is closer to the center of the second cluster, dividing the pixel point into the second cluster set.
After the first cluster set and the second cluster set are divided, the data in the first cluster set and the data in the second cluster set are respectively averaged to be used as a new cluster center. If the difference value of the second clustering centers of two adjacent times is smaller than a set value, the data division tends to be stable, and the clustering process is stopped; otherwise, repeating the steps until the condition of stopping clustering is met.
In the embodiment of the invention, the main operation process is the operation of K-means clustering, and the method has small operation amount and stable performance.
Step S202: and calculating the gray average value of the gray image.
Step S203: and dividing the state of the gray level image according to the average value of the gray levels.
In the embodiment of the invention, the gray level average value G is obtained for the gray level image, and the gray level image is divided into a first state and a second state according to the gray level average value G. The gray average value G is larger than a preset threshold value T, and the first state shows that the gray image is overall brighter; and the gray average value G is smaller than the preset threshold value T, so that the second state is realized, and the gray image is darker as a whole.
Step S204: and evaluating the definition of the gray level image according to the clustering center of each type and the state of the gray level image.
In the embodiment of the invention, if the gray-scale image is bright as a whole and belongs to the first state, the focus evaluation value D = 255-Smin; when the entire gray image is dark, the state is the second state, and the focus evaluation value D = Smax. The larger the focus evaluation value D is, the sharper the representative grayscale image is. When the gray level image is darker as a whole, the larger the center of the second cluster is, namely, the brighter part in the gray level image is, the clearer the gray level image is; when the gray image is overall brighter, the smaller the first clustering center, i.e., the darker the dark portion in the gray image, the clearer the gray image.
If the gray image is not divided according to the illumination degree, only one mode is adopted to calculate the focus evaluation value, a plurality of wave crests exist in a focus evaluation value curve during large-range focusing, and the best focus position may not be searched after the focus search process is combined. In the embodiment of the invention, the gray level image is divided according to the illumination degree, and the focus evaluation value is calculated in different ways according to different illumination degrees, so that the focus evaluation value curve is a single peak in large-range focusing, and the optimal focus position can be accurately searched by combining the focus search process.
In the embodiment of the invention, the gray values of all pixel points in the gray image are subjected to the first K-means clustering, and meanwhile, the definition of the gray image is calculated by selecting a proper clustering set according to the gray mean value of the gray image, so that the accuracy of evaluating the definition of the image is improved in the process of large-range focusing.
Step S102: and determining a second focusing position as a final focusing position according to a second focusing evaluation method in a preset interval with the first focusing position as the center, wherein the second focusing evaluation method evaluates the definition of the image through a sharpening operator and a second K-means clustering algorithm.
The accuracy of the first focus position obtained after the rough focus evaluation is often further improved. In the embodiment of the invention, after the coarse focusing evaluation is finished, the fine focusing evaluation is carried out, and in a preset interval taking the first focusing position as the center, a second focusing position is determined for a plurality of images shot in the preset interval by adopting a second focusing evaluation method. The preset interval is smaller than the search interval at the time of the coarse focus evaluation, and therefore may also be referred to as a fine focus evaluation.
As an embodiment of the present invention, a second focus evaluation method is shown in fig. 3, and includes:
step S301: and calculating to obtain a gradient image of the gray level image by adopting a sharpening operator.
In embodiments of the present invention, the sharpening operator includes, but is not limited to, a Sobel operator, a Roberts operator, a Laplacian operator, or an absolute gradient operator. If the sharpening operator is an absolute gradient operator, aiming at each pixel point, the gray value is v0, the gray values of the four fields are v1, v2, v3 and v4 respectively, the gradient value of the pixel point is Grad = abs (v0-v1) + abs (v0-v2) + abs (v0-v3) + abs (v0-v4), and abs is modulo operation; and for the pixel points on the four edges of the gray image, the gradient value of the pixel points does not have complete four fields and is subjected to zero setting treatment.
Step S302: and performing second K-means clustering on the gradient values of all the pixel points in the gradient image to obtain a clustering center of each type.
In the embodiment of the invention, the gradient values of all the pixel points in the gradient image are subjected to second K-means clustering. Wherein K2 is 2, that is, the gradient values of all the pixel points in the gradient image are divided into a third clustering set and a fourth clustering set; the third clustering center initial value of the third clustering set is the minimum gradient value of all pixel points in the gradient image, and the fourth clustering center initial value of the fourth clustering set is the maximum gradient value of all pixel points in the gradient image, which can also be called as dividing the gradient values of all pixel points in the gradient image into a low gradient value pixel point clustering set and a high gradient value pixel point clustering set.
The process of the second K-means clustering is similar to the process of the first K-means clustering, and is not repeated herein.
In the embodiment of the invention, the main operation processes are convolution operation for solving gradient values and K mean value clustering operation, and the method has small operation amount and stable performance.
Step S303: and evaluating the definition of the gray level image according to the fourth clustering center.
In the embodiment of the present invention, the fourth cluster center is used as the focus evaluation value. The larger the fourth clustering center is, that is, the larger the center of the high gradient value pixel point clustering set in the gradient image is, the clearer the gray level image is.
In the embodiment of the invention, the acquired gray level image is converted into the gradient image in the preset interval, then the pixels in the gradient image are divided by the K-means clustering algorithm, the definition of the gray level image is represented by the center of the high gradient value pixel clustering set, namely the mean value of the high gradient value pixel clustering set, the one-sidedness when the definition of the gray level image is represented by the maximum gradient value is avoided, and the method is suitable for the image definition evaluation near the final focusing position.
When the wide-range focusing is carried out, the first focusing position is determined through the state of the image and the first K-means clustering algorithm, the first focusing position can be called coarse focusing evaluation, then the second focusing position is determined through the sharpening operator and the second K-means clustering algorithm in a preset interval with the first focusing position as the center, the second focusing position is the final focusing position and can be called fine focusing evaluation, different focusing evaluation modes are adopted according to the characteristics of the images which are far away from the final focusing position and are near to the final focusing position in the definition evaluation, and the accuracy of the image definition evaluation is improved.
Example two
Fig. 4 is a block diagram of a focusing evaluation apparatus according to a second embodiment of the present invention, and as shown in fig. 4, the apparatus includes: a first evaluation unit 41 and a second evaluation unit 42.
The first evaluation unit 41 is configured to determine a first focus position according to a first focus evaluation method, which evaluates the sharpness of the image by the state of the image and a first K-means clustering algorithm.
The second evaluation unit 42 is configured to determine, within a preset interval centered on the first focusing position, a second focusing position as a final focusing position according to a second focusing evaluation method, where the second focusing evaluation method evaluates the sharpness of the image through a sharpening operator and a second K-means clustering algorithm.
Preferably, as shown in fig. 5, the first evaluation unit 41 includes: a first clustering subunit 411, a first calculating subunit 412, a state dividing subunit 413, and a first evaluating subunit 414.
The first clustering subunit 411 is configured to perform first K-means clustering on the gray values of all the pixels in the gray image to obtain a clustering center of each class, where K1 is 2, and divide the gray values of all the pixels in the gray image into a first clustering set and a second clustering set, where a first clustering center initial value of the first clustering set is a minimum gray value of all the pixels in the gray image, and a second clustering center initial value of the second clustering set is a maximum gray value of all the pixels in the gray image.
The first calculating subunit 412 is configured to calculate a mean grayscale value of the grayscale image.
The state division unit 413 is used to divide the states of the grayscale image according to the grayscale mean.
The first evaluation subunit 414 is configured to evaluate the sharpness of the grayscale image according to the cluster center of each class and the state of the grayscale image.
Preferably, the state-scoring unit 413 is specifically: and dividing the gray level image into a first state and a second state according to the gray level average value, wherein the first state is the gray level average value greater than a preset threshold value, and the second state is the gray level average value less than the preset threshold value.
Correspondingly, the first evaluation subunit 414 specifically is: if the gray level image is in the first state, the focusing evaluation value is a difference value between 255 and the first clustering center; if the gray scale image is in the second state, the focus evaluation value is the second cluster center.
Preferably, as shown in fig. 6, the second evaluation unit 42 includes: a second calculation subunit 421, a second clustering subunit 422, and a second evaluation subunit 423.
The second calculating subunit 421 is configured to calculate a gradient image of the grayscale image by using a sharpening operator.
The second clustering subunit 422 is configured to perform second K-means clustering on the gradient values of all the pixels in the gradient image to obtain a clustering center of each cluster, where K2 is 2, and divide the gradient values of all the pixels in the gradient image into a third clustering set and a fourth clustering set, where a third clustering center initial value of the third clustering set is a minimum gradient value of all the pixels in the gradient image, and a fourth clustering center initial value of the fourth clustering set is a maximum gradient value of all the pixels in the gradient image.
The second evaluation subunit 423 is configured to evaluate the sharpness of the grayscale image according to the fourth cluster center.
Preferably, the sharpening operator comprises a Sobel operator, a Roberts operator, a Laplacian operator, or an absolute gradient operator.
The focus evaluation method executed in the focus evaluation device corresponds to the method described in the first embodiment one by one, and is not described herein again.
When the wide-range focusing is carried out, the first focusing position is determined through the state of the image and the first K-means clustering algorithm, the first focusing position can be called coarse focusing evaluation, then the second focusing position is determined through the sharpening operator and the second K-means clustering algorithm in a preset interval with the first focusing position as the center, the second focusing position is the final focusing position and can be called fine focusing evaluation, different focusing evaluation modes are adopted according to the characteristics of the images which are far away from the final focusing position and are near to the final focusing position in the definition evaluation, and the accuracy of the image definition evaluation is improved.
While embodiments of the present invention have been shown and described above, it is to be understood that the above embodiments are illustrative and not to be construed as limiting the invention, which is within the scope of the invention for one of ordinary skill in the art.