Picture contrast recognition and verification method and device and medium
By converting the picture into a grayscale image and calculating the square root of the difference, the contrast of the picture is automatically detected, which solves the problem of time-consuming manual detection and achieves efficient contrast verification.
Patent Information
- Application Number
- CN202510225028.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-02-27
- Publication Date
- 2025-06-24
- Estimated Expiration
- Not applicable · inactive patent
AI Technical Summary
During the website development process, manual image contrast detection takes more time and is difficult to meet the needs of people with color blindness and weak color.
By converting the image to be tested into a grayscale image, the average brightness value of the grayscale image and the difference between each pixel and the average brightness value are calculated, and the contrast value of the picture is determined by using the square root to achieve automatic detection.
Automatically detect contrast during the image upload stage, save manual detection time, improve work efficiency, and ensure that image contrast meets the needs of special groups.
Smart Images

Figure CN120198689A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of picture contrast verification, and more specifically, to a picture contrast recognition and verification method, device, and medium. Background Art
[0002] During the website development process, in order to adapt to the viewing of special groups (color-blind and color-weak people), the pictures in the website need to meet a certain contrast. After each website picture is uploaded, tools are usually used to manually compare the color contrast in the picture, which is a large workload and consumes a lot of time. Summary of the Invention
[0003] Aiming at the deficiencies of the prior art, the present invention provides a picture contrast recognition and verification method, device, and medium.
[0004] According to one aspect of the present invention, there is provided a picture contrast recognition and verification method, including:
[0005] Read the picture to be verified and convert the picture to be verified into a grayscale image;
[0006] Calculate the average brightness value of the grayscale image;
[0007] Calculate the square of the difference between each pixel of the grayscale image and the average brightness value;
[0008] Calculate the average value of the squares of the differences of each pixel of the grayscale image;
[0009] Determine the contrast value of the picture to be verified according to the square root of the average value.
[0010] Optionally, calculating the average brightness value of the grayscale image includes:
[0011] Add the brightness values of each pixel of the grayscale image to obtain the total brightness value;
[0012] Divide the total brightness value by the number of pixels of the grayscale image to obtain the average brightness value.
[0013] Optionally, calculating the square of the difference between each pixel of the grayscale image and the average brightness value includes:
[0014] Create a zero matrix or an empty array with the same size as the grayscale image;
[0015] Loop through each pixel of the grayscale image, calculate the difference between each pixel and the average brightness value, and save the calculated difference result to the zero matrix or the empty array to generate a difference matrix or a difference array;
[0016] Perform an element-wise square operation on the difference matrix or difference array using the square function to obtain the square of the difference between each pixel and the average luminance value.
[0017] Optionally, calculating the average value of the squared differences of each pixel of the grayscale image includes:
[0018] Sum all the elements of the difference square array composed of the squared differences of all pixels in the grayscale image to obtain the total sum of the squared differences;
[0019] Divide the total sum of the squared differences by the number of elements in the difference square array to obtain the average value.
[0020] According to another aspect of the present invention, there is provided an apparatus for identifying and verifying the contrast of a picture, including:
[0021] A conversion module for reading the picture to be verified and converting the picture to be verified into a grayscale image;
[0022] A first calculation module for calculating the average luminance value of the grayscale image;
[0023] A second calculation module for calculating the square of the difference between each pixel of the grayscale image and the average luminance value;
[0024] A third calculation module for calculating the average value of the squared differences of each pixel of the grayscale image;
[0025] A determination module for determining the contrast value of the picture to be verified according to the square root of the average value.
[0026] According to yet another aspect of the present invention, there is provided a computer-readable storage medium storing a computer program for executing the method described in any of the above aspects of the present invention.
[0027] According to yet another aspect of the present invention, there is provided an electronic device including: a processor; a memory for storing executable instructions of the processor; the processor for reading the executable instructions from the memory and executing the instructions to implement the method described in any of the above aspects of the present invention.
[0028] Thus, the present invention reads the picture to be verified and converts the picture to be verified into a grayscale image; calculates the average luminance value of the grayscale image; calculates the square of the difference between each pixel of the grayscale image and the average luminance value; calculates the average value of the squared differences of each pixel of the grayscale image; and determines the contrast value of the picture to be verified according to the square root of the average value. The developed method for automatically verifying the contrast of a picture automatically detects the contrast of the picture during the picture uploading stage to detect whether the contrast of the picture meets the requirements, greatly saving the time for manually detecting the contrast and improving work efficiency. Description of the Drawings
[0029] The exemplary embodiments of the present invention can be more fully understood by referring to the following accompanying drawings:
[0030] Figure 1 is a schematic flowchart of a method for identifying and verifying picture contrast provided by an exemplary embodiment of the present invention;
[0031] Figure 2 is a schematic structural diagram of a device for identifying and verifying picture contrast provided by an exemplary embodiment of the present invention;
[0032] Figure 3 is the structure of an electronic device provided by an exemplary embodiment of the present invention. Detailed Embodiments
[0033] Hereinafter, example embodiments according to the present invention will be described in detail with reference to the accompanying drawings. Obviously, the described embodiments are only a part of the embodiments of the present invention, rather than all of the embodiments of the present invention. It should be understood that the present invention is not limited by the example embodiments described herein.
[0034] It should be noted that: Unless otherwise specifically stated, the relative arrangements of components and steps, numerical expressions, and numerical values set forth in these embodiments do not limit the scope of the present invention.
[0035] Those skilled in the art can understand that terms such as "first", "second", etc. in the embodiments of the present invention are only used to distinguish different steps, devices, or modules, etc., and neither represent any specific technical meaning nor indicate an inevitable logical order between them.
[0036] It should also be understood that in the embodiments of the present invention, "a plurality" may refer to two or more, and "at least one" may refer to one, two, or more.
[0037] It should also be understood that for any component, data, or structure mentioned in the embodiments of the present invention, unless otherwise clearly defined or given a contrary indication in the context, it can generally be understood as one or more.
[0038] In addition, the term "and / or" in the present invention is only a description of the association relationship of associated objects, indicating that there can be three relationships. For example, A and / or B can represent: A exists alone, A and B exist simultaneously, and B exists alone. In addition, the character " / " in the present invention generally represents an "or" relationship between the associated objects before and after.
[0039] It should also be understood that the present invention emphasizes the differences between the various embodiments, and their similarities or similarities can be referred to each other. For the sake of brevity, they will not be described one by one.
[0040] Meanwhile, it should be understood that, for ease of description, the dimensions of the various parts shown in the drawings are not drawn in actual proportional relationship.
[0041] The following description of at least one exemplary embodiment is merely illustrative in nature and is in no way a limitation on the present invention, its application, or its use.
[0042] Techniques, methods, and devices known to those of ordinary skill in the relevant art may not be discussed in detail, but where appropriate, the techniques, methods, and devices should be considered as part of the specification.
[0043] It should be noted that like reference numerals and letters refer to like items in the following figures, and thus, once an item is defined in one figure, further discussion thereof is not required in subsequent figures.
[0044] Embodiments of the present invention can be applied to electronic devices such as terminal devices, computer systems, servers, etc., which can operate with many other general or special computing system environments or configurations. Examples of well-known terminal devices, computing systems, environments, and / or configurations suitable for use with terminal devices, computer systems, servers, etc. include, but are not limited to: personal computer systems, server computer systems, thin clients, thick clients, handheld or laptop devices, microprocessor-based systems, set-top boxes, programmable consumer electronics, network personal computers, minicomputer systems, mainframe computer systems, and distributed cloud computing technology environments including any of the above systems, and so on.
[0045] Terminal devices, computer systems, servers, and other electronic devices can be described in the general context of computer system-executable instructions (such as program modules) executed by a computer system. Generally, program modules can include routines, programs, object programs, components, logic, data structures, etc., which perform specific tasks or implement specific abstract data types. The computer system / server can be implemented in a distributed cloud computing environment where tasks are performed by remote processing devices linked through a communication network. In a distributed cloud computing environment, program modules can be located on local or remote computing system storage media including storage devices.
[0046] Exemplary method
[0047] Figure 1 It is a schematic flowchart of a picture contrast recognition and verification method provided by an exemplary embodiment of the present invention. This embodiment can be applied to an electronic device, such as Figure 1 As shown, the picture contrast recognition and verification method 100 includes the following steps:
[0048] Step 101, read the picture to be verified and convert the picture to be verified into a grayscale image;
[0049] Step 102, calculate the average luminance value of the grayscale image;
[0050] Step 103, calculate the square of the difference between each pixel of the grayscale image and the average luminance value;
[0051] Step 104, calculate the average value of the squares of the differences of each pixel of the grayscale image;
[0052] Step 105, determine the contrast value of the picture to be tested according to the square root of the average value.
[0053] Specifically, during the website development process, in the picture transmission stage, develop a method for identifying and verifying the picture contrast. Automatically detect and verify the picture contrast during the picture uploading process to ensure that the picture contrast of the uploaded pictures meets the needs of special groups, saving the workload and time of manual comparison afterwards. The present invention uses related technologies of an image processing library to calculate the contrast of an image and give a contrast value. The specific implementation steps are as follows:
[0054] 1. Read the picture and convert it into a grayscale image.
[0055] 2. Calculate the average luminance of the image.
[0056] (1) Convert the image from the BGR color space to a grayscale image.
[0057] (2) Add up the luminance values of each pixel of the grayscale image.
[0058] (3) Divide the above sum by the number of pixels of the grayscale image to obtain the average luminance value.
[0059] 3. Calculate the square of the difference between each pixel of the image and the average luminance.
[0060] Calculate the difference between each pixel of the grayscale image and the average luminance to obtain a difference array. Finally, perform a square operation on the difference array to obtain the square of the difference between each pixel and the average luminance. Print it out.
[0061] (1) Create a zero matrix or empty array with the same size as the grayscale image to store the difference results. (The zero matrix means equivalent to a white picture with the same size as the original picture, the color number is #ffffff, the original picture is a color picture, and each pixel has its color number)
[0062] (2) Use a loop to traverse each pixel of the grayscale image.
[0063] (3) For each pixel, calculate the difference between it and the average brightness, and store the result in the difference array. (The average brightness has been calculated above and can be any color number such as #123456. As described above, compare the color number of each pixel in the original image with the average color number #123456 and store the difference in the difference array.)
[0064] (4) Use a square function to perform an element-wise square operation on the difference array to obtain the square of the difference between each pixel and the average brightness.
[0065] (5) Print out the square of the difference between each pixel and the average brightness.
[0066] 4. Calculate the average of the squared differences.
[0067] (1) First, perform a square operation on the difference array to obtain the square of each difference.
[0068] (2) Sum all the elements of the squared difference array.
[0069] (3) Divide the sum by the number of elements in the difference array to obtain the average value.
[0070] 5. Calculate the final contrast value, which can be represented by the square root of the average of the squared differences.
[0071] # Assume the average of the squared differences is mean_squared_diff
[0072] mean_squared_diff = 4.5
[0073] # Calculate the contrast value
[0074] contrast = np.sqrt(mean_squared_diff)
[0075] print('Contrast value:', contrast)
[0076] In the above code, we assume that the average of the squared differences is `mean_squared_diff`, and this value can be calculated through the previous steps. Then, use the `np.sqrt()` function to perform a square root operation on the average of the squared differences to obtain the final contrast value, and store the result in the `contrast` variable. Finally, print out `contrast`.
[0077] Thus, the present invention reads the picture to be inspected and converts the picture to be verified into a grayscale image; calculates the average brightness value of the grayscale image; calculates the square of the difference between each pixel of the grayscale image and the average brightness value; calculates the average value of the squares of the differences of each pixel of the grayscale image; and determines the contrast value of the picture to be inspected according to the square root of the average value. The developed automatic picture contrast verification method automatically detects the picture contrast during the picture uploading stage to check whether the picture contrast meets the requirements, greatly saving the time for manual contrast detection and improving work efficiency.
[0078] Exemplary device
[0079] Figure 2 is a schematic structural diagram of a picture contrast recognition and verification device provided by an exemplary embodiment of the present invention. As Figure 2 shown, the device 200 includes:
[0080] A conversion module 210, configured to read the picture to be inspected and convert the picture to be verified into a grayscale image;
[0081] A first calculation module 220, configured to calculate the average brightness value of the grayscale image;
[0082] A second calculation module 230, configured to calculate the square of the difference between each pixel of the grayscale image and the average brightness value;
[0083] A third calculation module 240, configured to calculate the average value of the squares of the differences of each pixel of the grayscale image;
[0084] A determination module 250, configured to determine the contrast value of the picture to be inspected according to the square root of the average value.
[0085] Optionally, the first calculation module 220 includes:
[0086] A first operator sub-module, configured to add the brightness values of each pixel of the grayscale image to obtain a total brightness value;
[0087] A second operator sub-module, configured to divide the total brightness value by the number of pixels of the grayscale image to obtain the average brightness value.
[0088] Optionally, the second calculation module 230 includes:
[0089] A creation sub-module, configured to create a zero matrix or an empty array having the same size as the grayscale image;
[0090] A calculation sub-module, configured to loop through each pixel of the grayscale image, calculate the difference between each pixel and the average brightness value, and save the calculated difference result to the zero matrix or the empty array to generate a difference matrix or a difference array;
[0091] A third operator module, configured to perform element - level squaring operation on the difference matrix or difference array using a square function, to obtain the squared difference of each pixel from the average luminance value.
[0092] Optionally, the third calculation module 240 includes:
[0093] A fourth operator module, configured to sum all elements of the difference - squared array composed of the squared differences of all pixels in the grayscale image, to obtain the total sum of squared differences;
[0094] A fifth operator module, configured to divide the total sum of squared differences by the number of elements in the difference - squared array, to obtain the average value.
[0095] Exemplary electronic device
[0096] Figure 3 is the structure of an electronic device provided by an exemplary embodiment of the present invention. As Figure 3 shown, the electronic device 30 includes one or more processors 31 and a memory 32.
[0097] The processor 31 may be a central processing unit (CPU) or other forms of processing units with data - processing capabilities and / or instruction - execution capabilities, and may control other components in the electronic device to perform desired functions.
[0098] The memory 32 may include one or more computer program products, and the computer program products may include various forms of computer - readable storage media, such as volatile memory and / or non - volatile memory. The volatile memory may include, for example, random access memory (RAM) and / or cache memory, etc. The non - volatile memory may include, for example, read - only memory (ROM), hard disk, flash memory, etc. One or more computer program instructions may be stored on the computer - readable storage media, and the processor 31 may run the program instructions to implement the methods of the software programs of the various embodiments of the present invention described above and / or other desired functions. In one example, the electronic device may further include: an input device 33 and an output device 34, and these components are interconnected through a bus system and / or other forms of connection mechanisms (not shown).
[0099] In addition, the input device 33 may further include, for example, a keyboard, a mouse, etc.
[0100] The output device 34 may output various information to the outside. The output device 34 may include, for example, a display, a speaker, a printer, and a communication network and its connected remote output devices, etc.
[0101] Of course, for simplicity, Figure 3 only some of the components related to the present invention in the electronic device are shown, and components such as buses, input / output interfaces, etc. are omitted. In addition, according to specific application scenarios, the electronic device may further include any other appropriate components.
[0102] Exemplary computer program product and computer-readable storage medium
[0103] In addition to the above methods and devices, an embodiment of the present invention may also be a computer program product, which includes computer program instructions that, when run by a processor, cause the processor to execute the steps in the methods according to various embodiments of the present invention described in the above "Exemplary Method" section of this specification.
[0104] The computer program product may be written in any combination of one or more programming languages for programming code to perform the operations of the embodiments of the present invention. The programming languages include object-oriented programming languages such as Java, C++, etc., and also include conventional procedural programming languages such as the "C" language or similar programming languages. The program code may be executed entirely on the user's computing device, partially on the user's device, executed as an independent software package, partially on the user's computing device and partially on a remote computing device, or entirely on a remote computing device or server.
[0105] In addition, an embodiment of the present invention may also be a computer-readable storage medium, on which computer program instructions are stored, and the computer program instructions, when run by a processor, cause the processor to execute the steps in the method of information mining on historical change records according to various embodiments of the present invention described in the above "Exemplary Method" section of this specification.
[0106] The computer-readable storage medium may adopt any combination of one or more readable media. The readable media may be a readable signal medium or a readable storage medium. The readable storage medium may, for example, include but is not limited to an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination of the above. More specific examples (non-exhaustive list) of the readable storage medium include: an electrical connection with one or more wires, a portable disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above.
[0107] The basic principles of the present invention have been described in conjunction with specific embodiments. However, it should be noted that the advantages, benefits, effects, etc. mentioned in the present invention are only examples and not limitations. It cannot be considered that these advantages, benefits, effects, etc. are essential for each embodiment of the present invention. Additionally, the specific details disclosed above are only for illustrative and easy-to-understand purposes, and not limitations. These details do not limit the present invention to necessarily implementing with the above specific details.
[0108] Each embodiment in this specification is described in a progressive manner. The key point of each embodiment is to illustrate the differences from other embodiments. For the same or similar parts among the embodiments, reference can be made to each other. For system embodiments, since they basically correspond to method embodiments, the description is relatively simple. For relevant parts, reference can be made to the partial description of the method embodiments.
[0109] The block diagrams of the devices, systems, equipment, and systems involved in the present invention are only illustrative examples and do not intend to require or imply that they must be connected, arranged, and configured in the manner shown in the block diagrams. As those skilled in the art will recognize, these devices, systems, equipment, and systems can be connected, arranged, and configured in any way. Words such as "including", "comprising", "having", etc. are open-ended terms, meaning "including but not limited to", and can be used interchangeably with each other. The word "or" and "and" used here refer to the word "and / or", and can be used interchangeably with each other, unless the context clearly indicates otherwise. The word "such as" used here refers to the phrase "such as but not limited to", and can be used interchangeably with each other.
[0110] The methods and systems of the present invention can be implemented in many ways. For example, the methods and systems of the present invention can be implemented through software, hardware, firmware, or any combination of software, hardware, and firmware. The above order of the steps for the methods is only for illustration, and the steps of the methods of the present invention are not limited to the specific order described above, unless otherwise specifically stated in other ways. Additionally, in some embodiments, the present invention can also be implemented as a program recorded in a recording medium, and these programs include machine-readable instructions for implementing the methods according to the present invention. Therefore, the present invention also covers the recording medium storing the programs for executing the methods according to the present invention.
[0111] It should also be noted that in the systems, devices, and methods of the present invention, each component or each step can be decomposed and / or recombined. These decompositions and / or recombinations shall be regarded as equivalent solutions of the present invention. The above description of the disclosed aspects is provided to enable any person skilled in the art to make or use the present invention. Various modifications to these aspects will be readily apparent to those skilled in the art, and the general principles defined herein can be applied to other aspects without departing from the scope of the present invention. Therefore, the present invention is not intended to be limited to the aspects shown herein, but rather to the broadest scope consistent with the principles and novel features disclosed herein.
[0112] The above description has been presented for purposes of illustration and description. Additionally, this description is not intended to limit the embodiments of the present invention to the forms disclosed herein. Although numerous example aspects and embodiments have been discussed above, those skilled in the art will recognize some of their variations, modifications, alterations, additions, and subcombinations.
Claims
1. A method for image contrast recognition and verification, characterized in that: include: Reading a picture to be checked and converting the picture to be checked into a grayscale image; Calculating the average brightness value of the grayscale image; Calculate the square of the difference between each pixel of the grayscale image and the average brightness value; Calculating the average of the squares of the difference values for each pixel of the grayscale image; The contrast value of the image to be tested is determined according to the square root of the average value.
2. The method according to claim 1, characterized in that Calculating the average brightness value of the grayscale image, comprising: Adding the brightness value of each pixel of the grayscale image to obtain a total brightness value; The sum of the brightness values is divided by the number of pixels of the grayscale image to obtain the average brightness value.
3. The method according to claim 1, characterized in that Calculating the square of the difference between each pixel of the grayscale image and the average brightness value, comprising: Create a zero matrix or an empty array of the same size as the grayscale image; Loop through each pixel of the grayscale image, calculate the difference between each pixel and the average brightness value, and save the calculated difference result into the zero matrix or empty array to generate a difference matrix or difference array; A square function is used to perform an element-level square operation on the difference matrix or the difference array to obtain the square of the difference between each pixel and the average brightness value.
4. The method according to claim 1, characterized in that Calculating the average of the squared difference values of each pixel of the grayscale image comprises: Summing all elements of a difference square array composed of difference squares of all pixels in the grayscale image to obtain a sum of difference squares; The sum of the squared differences is divided by the number of elements in the squared difference array to obtain the average value.
5. A device for image contrast recognition and verification, characterized in that: include: A conversion module, used for reading the image to be checked and converting the image to be checked into a grayscale image; A first calculation module, used for calculating the average brightness value of the grayscale image; A second calculation module, used for calculating the square of the difference between each pixel of the grayscale image and the average brightness value; A third calculation module, used for calculating the average value of the square of the difference of each pixel of the grayscale image; The determination module is used to determine the contrast value of the image to be tested according to the square root of the average value.
6. The device according to claim 5, characterized in that The first computing module includes: A first operator module, configured to add the brightness value of each pixel of the grayscale image to obtain a total brightness value; The second operation submodule is used to divide the sum of the brightness values by the number of pixels of the grayscale image to obtain the average brightness value.
7. The device according to claim 5, characterized in that The second computing module includes: A creation submodule is used to create a zero matrix or an empty array with the same size as the grayscale image; A calculation submodule, used for looping through each pixel of the grayscale image, performing difference calculation between each pixel and the average brightness value, and saving the calculated difference result into the zero matrix or empty array to generate a difference matrix or a difference array; The third operator module is used to use a square function to perform an element-level square operation on the difference matrix or the difference array to obtain the square of the difference between each pixel and the average brightness value.
8. The device according to claim 5, characterized in that The third computing module includes: A fourth operator module, configured to sum all elements of a difference square array composed of difference squares of all pixels in the grayscale image to obtain a sum of difference squares; The fifth operator module is used to divide the sum of the squared differences by the number of elements in the squared difference array to obtain the average value.
9. A computer-readable storage medium, characterized in that: The storage medium stores a computer program, and the computer program is used to execute the method according to any one of claims 1 to 4.
10. An electronic device, characterized in that: The electronic device comprises: processor; a memory for storing instructions executable by the processor; The processor is used to read the executable instructions from the memory and execute the instructions to implement the method described in any one of claims 1 to 4.