A method for adjusting image contrast
Through the maximum entropy principle, the image is divided into multiple parts and grayscale equalization is performed, which solves the problem of image display distortion caused by inappropriate contrast, and improves the image clarity and imaging quality.
Patent Information
- Application Number
- CN202411255880.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-09
- Publication Date
- 2024-11-15
- Estimated Expiration
- 2044-09-09
AI Technical Summary
Inappropriate contrast can cause distortion of image color display, affecting visual effects, and the prior art is difficult to avoid excessive stretching of the intermediate area while retaining the details of the bright and dark areas, resulting in grayscale layering.
The grayscale of the image is divided into multiple parts through the maximum entropy principle, and each part is individually equalized. The second and third distinction grayscale values are used as the segmentation threshold to divide the image into bright sub-image, dark sub-image and intermediate and perform grayscale histogram equalization for each part.
While retaining the details of the bright and dark areas, avoiding excessive stretching of the intermediate areas improves image imaging quality and enhances image clarity and color performance.
Smart Images

Figure CN118781039B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of image processing, and in particular to a method for adjusting image contrast. Background Art
[0002] In the field of images, contrast generally refers to the difference between the brightest and darkest parts of an image. When the contrast is greater, the difference between the bright and dark parts of the image is more obvious, which means the image is clearer and more eye-catching, and the colors are more vivid and bright; when the contrast is smaller, the image appears gray and dull, which means the whole picture is gray.
[0003] Therefore, inappropriate contrast will distort the color display of the image and directly affect the visual effect of the image. Adjusting the contrast can enhance the clarity of the image, improve the color performance of the image, avoid color distortion, and highlight or weaken specific areas, which is of great significance in practical use. Summary of the invention
[0004] In response to the above technical problems, the present invention proposes a method for adjusting image contrast. The method divides the image grayscale into multiple parts by repeatedly using the maximum entropy principle, and performs grayscale equalization on each part separately. While retaining the details of the bright and dark areas, it ensures that the middle area will not be over-stretched to cause grayscale stratification, thereby better retaining the image details and improving the imaging quality.
[0005] In order to achieve the above technical objectives, the present invention adopts the following technical solutions:
[0006] The present invention specifically relates to a method for adjusting image contrast, which comprises: step 1, grayscale processing a target image to obtain a grayscale image corresponding to the target image, and calculating a grayscale probability distribution of the grayscale image; step 2, traversing all grayscale values in the grayscale probability distribution, taking a grayscale value as a segmentation threshold each time, dividing the grayscale image into two parts, respectively calculating entropy values of the two parts, the sum of the entropy values of the two parts being the total entropy value of the image, recording the total entropy value corresponding to each grayscale value, and obtaining the grayscale value when the total entropy value is the maximum as the first distinguishing grayscale value loc1; step 3, taking the grayscale value obtained in step 2 as the segmentation threshold, and dividing the grayscale image into two parts; calculating the entropy values of the two parts respectively, and the sum of the entropy values of the two parts being the total entropy value of the image; recording the total entropy value corresponding to each grayscale value, and obtaining the grayscale value when the total entropy value is the maximum as the first distinguishing grayscale value loc1; step 3, taking the grayscale value obtained in step 2 as the segmentation threshold, and dividing the grayscale image into two parts; calculating the entropy values of the two parts respectively, and calculating the entropy values of the two parts as the total entropy value of the image; The first distinguishing grayscale value loc1 is used as the distinguishing threshold, the grayscale image is divided into a dark sub-image and a bright sub-image, and the corresponding second distinguishing grayscale value loc2 and the third distinguishing grayscale value loc3 of the dark sub-image and the bright sub-image when the total entropy value is maximum are calculated respectively; step 4, using the second distinguishing grayscale value loc2 and the third distinguishing grayscale value loc3 as the segmentation threshold, the grayscale image is divided into three parts, and grayscale histogram equalization is performed on the three parts respectively; step 5, converting the image after grayscale histogram equalization into a color image to obtain the final output image.
[0007] Furthermore, in step 2, the entropy values of the two parts are calculated respectively, and the sum of the entropy values of the two parts is the total entropy value of the image, which specifically includes:
[0008] ;
[0009] ;
[0010] Wherein, q is the segmentation threshold, K is the maximum grayscale level of the grayscale image, is the total entropy of the image, It represents the entropy of the background when the gray value q is used as the threshold for segmentation. It represents the entropy of the foreground when the gray value is q as the threshold for segmentation. p(i) refers to the proportion of pixels with gray value i in the entire image. It refers to the proportion of pixels smaller than the threshold in the entire image. It refers to the proportion of pixels greater than or equal to the threshold in the entire image.
[0011] Furthermore, in step 3, the first distinguishing grayscale value obtained in step 2 is used as a distinguishing threshold to divide the grayscale image into a dark sub-image and a bright sub-image, specifically including: pixels less than the first distinguishing grayscale value are dark sub-images, and pixels greater than or equal to the first distinguishing grayscale value are bright sub-images.
[0012] Furthermore, the step 3 respectively calculates the corresponding second distinguishing grayscale value loc2 and the third distinguishing grayscale value loc3 of the dark sub-image and the bright sub-image when the total entropy value is maximum, specifically including: step 31, generating an image set f1, saving the grayscale value of each position of the dark sub-image in the image set f1, and setting the image grayscale value of the remaining position of the image set f1 to 255; step 32, generating an image set f2, saving the grayscale value of each position of the bright sub-image in the image set f2, and setting the image grayscale value of the remaining position of the image set f2 to 0; step 33, calculating the total entropy value for each grayscale value from 0 to 255 in the image set f1, and obtaining the grayscale value when the total entropy value is maximum, calculating the total entropy value for each grayscale value from 0 to 255 in the image set f2, and obtaining the grayscale value when the total entropy value is maximum; step 34, repeating step 33, traversing each grayscale value in the image sets f1 and f2, and obtaining the second distinguishing grayscale value and the third distinguishing grayscale value of the image set f1 and the image set f2 respectively when the total entropy value is maximum.
[0013] Furthermore, in step 4, the second distinguishing grayscale value loc2 and the third distinguishing grayscale value loc3 are used as the division thresholds to divide the grayscale image into three parts, and grayscale histogram equalization is performed on the three parts of the image respectively, specifically including:
[0014] Step 41, for the three grayscale range intervals of 0:loc2, loc2+1:loc3, loc3+1:255, respectively count the probability cumulative sum of the probability of each grayscale value in each interval:
[0015] Among them, the cumulative result of 0:loc1 is , the cumulative result of loc1+1:loc2 is , the cumulative result of loc2+1:255 is , p(j) represents the frequency of occurrence of pixels with gray value j in the image;
[0016] Step 42, mapping the probability accumulation sum s1 to 0:loc2, and rounding the result:
[0017] s1=round(s1*loc2)
[0018] Map the probability accumulation sum s2 to loc2+1:loc3, and round the result:
[0019] s2=round(s2*(loc3-loc2-1))+loc2+1
[0020] Map the probability accumulation sum s3 to loc3+1:255 and round the result:
[0021] s3=round(s3*(255-loc3-1))+loc3+1
[0022] Step 43: output a grayscale image according to the mapping result.
[0023] On the other hand, the present invention further discloses a terminal, comprising a processor and a storage medium; the storage medium is used to store instructions; the processor is used to operate according to the instructions to execute the steps according to the above method.
[0024] The present invention divides the image grayscale into multiple parts by using the maximum entropy principle multiple times, and performs grayscale equalization on each part separately. The image contrast adjustment method can preserve the details of the bright and dark areas while ensuring that the middle area will not be overstretched to cause grayscale stratification, thereby better preserving image details and improving image quality. BRIEF DESCRIPTION OF THE DRAWINGS
[0025] The drawings described herein are used to provide a further understanding of the present disclosure and constitute a part of the present disclosure. The illustrative embodiments of the present disclosure and their descriptions are used to explain the present disclosure and do not constitute an improper limitation on the present disclosure. In the drawings:
[0026] Figure 1A schematic diagram showing a flow chart of a method for adjusting image contrast according to an embodiment of the present invention;
[0027] Figure 2 An original image provided by an embodiment of the present invention is shown;
[0028] Figure 3 shows a grayscale image obtained by processing an original image provided by an embodiment of the present invention;
[0029] Figure 4 shows the grayscale probability distribution obtained by processing the original image provided by one embodiment of the present invention;
[0030] Figure 5 A sub-image divided by a first grayscale threshold value calculated according to an embodiment of the present invention is shown;
[0031] Figure 6 Another sub-image divided by the first grayscale threshold calculated according to an embodiment of the present invention is shown;
[0032] Figure 7 The figure shows a grayscale image after the grayscale image is divided by the second and third grayscale discrimination thresholds calculated according to an embodiment of the present invention;
[0033] Figure 8 The figure shows the final image processed by one embodiment of the present invention. DETAILED DESCRIPTION
[0034] In order to make the purpose, technical solutions and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments.
[0035] Therefore, the following detailed description of the embodiments of the present invention provided in the accompanying drawings is not intended to limit the scope of the invention claimed for protection, but merely represents selected embodiments of the present invention. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0036] like Figure 1 As shown, the present invention provides a method for adjusting image contrast, which specifically includes:
[0037] Step 1: grayscale processing is performed on the target image to obtain its corresponding grayscale image, and the grayscale probability distribution of the grayscale image is calculated.
[0038] Step 2, traverse all grayscale values in the grayscale probability distribution, each time using a grayscale value as the segmentation threshold, divide the grayscale image into two parts, calculate the entropy values of the two parts respectively, the sum of the entropy values of the two parts is the total entropy value of the image, record the total entropy value corresponding to each grayscale value, and obtain the grayscale value when the total entropy value is the maximum as the first distinguishing grayscale value loc1.
[0039] Figure 2 An original image provided by an embodiment of the present invention, Figure 3 is the grayscale image obtained by processing the original image. Figure 4 The grayscale probability distribution obtained by processing the original image is shown.
[0040] like Figure 4 As shown, the present invention divides the grayscale image into two parts from low to high according to the grayscale value shown on the horizontal axis in the grayscale probability distribution, each time using a grayscale value as the segmentation threshold, calculates the entropy values of the two parts respectively, and calculates the total entropy value of the image which is the sum of the entropy values of the two parts, and records the total entropy value of the image corresponding to each grayscale value in turn. After traversing all the grayscale values in the grayscale probability distribution, the grayscale value when the total entropy value is the maximum is obtained as the first distinguishing grayscale value loc1.
[0041] In one embodiment, the entropy values of the two parts are calculated respectively, and the sum of the entropy values of the two parts is the total entropy value of the image, which specifically includes:
[0042] ;
[0043] ;
[0044] Wherein, q is the segmentation threshold, K is the maximum grayscale level of the grayscale image, is the total entropy of the image, It represents the entropy of the background when the gray value q is used as the threshold for segmentation. It represents the entropy of the foreground when the gray value is q as the threshold for segmentation. p(i) refers to the proportion of pixels with gray value i in the entire image. It refers to the proportion of pixels smaller than the threshold in the entire image. It refers to the proportion of pixels greater than or equal to the threshold in the entire image.
[0045] In actual images, since the difference between the bright and dark parts of the image is usually not large, how to perform appropriate contrast processing is particularly important. Since the greater the entropy, the more information the image represents, the present invention calculates and processes the grayscale image through the maximum entropy threshold method to obtain an optimal image segmentation threshold, which can maximize the sum of the entropies of the background and foreground parts, reducing the loss of information during image processing.
[0046] like Figure 5 , Figure 6 As shown, two sub-images are divided by the first distinguishing grayscale threshold calculated in one embodiment of the present invention. In one embodiment, the first distinguishing grayscale value obtained in step 2 is used as the distinguishing threshold to divide the grayscale image into a dark sub-image and a bright sub-image, including: pixels less than the first distinguishing grayscale value are dark sub-images, and pixels greater than or equal to the first distinguishing grayscale value are bright sub-images. Exemplarily, pixels less than or equal to the first distinguishing grayscale value may also be regarded as dark sub-images, and pixels greater than the first distinguishing grayscale value may be regarded as bright sub-images.
[0047] Since the actual image includes not only brightness pixels and darkness pixels, but also the middle area, in order to better preserve the image details, the present invention further uses the maximum entropy threshold segmentation method to divide the image more accurately, so that the middle area can be further distinguished from the brightness area and the darkness area.
[0048] The present invention further includes step 3, taking the first distinguishing grayscale value loc1 obtained in step 2 as the distinguishing threshold, dividing the grayscale image into a dark sub-image and a bright sub-image, and respectively calculating the corresponding second distinguishing grayscale value loc2 and third distinguishing grayscale value loc3 of the dark sub-image and the bright sub-image when the total entropy value is maximum.
[0049] In one embodiment, the step 3 respectively calculates the second distinguishing grayscale value loc2 and the third distinguishing grayscale value loc3 of the dark sub-image and the bright sub-image when the total entropy value is the maximum, specifically including:
[0050] Step 31, generate an image set f1, save the grayscale value of each position of the dark sub-image in the image set f1, and set the image grayscale value of the remaining position of the image set f1 to 255;
[0051] Step 32, generate an image set f2, save the grayscale value of each position of the bright sub-image in the image set f2, and set the image grayscale value of the remaining positions of the image set f2 to 0;
[0052] Step 33, calculate the total entropy value for each grayscale value from 0 to 255 in the image set f1, and obtain the grayscale value when the total entropy value is maximum; calculate the total entropy value for each grayscale value from 0 to 255 in the image set f2, and obtain the grayscale value when the total entropy value is maximum;
[0053] Step 34, repeat step 33, traverse each gray value in the image sets f1 and f2, and obtain the second distinguishing gray value loc2 and the third distinguishing gray value loc3 of the image sets f1 and f2 when the total entropy value is maximum.
[0054] It can be understood that the specific method of obtaining the second distinguishing grayscale value loc2 and the third distinguishing grayscale value loc3 in step 33 and step 34 is the same as the calculation method of obtaining the first distinguishing grayscale value loc1 in step 2, and both are obtained by the maximum entropy principle calculation method, which will not be repeated here.
[0055] like Figure 7 As shown in FIG. 1 , it is a grayscale image after the grayscale image is divided by the second and third grayscale thresholds calculated in one embodiment of the present invention. It can be seen that when the image is divided by the second grayscale value loc2 and the third grayscale value loc3 calculated by the present invention, the image can be more accurately divided into a brightness area, a dark area, and an intermediate area, thereby facilitating subsequent processing and avoiding excessive stretching of the intermediate area to cause grayscale stratification.
[0056] The present invention further comprises step 4, using the second distinguishing grayscale value loc2 and the third distinguishing grayscale value loc3 as segmentation thresholds, dividing the grayscale image into three parts, and then performing grayscale histogram equalization on the three parts of the image respectively.
[0057] Histogram equalization is a method of adjusting contrast using image histogram in the field of image processing. Since the present invention pre-divides the image into three sub-images, namely, the brightness area, the darkness area and the middle area, the grayscale histogram equalization image algorithm is used to process the three sub-images respectively, which can further enhance the local contrast without affecting other areas.
[0058] In one embodiment, the second distinguishing grayscale value loc2 and the third distinguishing grayscale value loc3 are used as the division thresholds to divide the grayscale image into three parts, and grayscale histogram equalization is performed on the three parts of the image respectively, specifically including:
[0059] Step 41, for the three grayscale range intervals of 0:loc2, loc2+1:loc3, loc3+1:255, respectively count the probability cumulative sum of the probability of each grayscale value in each interval:
[0060] Among them, the cumulative result of 0:loc1 is , the cumulative result of loc1+1:loc2 is , the cumulative result of loc2+1:255 is , p(j) represents the frequency of occurrence of pixels with gray value j in the image.
[0061] Step 42, map the s1 to 0:loc2, and round the result:
[0062] s1=round(s1*loc2)
[0063] Map s2 to loc2+1:loc3 and round the result:
[0064] s2=round(s2*(loc3-loc2-1))+loc2+1
[0065] Map s3 to loc3+1:255 and round the result:
[0066] s3=round(s3*(255-loc3-1))+loc3+1
[0067] Step 43: output the mapping result image.
[0068] The present invention divides the image grayscale into three sub-images, namely, the brightness area, the darkness area and the middle area based on the maximum entropy threshold method, and then performs grayscale histogram equalization on each part separately. While retaining the details of the bright and dark areas, the middle area will not be over-stretched to cause grayscale stratification, thereby better retaining the image details and improving the final imaging quality.
[0069] Step 5: Convert the grayscale histogram equalized image into a color image to obtain the final output image.
[0070] It should be noted that the grayscale image can be converted into a color image using a technology known in the field of images, and the present invention does not make any specific limitation.
[0071] Figure 8 The final processed image obtained by the method of the present invention is shown. It can be seen that compared with Figure 1 The original image in Figure 8 As shown in , the final image obtained by the method for adjusting image contrast provided by the present invention has a higher contrast and can better retain image details.
[0072] The present invention also discloses a terminal, comprising a processor and a storage medium; the storage medium is used to store instructions; the processor is used to operate according to the instructions to execute the steps according to the above method.
[0073] In summary, the present invention provides a method for adjusting image contrast, which divides the image grayscale into multiple parts by using the maximum entropy principle multiple times, and performs grayscale equalization on each part separately. The image contrast adjustment method can preserve the details of the bright and dark areas while ensuring that the middle area will not be overstretched to cause grayscale stratification, thereby better preserving image details and improving image quality.
[0074] It should be noted that, in this article, relational terms such as "first" and "second" are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the existence of other identical elements in the process, method, article or device including the elements.
Claims
1. A method for adjusting image contrast, characterized in that: include: Step 1, grayscale processing is performed on the target image to obtain its corresponding grayscale image, and the grayscale probability distribution of the grayscale image is calculated; Step 2, traverse all grayscale values in the grayscale probability distribution, each time taking a grayscale value as the segmentation threshold, divide the grayscale image into two parts, calculate the entropy values of the two parts respectively, the sum of the entropy values of the two parts is the total entropy value of the image, record the total entropy value corresponding to each grayscale value, and obtain the first distinguishing grayscale value loc1 when the total entropy value is the maximum; Step 3, using the first distinguishing grayscale value loc1 obtained in step 2 as a distinguishing threshold, dividing the grayscale image into a dark sub-image and a bright sub-image, and respectively calculating the corresponding second distinguishing grayscale value loc2 and third distinguishing grayscale value loc3 of the dark sub-image and the bright sub-image when the total entropy value is maximum; Step 4, using the second distinguishing grayscale value loc2 and the third distinguishing grayscale value loc3 as segmentation thresholds, dividing the grayscale image into three parts, and performing grayscale histogram equalization on the three parts of the image respectively; Step 5: Convert the grayscale histogram equalized image into a color image to obtain the final output image.
2. The method for adjusting image contrast according to claim 1, characterized in that: Also includes: In step 2, the entropy values of the two parts are calculated respectively, and the sum of the entropy values of the two parts is the total entropy value of the image, which specifically includes: ; ; Wherein, q is the segmentation threshold, K is the maximum grayscale level of the grayscale image, is the total entropy of the image, It represents the entropy of the background when the gray value q is used as the threshold for segmentation. It represents the entropy of the foreground when the gray value is q as the threshold for segmentation. p(i) refers to the proportion of pixels with gray value i in the entire image. It refers to the proportion of pixels smaller than the threshold in the entire image. It refers to the proportion of pixels greater than or equal to the threshold in the entire image.
3. The method for adjusting image contrast according to claim 2, characterized in that: In step 3, the first distinguishing gray value loc1 obtained in step 2 is used as a distinguishing threshold to divide the gray image into a dark sub-image and a bright sub-image, specifically including: pixels less than the first distinguishing gray value are dark sub-images, and pixels greater than or equal to the first distinguishing gray value are bright sub-images.
4. The method for adjusting image contrast according to claim 3, characterized in that: Also includes: The step 3 respectively calculates the second distinguishing grayscale value loc2 and the third distinguishing grayscale value loc3 corresponding to the dark sub-image and the bright sub-image when the total entropy value is the maximum, specifically including: Step 31, generate an image set f1, save the grayscale value of each position of the dark sub-image in the image set f1, and set the image grayscale value of the remaining position of the image set f1 to 255; Step 32, generate an image set f2, save the grayscale value of each position of the bright sub-image in the image set f2, and set the image grayscale value of the remaining positions of the image set f2 to 0; Step 33, calculate the total entropy value for each grayscale value from 0 to 255 in the image set f1, and obtain the grayscale value when the total entropy value is maximum; calculate the total entropy value for each grayscale value from 0 to 255 in the image set f2, and obtain the grayscale value when the total entropy value is maximum; Step 34, repeat step 33, traverse each gray value in the image sets f1 and f2, and obtain the second distinguishing gray value loc2 and the third distinguishing gray value loc3 of the image sets f1 and f2 when the total entropy value is maximum.
5. The method for adjusting image contrast according to claim 4, characterized in that: In step 4, the second distinguishing grayscale value loc2 and the third distinguishing grayscale value loc3 are used as the division thresholds to divide the grayscale image into three parts, and grayscale histogram equalization is performed on the three parts of the image respectively, specifically including: Step 41, for the three grayscale range intervals of 0:loc2, loc2+1:loc3, loc3+1:255, respectively count the probability cumulative sum of the probability of each grayscale value in each interval: Among them, the cumulative result of 0:loc1 is , the cumulative result of loc1+1:loc2 is , the cumulative result of loc2+1:255 is , p(j) represents the frequency of occurrence of pixels with gray value j in the image; Step 42, mapping the probability accumulation sum s1 to 0:loc2, and rounding the result: s1=round(s1*loc2) Map the probability accumulation sum s2 to loc2+1:loc3, and round the result: s2=round(s2*(loc3-loc2-1))+loc2+1 Map the probability accumulation sum s3 to loc3+1:255 and round the result: s3=round(s3*(255-loc3-1))+loc3+1 Step 43: output a grayscale image according to the mapping result.
6. A terminal comprising a processor and a storage medium; characterized in that: The storage medium is used to store instructions; The processor is configured to operate according to the instructions to execute the steps of the method according to any one of claims 1-5.
