Image processing method, device, electronic device and storage medium
By dynamically adjusting the threshold in image processing, the problem of poor image effects caused by single threshold selection in the prior art is solved, and better image processing effects are achieved.
Patent Information
- Application Number
- CN202210252464.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-03-15
- Publication Date
- 2024-09-24
- Estimated Expiration
- 2042-03-15
AI Technical Summary
In existing image binarization processing methods, the threshold selection is relatively simple, resulting in poor final processing results.
By obtaining the original image for grayscale processing, comparing the pixel value with the preset initial threshold, binary processing is performed, and the threshold is adjusted according to the indicator data until the preset convergence conditions are met, and the threshold selection is dynamically optimized.
It achieves more flexible and purposeful threshold selection, improves the image processing effect, and ensures better image performance.
Smart Images

Figure CN114627146B
Abstract
Description
Technical Field
[0001] The present application relates to the field of artificial intelligence technology, and in particular to an image processing method, device, electronic device and storage medium. Background Art
[0002] The binarization of an image means setting the grayscale value of the pixels on the image to 0 or 255, which means that the entire image presents an obvious black and white effect. By selecting the grayscale image with 256 brightness levels through an appropriate threshold, a binary image that can still reflect the overall and local features of the image is obtained.
[0003] At present, the threshold selection methods include bimodal method, p parameter method, maximum inter-class variance method and maximum entropy threshold method. In the related art, the selection of threshold is usually based on some determined thresholds to obtain the size of the final index, which mainly includes the statistics of the p parameter method, inter-class variance, entropy of the image grayscale histogram, etc. However, the thresholds selected by the above methods are usually relatively simple, which easily leads to poor image effects in the final processing. Summary of the invention
[0004] The main purpose of the embodiments of the present application is to provide an image processing method, device, electronic device and storage medium, aiming to ensure better image effects.
[0005] To achieve the above object, a first aspect of an embodiment of the present application provides an image processing method, the method comprising:
[0006] Get the original image;
[0007] Performing grayscale processing on the original image to obtain a grayscale image;
[0008] Comparing the pixel value corresponding to the grayscale image with a preset initial threshold, and binarizing the grayscale image according to the comparison result to obtain a binary image;
[0009] Performing index calculation on the binary image to obtain index data;
[0010] The preset initial threshold is adjusted to update the indicator data until a preset convergence condition is met.
[0011] In some embodiments, the step of comparing the pixel value corresponding to the grayscale image with a preset initial threshold, and binarizing the grayscale image according to the comparison result to obtain a binary image includes:
[0012] Obtaining a preset initial threshold and a number of pixel values corresponding to the grayscale image;
[0013] When the pixel value is greater than or equal to the preset initial threshold, setting the pixel value to a first grayscale value;
[0014] When the pixel value is less than the preset initial threshold, setting the pixel value to a second grayscale value;
[0015] The grayscale image is binarized according to the first grayscale value and the second grayscale value to obtain a binarized image.
[0016] In some embodiments, the binary image includes a first image area and a second image area, and the step of performing binarization processing on the grayscale image according to the first grayscale value and the second grayscale value to obtain the binary image includes:
[0017] Performing region segmentation processing on the grayscale image according to the first grayscale value to obtain the first image region;
[0018] Performing region segmentation processing on the grayscale image according to the second grayscale value to obtain the second image region.
[0019] In some embodiments, the step of performing index calculation on the binary image to obtain index data includes:
[0020] Calculating first distance parameter data corresponding to the first image area, second distance parameter data corresponding to the second image area, and image clarity data, wherein the first distance parameter data represents the distance from the pixel point corresponding to the first error value to the binary image contour when the first gray value corresponding to the first image area is a first error value, and the second distance parameter data represents the distance from the pixel point corresponding to the second error value to the binary image contour when the second gray value corresponding to the second image area is a second error value;
[0021] The indicator data is calculated according to the first distance parameter data, the second distance parameter data, the image clarity data and a preset indicator calculation formula.
[0022] In some embodiments, the preset indicator calculation formula is as follows:
[0023]
[0024] Wherein, I represents the indicator data, represents the i-th first distance parameter data in the binarized image, FN represents the first error value, represents the jth second distance parameter data in the binary image, FP represents the second error value, i and j are both integers, D represents a normalization factor, and the normalization factor is the sum of the distances from the pixel point corresponding to the binary image to the contour of the binary image, S total represents the total image area corresponding to the binary image, S white Represents the image area corresponding to the second image region.
[0025] In some embodiments, the preset convergence condition includes one of the following:
[0026] The indicator data meets the preset target threshold; or,
[0027] The first grayscale value corresponding to the pixel value exceeding the preset ratio in the grayscale image is the same as the first grayscale value corresponding to the pixel value before adjustment; or
[0028] The second grayscale value corresponding to the pixel value exceeding the preset ratio in the grayscale image is the same as the second grayscale value corresponding to the pixel value before adjustment.
[0029] In some embodiments, the step of adjusting the preset initial threshold to update the indicator data until a preset convergence condition is met includes:
[0030] Divide the interval according to the preset initial threshold to obtain a plurality of threshold intervals;
[0031] Calculating the average value for each threshold interval to obtain a number of average value data;
[0032] Using each of the mean data as the preset initial threshold value to update the indicator data corresponding to each of the mean data;
[0033] When the updated indicator data is smaller than the corresponding indicator data before the update, the interval division is performed again until the preset convergence condition is met.
[0034] To achieve the above-mentioned purpose, a second aspect of an embodiment of the present application provides an image processing device, the device comprising:
[0035] An image acquisition module, used for acquiring original images;
[0036] A grayscale processing module, used for performing grayscale processing on the original image to obtain a grayscale image;
[0037] A binarization module, used for comparing the pixel value corresponding to the grayscale image with a preset initial threshold, and performing binarization processing on the grayscale image according to the comparison result to obtain a binarized image;
[0038] An index calculation module, used to perform index calculation on the binary image to obtain index data;
[0039] The preset convergence module is used to adjust the preset initial threshold to update the indicator data until the preset convergence condition is met.
[0040] To achieve the above-mentioned purpose, the third aspect of an embodiment of the present application proposes an electronic device, which includes a memory, a processor, a program stored in the memory and executable on the processor, and a data bus for realizing connection and communication between the processor and the memory, and the program, when executed by the processor, implements the method described in the first aspect above.
[0041] To achieve the above-mentioned purpose, the fourth aspect of an embodiment of the present application proposes a storage medium, which is a computer-readable storage medium used for computer-readable storage, and the storage medium stores one or more programs, and the one or more programs can be executed by one or more processors to implement the method described in the first aspect above.
[0042] The image processing method, device, electronic device and storage medium proposed in the present application obtain an original image, grayscale process the original image to obtain a grayscale image, then compare the pixel value corresponding to the grayscale image with a preset initial threshold value to obtain a comparison result, then binarize the grayscale image according to the comparison result to obtain a binarized image, calculate the index of the binarized image to obtain index data, and adjust the preset initial threshold value to update the index data until the preset convergence condition is met. By dynamically adjusting the preset initial threshold value, the selection of the threshold value is more purposeful, and it is continuously iterated and optimized to meet the preset convergence condition. Compared with the related art, the selection of the threshold value of the embodiment of the present application is more flexible, so that the final processed image effect is better, and a better image effect can be guaranteed. BRIEF DESCRIPTION OF THE DRAWINGS
[0043] Figure 1 is a flowchart of an image processing method provided in an embodiment of the present application;
[0044] Figure 2 yes Figure 1 A schematic diagram of a sub-process of step S103 in FIG.
[0045] Figure 3 yes Figure 2 A schematic diagram of a sub-process of step S204 in FIG.
[0046] Figure 4 yes Figure 1 A schematic diagram of a sub-process of step S104 in FIG.
[0047] Figure 5 yes Figure 1 A schematic diagram of a sub-process of step S105 in FIG.
[0048] Figure 6 yes Figure 1 Another sub-process diagram of step S105 in FIG.
[0049] Figure 7 is a structural schematic diagram of an image processing device provided in an embodiment of the present application;
[0050] Figure 8 It is a schematic diagram of the hardware structure of the electronic device provided in the embodiment of the present application. DETAILED DESCRIPTION
[0051] In order to make the purpose, technical solution and advantages of the present application more clearly understood, the present application is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.
[0052] It should be noted that, although the functional modules are divided in the device schematic diagram and the logical order is shown in the flowchart, in some cases, the steps shown or described may be performed in a different order than the module division in the device or the order in the flowchart. The terms "first", "second", etc. in the specification, claims and the above drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence.
[0053] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as those commonly understood by those skilled in the art to which this application belongs. The terms used herein are only for the purpose of describing the embodiments of this application and are not intended to limit this application.
[0054] First, some nouns involved in this application are analyzed:
[0055] Artificial Intelligence (AI): It is a new technical science that studies and develops theories, methods, technologies and application systems for simulating, extending and expanding human intelligence. AI is a branch of computer science. AI attempts to understand the essence of intelligence and produce a new intelligent machine that can respond in a similar way to human intelligence. Research in this field includes robots, language recognition, image recognition, natural language processing and expert systems. AI can simulate the information process of human consciousness and thinking. AI is also a theory, method, technology and application system that uses digital computers or machines controlled by digital computers to simulate, extend and expand human intelligence, perceive the environment, acquire knowledge and use knowledge to obtain the best results.
[0056] Natural Language Processing (NLP): NLP uses computers to process, understand, and apply human languages (such as Chinese, English, etc.). NLP is a branch of artificial intelligence and an interdisciplinary subject between computer science and linguistics. It is often referred to as computational linguistics. Natural language processing includes grammatical analysis, semantic analysis, and text understanding. Natural language processing is commonly used in technical fields such as machine translation, handwritten and printed character recognition, speech recognition and text-to-speech conversion, information intent recognition, information extraction and filtering, text classification and clustering, public opinion analysis, and opinion mining. It involves data mining related to language processing, machine learning, knowledge acquisition, knowledge engineering, artificial intelligence research, and linguistic research related to language computing.
[0057] Grayscale: Use black tones to represent objects, that is, use black as the base color and different saturation of black to display images. Each grayscale object has a brightness value from 0% (white) to 100% (black). Images generated using black and white or grayscale scanners are usually displayed in grayscale. In the computer field, a grayscale digital image is an image with only one sampled color per pixel. Such images are usually displayed as grayscale from the darkest black to the brightest white, although in theory this sample can be any color of different shades, or even different colors at different brightness. Grayscale images are different from black and white images. In the field of computer graphics, black and white images only have black and white colors, while grayscale images have many levels of color depth between black and white. However, outside the field of digital images, "black and white images" also mean "grayscale images", for example, grayscale photos are usually called "black and white photos".
[0058] F-Measure (FM): When applied to classification, it is necessary to consider precision and recall comprehensively. Select a threshold, and F-Measure is a common method to select this threshold. F-Measure = Accuracy * Recall * 2 / (Accuracy + Recall), that is, F-Measure is the harmonic mean of Accuracy and Recall.
[0059] Peak Signal to Noise Ratio (PNSR): is an engineering term that represents the ratio of the maximum possible power of a signal to the destructive noise power that affects its representation accuracy. Since many signals have a very wide dynamic range, the peak signal to noise ratio is often expressed in logarithmic decibel units. The peak signal to noise ratio is often used as a measure of signal reconstruction quality in fields such as image compression, and is often simply defined by the mean square error (MSE).
[0060] Mean Squared Error (MSE): It is a measure of the difference between the estimator and the estimated quantity. Let t be an estimator of the population parameter θ determined based on the sample, (θ-t) 2 The mathematical expectation of is called the mean square error of the estimator t. It is equal to σ 2 +b 2 , where σ 2 and b are the variance and bias of t, respectively.
[0061] The binarization of an image means setting the grayscale value of the pixels on the image to 0 or 255, which means that the entire image presents an obvious black and white effect. By selecting the grayscale image with 256 brightness levels through an appropriate threshold, a binary image that can still reflect the overall and local features of the image is obtained.
[0062] At present, the threshold selection methods include the bimodal method (i.e., the gray value represented by the trough between the two peaks of the background and target histograms is used as the threshold), the p-parameter method (i.e., the gray value with the smallest statistic), the maximum inter-class variance method (i.e., the gray value corresponding to the maximum variance of the image grayscale divided into two groups) and the maximum entropy threshold method (i.e., the gray value that makes the entropy of the image grayscale histogram reach the maximum). In the related art, the selection of the threshold is usually based on some determined thresholds, such as F-Measure (FM), Peak Signal To Noise Ratio (PNSR), Mean Squared Error (MSE), etc. to obtain the size of the final index. These indicators mainly include the statistics of the p-parameter method, inter-class variance, entropy of the image grayscale histogram, etc. However, the thresholds selected by the above methods are usually relatively simple, for example, most of them are determined once, which easily leads to poor image effects obtained by the final processing. Therefore, how to provide an image processing method to ensure better image effects has become a technical problem that needs to be solved urgently.
[0063] Based on this, the embodiments of the present application provide an image processing method, device, electronic device and storage medium, aiming to ensure a good image effect. The image processing method provided in the embodiments of the present application can be applied to image processing, image analysis and the like.
[0064] The image processing method, device, electronic device and storage medium provided in the embodiments of the present application are specifically described through the following embodiments. First, the image processing method in the embodiments of the present application is described.
[0065] The embodiments of the present application can acquire and process relevant data based on artificial intelligence technology. Among them, artificial intelligence (AI) is the theory, method, technology and application system that uses digital computers or machines controlled by digital computers to simulate, extend and expand human intelligence, perceive the environment, acquire knowledge and use knowledge to obtain the best results.
[0066] AI basic technologies generally include sensors, dedicated AI chips, cloud computing, distributed storage, big data processing technology, operation / interaction systems, mechatronics, etc. AI software technologies mainly include computer vision technology, robotics technology, biometrics technology, speech processing technology, natural language processing technology, and machine learning / deep learning.
[0067] The image processing method provided in the embodiment of the present application relates to the field of artificial intelligence technology. The image processing method provided in the embodiment of the present application can be applied to a terminal, can be applied to a server side, or can be software running in a terminal or a server side. In some embodiments, the terminal can be a smart phone, a tablet computer, a laptop computer, a desktop computer, etc.; the server side can be configured as an independent physical server, or a server cluster or distributed system composed of multiple physical servers, or a cloud server that provides basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communications, middleware services, domain name services, security services, CDN, and big data and artificial intelligence platforms; the software can be an application that implements the image processing method, etc., but is not limited to the above forms.
[0068] The present application can be used in many general or special computer system environments or configurations. For example: personal computers, server computers, handheld or portable devices, tablet devices, multiprocessor systems, microprocessor-based systems, set-top boxes, programmable consumer electronics, network PCs, minicomputers, mainframe computers, distributed computing environments including any of the above systems or devices, etc. The present application can be described in the general context of computer-executable instructions executed by a computer, such as program modules. Generally, program modules include routines, programs, objects, components, data structures, etc. that perform specific tasks or implement specific abstract data types. The present application can also be practiced in distributed computing environments, in which tasks are performed by remote processing devices connected through a communication network. In a distributed computing environment, program modules can be located in local and remote computer storage media including storage devices.
[0069] Figure 1 is an optional flowchart of the image processing method provided in the embodiment of the present application, Figure 1The method may include but is not limited to steps S101 to S105.
[0070] Step S101, obtaining an original image;
[0071] Step S102, grayscale processing is performed on the original image to obtain a grayscale image;
[0072] Step S103, comparing the pixel value corresponding to the grayscale image with a preset initial threshold, and binarizing the grayscale image according to the comparison result to obtain a binary image;
[0073] Step S104, performing index calculation on the binary image to obtain index data;
[0074] Step S105, adjusting the preset initial threshold to update the indicator data until the preset convergence condition is met.
[0075] In the step S101 to step S105 of the embodiment of the present application, the original image is obtained, grayscale processing is performed on the original image to obtain a grayscale image, and then the pixel value corresponding to the grayscale image is compared with the preset initial threshold to obtain a comparison result, and then the grayscale image is binarized according to the comparison result to obtain a binarized image, and the index calculation is performed on the binarized image to obtain index data, and the preset initial threshold is adjusted to update the index data until the preset convergence condition is met. By dynamically adjusting the preset initial threshold, the selection of the threshold is more purposeful, and it is continuously iterated and optimized to meet the preset convergence condition. Compared with the related art, the selection of the threshold in the embodiment of the present application is more flexible, so that the image effect obtained by the final processing is better, and a better image effect can be guaranteed.
[0076] It is understandable that the embodiments of the present application continuously obtain better preset initial thresholds through iterative updates.
[0077] It should be noted that the iterative method is also called the optimal threshold method, which uses the initial value to calculate the grayscale mean of the two categories, and then takes the average value, and iterates in this way, which is similar to k-means clustering.
[0078] See also Figure 2 In some embodiments, step S103 may include but is not limited to steps S201 to S204:
[0079] Step S201, obtaining a preset initial threshold and a plurality of pixel values corresponding to the grayscale image;
[0080] Step S202, when the pixel value is greater than or equal to a preset initial threshold, the pixel value is set to a first grayscale value;
[0081] Step S203, when the pixel value is less than the preset initial threshold, the pixel value is set to a second grayscale value;
[0082] Step S204: binarize the grayscale image according to the first grayscale value and the second grayscale value to obtain a binary image.
[0083] In step S201 of some embodiments, a preset initial threshold is obtained. For example, in some embodiments, the preset initial threshold may be an average value corresponding to all pixels in the grayscale image, or other values in other embodiments, which are not specifically limited in the embodiments of the present application; and multiple corresponding pixel values in the grayscale image are obtained.
[0084] In step S202 of some embodiments, the pixel value corresponding to the grayscale image is compared with a preset initial threshold. When the pixel value is greater than or equal to the preset initial threshold, for example, a pixel value corresponding to the grayscale image is greater than 75, the pixel value is set to the first grayscale value.
[0085] In step S203 of some embodiments, the pixel value corresponding to the grayscale image is compared with a preset initial threshold. When the pixel value is less than the preset initial threshold, for example, a pixel value corresponding to the grayscale image is less than 75, the pixel value is set to the second grayscale value.
[0086] In step S204 of some embodiments, the above-mentioned comparison and binarization processing is performed on the pixel values corresponding to all corresponding pixel points in the grayscale image to obtain the first grayscale value or the second grayscale value corresponding to each pixel value.
[0087] It is understandable that to analyze and process a binary image, the grayscale image must first be binarized to obtain a binary image. In order to obtain an ideal binary image, closed and connected boundaries are generally used to define non-overlapping areas. All pixel values greater than or equal to a preset initial threshold are judged to belong to a specific object, and their grayscale values can be represented as 255. Otherwise, the pixel values corresponding to these pixels will be excluded from the specific object area, and the grayscale value can be represented as 0, indicating the background or the area outside the specific object area. The embodiment of the present application specifically obtains a binary image by binarizing the grayscale image according to the first grayscale value and the second grayscale value.
[0088] It can be understood that, since the embodiment of the present application performs binarization processing on the grayscale image, and the binarization processing is specifically a process of setting the pixel values corresponding to the pixel points on the grayscale image to 0 or 255, so that the binarized image obtained after the binarization processing can present an obvious black and white effect. Therefore, the first grayscale value of the embodiment of the present application can be 255, representing black, and the second grayscale value can be 0, representing white.
[0089] It is understandable that in other embodiments, the first grayscale value may also be set to 225 or other values, and the embodiments of the present application do not specifically limit this.
[0090] See also Figure 3 In some embodiments, the binary image includes a first image region and a second image region, and step S204 may include but is not limited to steps S301 to S302:
[0091] Step S301, performing region segmentation processing on the grayscale image according to the first grayscale value to obtain a first image region;
[0092] Step S302, performing region segmentation processing on the grayscale image according to the second grayscale value to obtain a second image region.
[0093] In step S301 of some embodiments, the first grayscale value can be 255, and the grayscale image is segmented into regions according to the first grayscale value, so that the region formed by the pixel points corresponding to the first grayscale value is used as the first image region. Then, the first image region of the embodiments of the present application can be represented as a specific object region and characterized as a black area.
[0094] In step S302 of some embodiments, the second grayscale value can be 0, and the grayscale image is segmented according to the second grayscale value, so that the area formed by the pixel points corresponding to the second grayscale value is used as the second image area. Then, the second image area of the embodiment of the present application can be represented as the area or background other than the specific object area, and characterized as a white area.
[0095] By acquiring a binary image, the embodiment of the present application is advantageous in that when the binary image is further processed, the collective property of the binary image is only related to the position of the pixel points corresponding to the first grayscale value of 255 or the second grayscale value of 0, and there is no need to involve the multi-level values of the pixels, thereby simplifying the image processing and reducing the amount of data processing and compression.
[0096] See also Figure 4 In some embodiments, step S104 may include but is not limited to steps S401 to S402:
[0097] Step S401, calculating first distance parameter data corresponding to the first image area, second distance parameter data corresponding to the second image area and image definition data;
[0098] Step S402, calculating the index data according to the first distance parameter data, the second distance parameter data, the image definition data and a preset index calculation formula.
[0099] In step S401 of some embodiments, the first distance parameter data represents the distance from the pixel point corresponding to the first error value to the binary image contour when the first grayscale value corresponding to the first image area is the first error value, and the second distance parameter data represents the distance from the pixel point corresponding to the second error value to the binary image contour when the second grayscale value corresponding to the second image area is the second error value.
[0100] It can be understood that the error value indicates that the gray value obtained after the above comparison and binarization processing is an error value. For example, in some embodiments, the first image area, that is, the black area, is defined as negative, and the second image area, that is, the white area, is defined as positive. The pixel point corresponding to the pixel value corresponding to the first error value is represented as a false positive, and the pixel point corresponding to the pixel value corresponding to the second error value is represented as a false negative. Then:
[0101] A false positive means that the pixel corresponding to the false positive originally belongs to negative. After comparison and binarization according to the preset initial threshold, the pixel value corresponding to the pixel is set to the second gray value, that is, it is set to positive, indicating that the pixel is a false positive;
[0102] A false negative means that the pixel corresponding to the false negative originally belongs to the positive. After comparison and binarization according to the preset initial threshold, the pixel value corresponding to the pixel is set to the first gray value, that is, it is set to the negative, indicating that the pixel is a false negative;
[0103] Get the number of false negatives, represented by i, where false negatives correspond one-to-one to the first error value;
[0104] The number of false positives is obtained, denoted by j, where the false positives correspond to the second error values one by one, wherein i, j = 0, 1, 2, 3 ... N.
[0105] Since the image index in the related art focuses on separating the black and white areas, but has no special requirements for image clarity, etc., the embodiment of the present application calculates the first distance parameter data corresponding to the first image area, the second distance parameter data corresponding to the second image area, and the image clarity data, and then calculates the index data according to the first distance parameter data, the second distance parameter data, the image clarity data, and the preset index calculation formula, so that the image clarity is taken into account in the index data, thereby making the image effect obtained by the final processing better, so as to ensure a better image effect.
[0106] It can be understood that the image clarity data can be characterized as the proportion of the white area (ie, the second image area) representing the degree of separation between the black and white areas (ie, the first image area and the second image area in the binary image).
[0107] In some embodiments, the preset indicator calculation formula is as follows:
[0108]
[0109] Among them, I represents indicator data, represents the i-th first distance parameter data in the binary image, FN (FalseNegative) represents the first error value, represents the jth second distance parameter data in the binary image, FP (False Positive) represents the second error value, i and j are both integers, D represents the normalization factor, and the normalization factor is the sum of the distances from the pixel point corresponding to the binary image to the contour of the binary image, S total Represents the total image area corresponding to the binary image, S white Indicates the image area corresponding to the second image region.
[0110] It can be understood that the indicator data in the embodiment of the present application includes first indicator data, second indicator data and image clarity data, wherein the first indicator data is The first index data represents the index data corresponding to the first image area, that is, the black area, which is calculated based on the first distance parameter data and the normalization factor; the second index data is The second index data represents the index data corresponding to the second image area, that is, the white area, which is calculated based on the second distance parameter data and the normalization factor; the image clarity data is It is calculated based on the total image area corresponding to the binary image and the image area corresponding to the second image area. The embodiment of the present application prevents data overflow by setting the denominator of the image clarity data to the sum of the total image area corresponding to the binary image and the image area corresponding to the second image area (i.e., the white area).
[0111] The embodiment of the present application calculates the final index data by selecting the method of combining the index for separating black and white areas (i.e., the first index data and the second index data) with the image clarity data. The index data can characterize the image integration performance and the proportion of white areas in the degree of separation of black and white areas.
[0112] It can be understood that the smaller the index data is, the better the image integration performance and the ability to separate the black and white areas are, which means that the final effect of the image is also better.
[0113] See also Figure 5 In some embodiments, the preset convergence condition in step S105 may include but is not limited to one of steps S501 to S503:
[0114] Step S501: the indicator data meets the preset target threshold; or,
[0115] Step S502: the first grayscale value corresponding to the pixel values exceeding the preset ratio in the grayscale image is the same as the first grayscale value corresponding to the pixel values before adjustment; or
[0116] Step S503: The second grayscale value corresponding to the pixel value exceeding the preset ratio in the grayscale image is the same as the second grayscale value corresponding to the pixel value before adjustment.
[0117] In step S501 of some embodiments, when the indicator data meets the preset target threshold, it means that the preset convergence condition is met. At this time, the binary image corresponding to the indicator data that meets the preset target threshold is the final output image. It is understandable that the preset target threshold can be preset, and the embodiments of the present application do not specifically limit this. For example, the test image with the best effect can be obtained through multiple tests, inspections and comparisons, and the current indicator data corresponding to the test image with the best effect is used as the preset target threshold, so that in the actual processing of the binary image, the preset initial threshold is continuously adjusted to update the indicator data until the indicator data meets the preset target threshold, so as to output the binary image corresponding to the indicator data that meets the preset target threshold, and the obtained binary image has higher clarity and better separation of black and white areas.
[0118] It is understandable that, since the indicator data can characterize the image integration performance and the proportion of white area in the degree of separation of black and white areas, the preset target threshold corresponding to the indicator data can also be set manually, for example, according to different accuracy requirements, such as 0.5, 0.1, etc.
[0119] In step S502 of some embodiments, when the first grayscale value corresponding to the pixel values exceeding the preset ratio in the grayscale image is the same as the first grayscale value corresponding to the grayscale image before adjustment, or in step S503 of some embodiments, when the second grayscale value corresponding to the pixel values exceeding the preset ratio in the grayscale image is the same as the second grayscale value corresponding to the grayscale image before adjustment, it indicates that the preset convergence condition is satisfied, and the binary image is the final output image at this time. It can be understood that both step S502 and step S503 indicate that the preset convergence condition is satisfied by continuous iteration until convergence. It can be understood that in the grayscale image at this time, the first grayscale value or the second grayscale value corresponding to the pixel values exceeding the preset ratio, for example, the preset ratio of 80%, no longer changes, that is, the adjusted first grayscale value is the same as the corresponding first grayscale value before adjustment, or the adjusted second grayscale value is the same as the corresponding second grayscale value before adjustment, indicating that the preset convergence condition is satisfied.
[0120] See also Figure 6 In some embodiments, step S105 may include but is not limited to steps S601 to S604:
[0121] Step S601, dividing the interval according to the preset initial threshold value to obtain a plurality of threshold intervals;
[0122] Step S602, calculating the average value for each threshold interval to obtain a number of average value data;
[0123] Step S603, taking each mean value data as a preset initial threshold value to update the indicator data corresponding to each mean value data;
[0124] Step S604: when the updated indicator data is smaller than the corresponding indicator data before updating, the interval is re-divided until the preset convergence condition is met.
[0125] Specifically, assuming that for a grayscale image, the corresponding pixel value is Then, the preset initial threshold is set to 75 according to the pixel value corresponding to the grayscale image. It can be understood that 75 is the average value of the pixel value corresponding to the grayscale image. Then, the pixel value corresponding to the grayscale image is compared with the preset initial threshold, and the grayscale image is binarized according to the comparison result to obtain a binarized image. Therefore, the binarized pixel matrix corresponding to the binarized image of this embodiment is obtained as follows: The index calculation is performed on the binary pixel matrix corresponding to the binary image to obtain index data.
[0126] The preset initial threshold is then adjusted to update the indicator data until the preset convergence condition is met. For example, the preset initial threshold is divided into intervals to obtain several threshold intervals. It is understandable that the preset initial threshold of this embodiment is 75, and the interval division can be performed to obtain a first threshold interval of [0, 75] and a second threshold interval of (75, 255].
[0127] Then, the average value is calculated for each threshold interval to obtain several average data. For example, the average value is calculated for the first threshold interval to obtain the first average data of 37.5, and the average value is calculated for the second threshold interval to obtain the second average data of 90.
[0128] Each mean data is used as a preset initial threshold to update the index data corresponding to each mean data. For example, the first mean data is used as the preset initial threshold, that is, the preset initial threshold is 37.5, the preset initial threshold is compared with the pixel value corresponding to the grayscale image, and the grayscale image is binarized according to the comparison result to obtain an adjusted binarized image, and the index is calculated on the adjusted binarized image to obtain updated index data; for another example, the second mean data is used as the preset initial threshold, that is, the preset initial threshold is 90, the preset initial threshold is compared with the pixel value corresponding to the grayscale image, and the grayscale image is binarized according to the comparison result to obtain an adjusted binarized image, and the index is calculated on the adjusted binarized image to obtain updated index data.
[0129] It is understandable that the smaller the index data of the embodiment of the present application is, the better the image integration performance and the ability to separate the black and white areas are. Therefore, the embodiment of the present application needs to compare the updated index data with the index data before the update. When the updated index data decreases, it is necessary to reset the preset initial threshold value according to the threshold interval corresponding to the updated index data. That is, when the updated index data is less than the corresponding index data before the update, the interval is re-divided until the preset convergence condition is met.
[0130] For example, in the first threshold interval [0, 75], the corresponding first mean data is 37.5, that is, the preset initial threshold is adjusted to 37.5. Assuming that the updated index data is smaller than the index data before the update according to the adjusted preset initial threshold, the interval is re-divided according to the adjusted preset initial threshold, for example, the interval is divided according to the preset initial threshold of 37.5, and the threshold intervals are: (1, 37.5) and (37.5, 75). Then, step S602 to step S604 are repeated until the preset convergence condition is met.
[0131] The embodiment of the present application continuously iterates and updates the indicator data through steps S601 to S604 to obtain better indicator data. Through the dynamic adjustment method, the selection of the preset initial threshold is more purposeful, and it is continuously iterated and optimized until convergence. It is more flexible and has better effects than the "one-size-fits-all" selection in the related technology.
[0132] The embodiment of the present application processes the denominator in the image clarity data, that is, takes the inverse of the white area ratio, which can effectively prevent overflow and make the indicator data more comprehensive.
[0133] Specifically, the embodiment of the present application obtains the original image, performs grayscale processing on the original image, obtains the grayscale image, then compares the pixel value corresponding to the grayscale image with the preset initial threshold value to obtain the comparison result, then performs binarization processing on the grayscale image according to the comparison result to obtain the binarized image, performs index calculation on the binarized image to obtain index data, and continuously adjusts the preset initial threshold value to update the index data until the preset convergence condition is met. By dynamically adjusting the preset initial threshold value, the selection of the threshold value is more purposeful, and it is continuously iterated and optimized to meet the preset convergence condition. Compared with the related art, the selection of the threshold value of the embodiment of the present application is more flexible, so that the final processed image effect is better, and a better image effect can be guaranteed.
[0134] See also Figure 7 The present application also provides an image processing device that can implement the above-mentioned image processing method. The device includes:
[0135] An image acquisition module 701 is used to acquire an original image;
[0136] Grayscale processing module 702, used to perform grayscale processing on the original image to obtain a grayscale image;
[0137] A binarization module 703 is used to compare the pixel value corresponding to the grayscale image with a preset initial threshold value, and perform binarization processing on the grayscale image according to the comparison result to obtain a binarized image;
[0138] The index calculation module 704 is used to calculate the index of the binary image to obtain the index data;
[0139] The preset convergence module 705 is used to adjust the preset initial threshold to update the indicator data until the preset convergence condition is met.
[0140] The embodiment of the present application also provides an electronic device, the electronic device comprising: a memory, a processor, a program stored in the memory and executable on the processor, and a data bus for realizing connection and communication between the processor and the memory, and the program is executed by the processor to realize the above-mentioned image processing method. The electronic device can be any intelligent terminal including a tablet computer, a car computer, etc.
[0141] See also Figure 8 , Figure 8 The hardware structure of an electronic device of another embodiment is illustrated, and the electronic device includes:
[0142] The processor 801 may be implemented by a general-purpose CPU (Central Processing Unit), a microprocessor, an application-specific integrated circuit (ASIC), or one or more integrated circuits, and is used to execute relevant programs to implement the technical solutions provided in the embodiments of the present application;
[0143] The memory 802 can be implemented in the form of a read-only memory (ROM), a static storage device, a dynamic storage device, or a random access memory (RAM). The memory 802 can store an operating system and other application programs. When the technical solution provided in the embodiment of this specification is implemented by software or firmware, the relevant program code is stored in the memory 802, and the processor 801 calls and executes the image processing method of the embodiment of this application;
[0144] Input / output interface 803, used to implement information input and output;
[0145] The communication interface 804 is used to realize the communication interaction between the device and other devices. The communication can be realized through a wired manner (such as USB, network cable, etc.) or a wireless manner (such as mobile network, WIFI, Bluetooth, etc.);
[0146] A bus 805 that transmits information between the various components of the device (e.g., the processor 801, the memory 802, the input / output interface 803, and the communication interface 804);
[0147] The processor 801 , the memory 802 , the input / output interface 803 and the communication interface 804 are connected to each other in communication within the device via a bus 805 .
[0148] An embodiment of the present application also provides a storage medium, which is a computer-readable storage medium used for computer-readable storage. The storage medium stores one or more programs, and the one or more programs can be executed by one or more processors to implement the above-mentioned image processing method.
[0149] The memory, as a non-transient computer-readable storage medium, can be used to store non-transient software programs and non-transient computer executable programs. In addition, the memory may include a high-speed random access memory, and may also include a non-transient memory, such as at least one disk storage device, a flash memory device, or other non-transient solid-state storage device. In some embodiments, the memory may optionally include a memory remotely disposed relative to the processor, and these remote memories may be connected to the processor via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.
[0150] The image processing method, image processing device, electronic device and storage medium of the embodiment of the present application obtain an original image, grayscale process the original image to obtain a grayscale image, then compare the pixel value corresponding to the grayscale image with a preset initial threshold value to obtain a comparison result, then binarize the grayscale image according to the comparison result to obtain a binarized image, calculate the index of the binarized image to obtain index data, and continuously adjust the preset initial threshold value to update the index data until the preset convergence condition is met. By dynamically adjusting the preset initial threshold value, the selection of the threshold value is more purposeful, and it is continuously iterated and optimized to meet the preset convergence condition. Compared with the related art, the selection of the threshold value of the embodiment of the present application is more flexible, so that the image effect obtained by the final processing is better, and a better image effect can be guaranteed.
[0151] The embodiments described in the embodiments of the present application are intended to more clearly illustrate the technical solutions of the embodiments of the present application and do not constitute a limitation on the technical solutions provided in the embodiments of the present application. Those skilled in the art will appreciate that with the evolution of technology and the emergence of new application scenarios, the technical solutions provided in the embodiments of the present application are also applicable to similar technical problems.
[0152] It can be understood by those skilled in the art that Figure 1-6 The technical solutions shown in the figure do not constitute a limitation on the embodiments of the present application, and may include more or fewer steps than those shown in the figure, or a combination of certain steps, or different steps.
[0153] The device embodiments described above are merely illustrative, and the units described as separate components may or may not be physically separated, that is, they may be located in one place or distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0154] Those skilled in the art will appreciate that all or some of the steps in the methods disclosed above, and the functional modules / units in the systems and devices may be implemented as software, firmware, hardware, or a suitable combination thereof.
[0155] The terms "first", "second", "third", "fourth", etc. (if any) in the specification of the present application and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchangeable where appropriate, so that the embodiments of the present application described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "having" and any of their variations are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device comprising a series of steps or units is not necessarily limited to those steps or units clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.
[0156] It should be understood that in the present application, "at least one (item)" means one or more, and "plurality" means two or more. "And / or" is used to describe the association relationship of associated objects, indicating that three relationships may exist. For example, "A and / or B" can mean: only A exists, only B exists, and A and B exist at the same time, where A and B can be singular or plural. The character " / " generally indicates that the objects associated before and after are in an "or" relationship. "At least one of the following" or similar expressions refers to any combination of these items, including any combination of single or plural items. For example, at least one of a, b or c can mean: a, b, c, "a and b", "a and c", "b and c", or "a and b and c", where a, b, c can be single or multiple.
[0157] In the several embodiments provided in the present application, it should be understood that the disclosed devices and methods can be implemented in other ways. For example, the device embodiments described above are only schematic. For example, the division of the above units is only a logical function division. There may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.
[0158] The units described above as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0159] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.
[0160] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present application, or the part that contributes to the prior art, or all or part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including multiple instructions to enable a computer device (which can be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods of various embodiments of the present application. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (Read Only Memory, referred to as ROM), random access memory (Random Access Memory, referred to as RAM), disk or optical disk and other media that can store programs.
[0161] The preferred embodiments of the present invention are described above with reference to the accompanying drawings, but the scope of the rights of the present invention is not limited thereto. Any modification, equivalent substitution and improvement made by a person skilled in the art without departing from the scope and essence of the present invention should be within the scope of the rights of the present invention.
Claims
1. An image processing method, characterized in that: The method comprises: Get the original image; Performing grayscale processing on the original image to obtain a grayscale image; Comparing the pixel value corresponding to the grayscale image with a preset initial threshold, and binarizing the grayscale image according to the comparison result to obtain a binary image; wherein the binary image includes a first image area and a second image area; Performing index calculation on the binary image to obtain index data; Adjusting the preset initial threshold to update the indicator data until a preset convergence condition is met; The step of performing index calculation on the binary image to obtain index data includes: Calculating first distance parameter data corresponding to the first image area, second distance parameter data corresponding to the second image area, and image clarity data, wherein the first distance parameter data represents the distance from the pixel point corresponding to the first error value to the binary image contour when the first gray value corresponding to the first image area is a first error value, and the second distance parameter data represents the distance from the pixel point corresponding to the second error value to the binary image contour when the second gray value corresponding to the second image area is a second error value; Calculate the index data according to the first distance parameter data, the second distance parameter data, the image clarity data and a preset index calculation formula; The preset index calculation formula is as follows: I= + + , Wherein, I represents the indicator data, represents the i-th first distance parameter data in the binarized image, FN represents the first error value, represents the jth second distance parameter data in the binary image, FP represents the second error value, i and j are both integers, D represents a normalization factor, and the normalization factor is the sum of the distances from the pixel point corresponding to the binary image to the contour of the binary image, S total represents the total image area corresponding to the binary image, S white Represents the image area corresponding to the second image region.
2. The image processing method according to claim 1, characterized in that: The step of comparing the pixel value corresponding to the grayscale image with a preset initial threshold, and binarizing the grayscale image according to the comparison result to obtain a binary image includes: Obtaining a preset initial threshold and a number of pixel values corresponding to the grayscale image; When the pixel value is greater than or equal to the preset initial threshold, setting the pixel value to the first grayscale value; When the pixel value is less than the preset initial threshold, setting the pixel value to the second grayscale value; The grayscale image is binarized according to the first grayscale value and the second grayscale value to obtain a binarized image.
3. The image processing method according to claim 2, characterized in that: The step of performing binarization processing on the grayscale image according to the first grayscale value and the second grayscale value to obtain a binary image comprises: Performing region segmentation processing on the grayscale image according to the first grayscale value to obtain the first image region; Performing region segmentation processing on the grayscale image according to the second grayscale value to obtain the second image region.
4. The image processing method according to any one of claims 2 to 3, characterized in that: The preset convergence condition includes one of the following: The indicator data meets the preset target threshold; or, The first grayscale value corresponding to the pixel value exceeding the preset ratio in the grayscale image is the same as the first grayscale value corresponding to the pixel value before adjustment; or The second grayscale value corresponding to the pixel value exceeding the preset ratio in the grayscale image is the same as the second grayscale value corresponding to the pixel value before adjustment.
5. The image processing method according to any one of claims 1 to 3, characterized in that: The step of adjusting the preset initial threshold to update the indicator data until a preset convergence condition is met includes: Divide the interval according to the preset initial threshold to obtain a plurality of threshold intervals; Calculating the average value for each threshold interval to obtain a number of average value data; Using each of the mean data as the preset initial threshold value to update the indicator data corresponding to each of the mean data; When the updated indicator data is smaller than the corresponding indicator data before the update, the interval division is performed again until the preset convergence condition is met.
6. An image processing device, characterized in that: The device comprises: An image acquisition module, used for acquiring original images; A grayscale processing module, used for performing grayscale processing on the original image to obtain a grayscale image; A binarization module, used for comparing the pixel value corresponding to the grayscale image with a preset initial threshold value, and binarizing the grayscale image according to the comparison result to obtain a binarized image; wherein the binarized image includes a first image area and a second image area; An index calculation module, used to perform index calculation on the binary image to obtain index data; A preset convergence module, used to adjust the preset initial threshold to update the indicator data until a preset convergence condition is met; The index calculation module is used to perform index calculation on the binary image to obtain index data, including: Calculating first distance parameter data corresponding to the first image area, second distance parameter data corresponding to the second image area, and image clarity data, wherein the first distance parameter data represents the distance from the pixel point corresponding to the first error value to the binary image contour when the first gray value corresponding to the first image area is a first error value, and the second distance parameter data represents the distance from the pixel point corresponding to the second error value to the binary image contour when the second gray value corresponding to the second image area is a second error value; Calculate the index data according to the first distance parameter data, the second distance parameter data, the image clarity data and a preset index calculation formula; The preset index calculation formula is as follows: I= + + , Wherein, I represents the indicator data, represents the i-th first distance parameter data in the binarized image, FN represents the first error value, represents the jth second distance parameter data in the binary image, FP represents the second error value, i and j are both integers, D represents a normalization factor, and the normalization factor is the sum of the distances from the pixel point corresponding to the binary image to the contour of the binary image, S total represents the total image area corresponding to the binary image, S white Represents the image area corresponding to the second image region.
7. An electronic device, characterized in that: The electronic device includes a memory, a processor, a program stored in the memory and executable on the processor, and a data bus for realizing connection and communication between the processor and the memory. When the program is executed by the processor, the steps of the image processing method as described in any one of claims 1 to 5 are realized.
8. A storage medium, the storage medium being a computer-readable storage medium, used for computer-readable storage, characterized in that: The storage medium stores one or more programs, and the one or more programs can be executed by one or more processors to implement the steps of the image processing method according to any one of claims 1 to 5.
