Image coding method and device, equipment and medium
By dividing the image into different regions and adopting appropriate encoding methods for each region, the problem of limited improvement in image encoding quality in the prior art is solved, more efficient image encoding is achieved, and user experience is optimized.
Patent Information
- Application Number
- CN202411696572.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-25
- Publication Date
- 2025-03-28
AI Technical Summary
The existing image encoding technology ignores the differences in the content and characteristics of the image, which makes it difficult to greatly improve the image encoding quality while ensuring the encoding volume and real-time nature.
By dividing the image into different regions, the encoding method is adopted according to the characteristics of each region. The specific steps include obtaining the image to be encoded and converting it into a grayscale format, dividing it into multiple macroblocks, calculating the variance of the macroblock, and dividing the image areas based on the variance, and finally adopting lossless, lossy or lossless encoding methods for different areas.
On the premise of ensuring image encoding volume and real-time encoding, the image encoding quality is greatly improved, the user experience is optimized, and the encoding volume and encoding and codec quality are taken into account.
Smart Images

Figure CN119728989A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of image coding and decoding technology, and in particular to an image coding method, device, equipment and medium. Background Art
[0002] With the rapid development of audio and video applications, the proportion of video image content on the Internet has increased rapidly, and efficient image encoding and decoding technology has gradually become one of the key technical fields of concern in the industry. The current mainstream image compression methods can be divided into two categories: lossless and lossy: lossless compression is a reversible algorithm, which is often more suitable for non-natural images, and for natural images, the problem of large encoding volume is inevitable. Lossy compression is committed to improving the compression rate, taking advantage of the fact that the human eye is insensitive to certain frequency components, allowing a certain amount of pixel accuracy to be lost during the compression process, and maximizing the storage space of character encoding according to the frequency of use. In addition to traditional methods, deep learning has also been used for image compression in recent years, but it requires large computing resources.
[0003] Current image coding technologies all use a unified coding strategy to process the entire image, ignoring the large differences in the content and characteristics of the image. As the application areas of images and videos continue to expand, the compression coding scenarios for images have become more diverse. Due to the different sensitivities of the human eye to images with different features, part of the content of an image is often suitable for lossless compression, while part is suitable for lossy compression. However, most current compression algorithms only compress images in a single way, and cannot significantly improve the image coding quality while ensuring the image coding volume and coding real-time performance. Summary of the invention
[0004] In view of the above problems, the present invention aims to propose an image coding method, device, equipment and medium, which can divide the image into different areas, so as to more specifically adopt a suitable coding method for encoding, and greatly improve the image coding quality and optimize the user experience while ensuring the image coding volume and coding real-time performance.
[0005] According to a first aspect of the present invention, a method for image encoding is first provided, wherein the method comprises:
[0006] Acquire an image to be encoded, and convert the image to be encoded into a grayscale format;
[0007] Dividing the converted image to be encoded into a plurality of macroblocks;
[0008] Calculating the variance of the macroblock based on pixel values of pixels in the macroblock;
[0009] Dividing the image to be encoded into different regions based on the variance of the macroblock;
[0010] Different encoding modes are used to encode the image in different areas of the image to be encoded.
[0011] Optionally, the image to be encoded is a color image, and the color image includes three channels: red, green and blue;
[0012] The step of converting the image to be encoded into a grayscale format comprises:
[0013] Obtaining the values of the three channels of pixels in the image to be encoded;
[0014] Obtaining the grayscale value of the pixel according to the values of the three channels;
[0015] The grayscale format of the image to be encoded is obtained according to the grayscale value.
[0016] Optionally, dividing the converted image to be encoded into a plurality of macroblocks comprises:
[0017] Determine the size of the macroblock;
[0018] The to-be-encoded image is traversed, and the to-be-encoded image is divided into a plurality of macroblocks according to the size of the macroblock.
[0019] Optionally, calculating the variance of the macroblock according to pixel values of pixels in the macroblock includes:
[0020] Calculating an average pixel value of pixels in the macroblock based on the pixel values of the pixels in the macroblock;
[0021] The variance of the macroblock is calculated based on the average pixel value and the pixel values of the pixels in the macroblock.
[0022] Optionally, dividing the to-be-encoded image into different regions based on the variance of the macroblock includes:
[0023] Comparing the value of the variance of the macroblock with a preset variance threshold, and if the variance of the macroblock is greater than the preset variance threshold, determining that the macroblock is a non-uniform macroblock;
[0024] If the value of the variance of the macroblock is less than the preset variance threshold, determining that the macroblock is a uniform macroblock;
[0025] The image to be encoded is divided into different areas according to the uniform macroblocks and the non-uniform macroblocks.
[0026] Optionally, the dividing the to-be-encoded image into different regions according to the uniform macroblocks and the non-uniform macroblocks further includes:
[0027] Set a fixed-size sliding window;
[0028] The sliding window is used in the image to be encoded, and if the macroblock in the sliding window is a uniform macroblock, then the area in the sliding window is a uniform area;
[0029] If the macroblock in the sliding window is a non-uniform macroblock, then the area in the sliding window is a non-uniform area;
[0030] If the macroblocks in the sliding window are uniform macroblocks and non-uniform macroblocks, the area in the sliding window is an edge area.
[0031] Optionally, the adopting different encoding modes to perform image encoding for different regions of the image to be encoded includes:
[0032] For uniform areas, lossless coding is used for image encoding;
[0033] For non-uniform areas, lossy coding is used for image encoding;
[0034] For edge areas, a lossless coding method or a quasi-lossless coding method is used to encode the image.
[0035] According to a second aspect of the present invention, there is further provided an image encoding device, characterized in that the device comprises:
[0036] An image acquisition module, used for acquiring an image to be encoded and converting the image to be encoded into a grayscale format;
[0037] A macroblock division module, used for dividing the converted image to be encoded into a plurality of macroblocks;
[0038] A calculation module, used for calculating the variance of the macroblock according to the pixel values of the pixels in the macroblock;
[0039] A region division module, used for dividing the image to be encoded into different regions based on the variance of the macroblock;
[0040] The image encoding module is used to encode the image using different encoding methods for different areas of the image to be encoded.
[0041] According to the third aspect of the present invention, there is also provided an electronic device, characterized in that it comprises a processor, a memory and a computer program stored in the memory and capable of running on the processor, wherein the computer program implements the image encoding method as described above when executed by the processor.
[0042] According to a fourth aspect of the present invention, there is further provided a computer-readable storage medium, characterized in that a computer program is stored on the computer-readable storage medium, and when the computer program is executed by a processor, the image encoding method as described above is implemented.
[0043] The image encoding method provided by the embodiment of the present invention obtains the image to be encoded, converts the image to be encoded into a grayscale format, divides the converted image to be encoded into multiple macroblocks, calculates the variance of the macroblock according to the pixel values of the pixels in the macroblock, divides the image to be encoded into different regions based on the variance of the macroblock, and uses different encoding methods to encode the image for different regions of the image to be encoded. The embodiment of the present invention takes into account the encoding volume and encoding and decoding quality of the image, greatly reduces the encoding volume compared with the single use of lossless encoding format, and significantly improves the image encoding and decoding quality compared with the single use of lossy encoding. By analyzing, identifying and dividing the image, the image is divided into different regions, so that a suitable encoding method can be used for encoding in a more targeted manner, and the image encoding quality is greatly improved while ensuring the image encoding volume and encoding real-time performance, and the user experience is optimized.
[0044] The above description is only an overview of the technical solution of the present invention. In order to more clearly understand the technical means of the present invention, it can be implemented according to the contents of the specification. In order to make the above and other purposes, features and advantages of the present invention more obvious and easy to understand, the specific implementation methods of the present invention are listed below. BRIEF DESCRIPTION OF THE DRAWINGS
[0045] In order to make the purpose, technical solution and advantages of the embodiments of the present application clearer, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present application. In the drawings:
[0046] Figure 1 is a flowchart of a method for encoding an image provided by an embodiment of the present invention;
[0047] Figure 2 A web page scrolling scene graph provided by an embodiment of the present invention;
[0048] Figure 3 is a flow chart of an image encoding method provided by an embodiment of the present invention;
[0049] Figure 4 A macroblock annotation diagram of an image to be encoded provided by an embodiment of the present invention;
[0050] Figure 5 is an image coding comparison diagram provided by an embodiment of the present invention;
[0051] Figure 6This is a peak signal-to-noise ratio comparison diagram of different image encoding methods provided by an embodiment of the present invention;
[0052] Figure 7 is a comparison diagram of structural similarity index values of different image encoding methods provided by an embodiment of the present invention;
[0053] Figure 8 is a coding volume comparison diagram of different image coding methods provided by an embodiment of the present invention;
[0054] Fig. 9 It is a structural schematic diagram of an image encoding device provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0055] In order to make the purpose, technical scheme and advantages of the embodiments of the present invention clearer, the embodiments of the present invention will be described in detail below in conjunction with the accompanying drawings. However, it can be understood by those skilled in the art that in the embodiments of the present invention, many technical details are proposed in order to enable readers to better understand the present invention. However, even without these technical details and various changes and modifications based on the following embodiments, the technical scheme claimed in the present invention can also be implemented. The division of the following embodiments is for the convenience of description and should not constitute any limitation on the specific implementation of the present invention. The various embodiments can be combined and referenced with each other without contradiction.
[0056] Image compression methods are divided into two categories: lossless and lossy. Lossless compression is a reversible algorithm, which can restore all pixel information of the image. It is widely used in document compression, medical imaging and other fields. Among them, Huffman coding and Lempel-Ziv-Welch (LZW) are often used in formats such as PNG (Portable Network Graphics). Lossless compression algorithms are often more suitable for non-natural images, and the encoding volume is inevitably large for natural images. Lossy compression is committed to improving the compression rate, taking advantage of the fact that the human eye is insensitive to certain frequency components, and allowing a certain loss of pixel accuracy during the compression process. For example, JPEG (Joint Photographic Experts Group) is the most widely used lossy method. It is based on discrete cosine transform (DCT) and Huffman coding. The former converts time domain signals into frequency domain signals to filter out information that the human eye is more sensitive to; while the latter maximizes the storage space of character encoding based on the frequency of use.
[0057] As the application fields of image and video continue to expand, the compression coding scenarios for images have become more diverse. Figure 2Take the scrolling scene image of the CSDN (Chinese Software Developer Network) web page as an example. The image contains more content. Some boxes contain areas that are closer to natural images (images with people in the area), which are more suitable for lossy compression. In this case, the quality loss is difficult to be detected by the naked eye; while some box areas (areas full of text content) are suitable for lossless compression, which can not only obtain a smaller compression volume or a compression volume close to lossy compression, but also ensure lossless compression quality.
[0058] With the development of cloud technology, image encoding and decoding has been applied to many engineering fields. In recent years, the rapid development of cloud desktop, video conferencing, remote desktop assistance and other applications are inseparable from image encoding and decoding technology. Taking cloud desktop as an example, the original image is transmitted from the server to the client in the form of a code stream after encoding and compression. The client decodes the character information in real time to convert it into image information with semantic content, and finally presents the picture on the display in front of the user. Application scenarios such as cloud desktop not only have extremely high requirements for real-time performance, but also need to ensure the quality of the picture, otherwise it will seriously affect the user experience. Different from traditional image collection compression, the image content in cloud desktop is determined by user behavior. It may be a natural image, an unnatural image, or even a combination of the two.
[0059] In summary, more and more complex scenes require more sophisticated compression methods to achieve better results, and most current compression algorithms only compress (encode) images. If you need to ensure the encoding quality without causing too large an encoding volume, you cannot simply use a certain compression method to compress the image.
[0060] This application can analyze, identify and divide images, and can divide images into different areas, so as to adopt appropriate encoding methods more specifically. Under the premise of ensuring the image encoding volume and encoding real-time performance, it can greatly improve the image encoding quality and optimize the user experience.
[0061] Reference Figure 1 , shows a flowchart of a method for encoding an image provided by an embodiment of the present invention, which may specifically include the following steps:
[0062] Step 101: obtain an image to be encoded, and convert the image to be encoded into a grayscale format.
[0063] The image to be encoded is generally a color image, and each pixel of a color image usually contains three channels: red (R), green (G), and blue (B). The value of each channel usually ranges from 0 to 255, resulting in the need to store three 8-bit integers (24 bits) for each pixel. Grayscale images have only one channel (brightness channel), and each pixel contains only one grayscale value, which usually ranges from 0 to 255. Therefore, each pixel only needs to store one 8-bit integer (8 bits). By converting the image to grayscale format, the amount of data is reduced by two-thirds, reducing the storage and transmission overhead. In addition, when processing color images, the three channels of each pixel need to be calculated separately, which increases the computational complexity. When processing grayscale images, only one grayscale value of each pixel needs to be calculated, which greatly reduces the computational complexity.
[0064] The grayscale value of each pixel can be calculated using a grayscale conversion formula to convert the image to be encoded into a grayscale image.
[0065] Step 102: Divide the converted image to be encoded into a plurality of macroblocks.
[0066] Macroblock is a basic concept in image and video coding technology. It is usually composed of N times N pixels. An image can be divided into multiple macroblocks. By dividing an image into multiple macroblocks, each macroblock can be processed independently (such as calculating variance), thereby improving compression efficiency.
[0067] First determine the size of the macroblock, and then use a double loop to traverse the image and divide the image into multiple macroblocks. The outer loop traverses the rows of the image, and the inner loop encodes the columns of the image. In each loop, the macroblock at the current position is extracted, and the size of the macroblock is determined by the determined macroblock size.
[0068] Step 103: Calculate the variance of the macroblock according to the pixel values of the pixels in the macroblock.
[0069] Variance is a statistic that measures the degree of data dispersion. Calculating the variance of a macroblock can measure the degree of discreteness of the pixel values within the macroblock. The smaller the variance, the more concentrated the data, that is, the smaller the change in pixel values within the macroblock.
[0070] Before calculating the variance of a macroblock, since a macroblock is usually composed of N times N pixels, the two-dimensional pixel information of the macroblock is first converted into one-dimensional pixel information, which can be done by expanding the two-dimensional data into a one-dimensional array by row or by column. Then the variance of each macroblock can be calculated based on the number of pixels in the macroblock, the pixel value corresponding to the position of each pixel in the one-dimensional array, and the average pixel value of all pixels in the macroblock.
[0071] Step 104: Divide the image to be encoded into different regions based on the variance of the macroblock.
[0072] The variance threshold of the macroblock is set according to the specific application requirements and image characteristics, the variance of each macroblock is compared with the variance threshold, and the macroblocks are divided into two different types, namely, macroblocks with smooth pixel changes and macroblocks with relatively drastic pixel fluctuations.
[0073] A sliding window of a fixed size is set according to the resolution of the image. Generally, the sliding window contains at least one macroblock. The type of the area in the sliding window is determined according to whether the types of macroblocks in the sliding window are the same. Then, the image to be encoded can be divided into three different areas according to the type of the area in the sliding window.
[0074] Step 105: For different regions of the image to be encoded, different encoding methods are used to encode the image.
[0075] There are three encoding methods: lossless encoding method, lossy encoding method and quasi-lossless encoding method. Different encoding methods are used to encode images according to different areas of the image.
[0076] Through the above method, the image is divided into different regions by analyzing, identifying and dividing the image, so that the image regions with the same characteristics can be encoded in a more targeted manner using a suitable encoding method.
[0077] In an embodiment of the present invention, an image to be encoded is obtained, the image to be encoded is converted into a grayscale format, the converted image to be encoded is divided into a plurality of macroblocks, the variance of the macroblock is calculated according to the pixel values of the pixels in the macroblock, the image to be encoded is divided into different regions based on the variance of the macroblock, and different encoding methods are used for image encoding for different regions of the image to be encoded. The embodiment of the present invention takes into account both the encoding volume and the encoding and decoding quality of the image, greatly reduces the encoding volume compared to the single use of lossless encoding format, and significantly improves the image encoding and decoding quality compared to the single use of lossy encoding. By analyzing, identifying and dividing the image, the image is divided into different regions, so that a suitable encoding method can be used for encoding in a more targeted manner, and the image encoding quality is greatly improved while ensuring the image encoding volume and encoding real-time performance, and the user experience is optimized.
[0078] In an optional embodiment of the present invention, step 101 further includes the following steps:
[0079] S1011, obtaining values of three channels of pixels in the image to be encoded;
[0080] S1012, obtaining the gray value of the pixel according to the values of the three channels;
[0081] S1013, obtaining a grayscale format of the image to be encoded according to the grayscale value.
[0082] The image to be encoded is generally a color image, and a color image usually contains three channels: red (R), green (G), and blue (B). Get the values of the red (R), green (G), and blue (B) channels of each pixel in the image, that is, get the red component value, green component value, and blue component value of each pixel, according to the red component value, green component value, and blue component value of each pixel.
[0083] According to the red component value, green component value and blue component value of each pixel, the grayscale value of each pixel is calculated by the grayscale conversion formula, and the grayscale image corresponding to the image to be encoded is obtained according to the calculated grayscale value. The grayscale conversion formulas include the average method, the weighted average method, etc.
[0084] The formula of the average method is: gray value = (R+G+B) / 3;
[0085] The weighted average method is based on the sensitivity of the human eye to different colors. The R, G, and B values of each pixel in the color image are weighted averaged according to specific weights to obtain the gray value. Usually the weight of the red channel is 0.299, the weight of the green channel is 0.587, and the weight of the blue channel is 0.114. The formula for the weighted average method is: Gray value = 0.299*R+0.587*G+0.114*B.
[0086] Specifically, suppose there is an image to be encoded, each pixel consists of three values (R, G, B), and the value of a pixel in the image is (128, 64, 32), where:
[0087] R=128, indicating that the red component value of the pixel is 128;
[0088] G=64, indicating that the green component value of the pixel is 64;
[0089] B=32: indicates that the blue component value of the pixel is 32.
[0090] The grayscale value of the pixel is calculated using the average method or the weighted average method, and the grayscale value of each pixel in the image is obtained by analogy. According to the calculated grayscale value, the grayscale image corresponding to the image to be encoded can be obtained.
[0091] By converting the coded image into grayscale format, when processing the grayscale image, only one grayscale value of each pixel needs to be calculated, which greatly reduces the complexity of the calculation.
[0092] In an optional embodiment of the present invention, step 102 further includes the following steps:
[0093] S1021, determining the size of the macroblock;
[0094] S1022, traverse the image to be encoded, and divide the image to be encoded into multiple macroblocks according to the size of the macroblock.
[0095] Specifically, the size of the macroblock is determined to be 16 by 16 pixels. A double loop is used to traverse the image, the outer loop traverses the rows of the image, and the inner loop traverses the columns of the image. In each loop, the macroblock at the current position is extracted, the size of which is 16 by 16 pixels, and the extracted macroblocks are stored in a list, thereby dividing the image into multiple macroblocks.
[0096] In an optional embodiment of the present invention, step 103 further includes the following steps:
[0097] S1031, based on the pixel values of the pixels in the macroblock, calculate the average pixel value of the pixels in the macroblock.
[0098] Before calculation, the two-dimensional pixel information of each macroblock is converted into one dimension, which can simplify the image processing and compression process. Specifically, the pixel matrix of the macroblock is a two-dimensional pixel matrix, which is expanded into a one-dimensional array by row or column.
[0099] Assuming the size of the macroblock is 3 by 3, its two-dimensional pixel matrix is as follows:
[0100]
[0101] The one-dimensional data after expanding by row is: [1 2 3 9 10 11 17 18 19]
[0103] Or it can be expanded into a one-dimensional array by column.
[0104] Assuming there are n pixels in a macroblock, the average pixel value P of the pixels in the macroblock is:
[0105]
[0106] Among them, x i Represents the pixel value of the i-th pixel at the one-dimensional position of the macroblock.
[0107] S1032, calculating the variance of the macroblock based on the average pixel value and the pixel values of the pixels in the macroblock.
[0108] Specifically, the average pixel value of the pixel is P, and the calculation formula of the variance D of the macroblock is as follows:
[0109]
[0110] Where n is the number of pixels in a macroblock, P x is the pixel value of the i-th pixel at the one-dimensional position of the macroblock.
[0111] By calculating the variance of macroblocks, we can better understand and process the details and texture information in the image, thereby improving the efficiency and quality of image processing.
[0112] In an optional embodiment of the present invention, step 104 further includes the following steps:
[0113] S1041, comparing the variance value of the macroblock with a preset variance threshold, if the variance of the macroblock is greater than the preset variance threshold, determining that the macroblock is a non-uniform macroblock;
[0114] S1042, if the value of the variance of the macroblock is less than a preset variance threshold, determine that the macroblock is a uniform macroblock;
[0115] S1043: Divide the image to be encoded into different areas according to the uniform macroblocks and the non-uniform macroblocks.
[0116] The variance threshold of the macroblock is set according to the specific application requirements and image characteristics, the variance of each macroblock is compared with the variance threshold, and the macroblock is divided into two different types of macroblocks using the segmentation function F. Specifically, the value of the segmentation function F can be determined according to the size of the variance of the macroblock and the variance threshold, and then the macroblock is divided into uniform macroblocks (pixel changes are gentle) and non-uniform macroblocks (pixel changes are relatively drastic) according to the F value.
[0117] Specifically, the variance threshold of the macroblock is set to 100, and the variance of each macroblock is compared with the threshold to obtain the segmentation function value, where the segmentation function is as follows:
[0118]
[0119] Among them, a represents the current macroblock and D is the variance of the macroblock.
[0120] When the value of F is 0, the macroblock is a non-uniform macroblock; when the value of F is 1, the macroblock is a uniform macroblock.
[0121] Next, the image to be encoded may be divided into a plurality of different connected regions according to the F values of a plurality of macroblocks divided in the image to be encoded.
[0122] In an optional embodiment of the present invention, S1043 further includes the following sub-steps:
[0123] S1043-1, set a sliding window of fixed size;
[0124] S1043-2, using a sliding window in the image to be encoded, if the macroblock in the sliding window is a uniform macroblock, then the area in the sliding window is a uniform area;
[0125] S1043-3, if the macroblock in the sliding window is a non-uniform macroblock, then the area in the sliding window is a non-uniform area;
[0126] S1043-4: If the macroblocks in the sliding window are uniform macroblocks and non-uniform macroblocks, the area in the sliding window is an edge area.
[0127] A sliding window of a fixed size is set according to the resolution of the image. In the embodiment of the present invention, it is assumed that one sliding window contains two macroblocks.
[0128] When a sliding window is used in an image, the type of region in the sliding window is determined based on the F values of the two macroblocks in the sliding window. If the F values of the two macroblocks in the sliding window are both 0, the region in the sliding window is a non-uniform region; if the F values of the two macroblocks in the sliding window are both 1, the region in the sliding window is a uniform region; if the F values of the two macroblocks in the sliding window are one 0 and the other 1, that is, 0 and 1 appear alternately, the region in the sliding window is an edge region.
[0129] After determining the region type in the sliding window, adjacent small regions with similar characteristics may be merged. For example, if the F values of the macroblocks in two adjacent small regions are both 1, the two adjacent small regions may be merged.
[0130] Reference Figure 4 , shows a macroblock annotation diagram of an image to be encoded provided by an embodiment of the present invention, which is as follows:
[0131] The rose image is encoded (compressed). In the figure, the macroblocks marked with 0 represent macroblocks with an F value of 0, which are non-uniform macroblocks; and the macroblocks marked with 1 represent macroblocks with an F value of 1, which are uniform macroblocks.
[0132] Assume that the sliding window contains two macroblocks, and the sliding window is used to slide the row where the X macroblock in the figure is located. Initially, the F values of the two macroblocks in the sliding window are both 1, indicating a uniform area; then the sliding window slides to the right for the first time, and the F values of the two macroblocks in the sliding window are one 1 and one 0, respectively, so the area in the sliding window is an edge area. Therefore, the X macroblock is located in the edge area, and a lossless or quasi-lossless encoding method needs to be used for it.
[0133] Assume that the sliding window contains two macroblocks, and use the sliding window to slide the upper row of the row where the X macroblock is located in the figure. Initially, the F values of the two macroblocks in the sliding window are both 1, which is a uniform area; then the sliding window slides to the right for the first time by a distance of one macroblock, and at this time the F values of the two macroblocks in the sliding window are both 1, which is a uniform area. Until sliding to the right for the fourth time, at this time, the F values of the two macroblocks in the sliding window are one 1 and the other 0, which is an edge area. At this time, the first few areas are adjacent and the F values of the macroblocks in the area are all 1, so the first few areas can be merged, that is, the first four macroblocks from left to right in the upper row where the X macroblock is located are merged into one area, which is a uniform area. By analogy, the rose image is divided into uniform area, non-uniform area and edge area.
[0134] Different from using a unified encoding method for a picture, this application divides the image into multiple different connected areas and uses appropriate encoding methods to encode different areas in a targeted manner, which not only ensures the image encoding volume and encoding real-time performance, but also greatly improves the image encoding quality.
[0135] In an optional embodiment of the present invention, step 105 further includes the following sub-steps:
[0136] S1051, for the uniform area, adopting a lossless coding method to encode the image;
[0137] S1052, for the non-uniform area, adopt a lossy encoding method to encode the image;
[0138] S1053: For the edge area, use a lossless coding method or a quasi-lossless coding method to encode the image.
[0139] Specifically, for uniform areas (the F values of all macroblocks in the area are 1), a lossless encoding method is used; for non-uniform areas (the F values of all macroblocks in the area are 0), a lossy encoding method is used; for edge areas, that is, areas where the F values of macroblocks in the area are 0 and 1 alternately appear, a high bit depth and high-fidelity encoding strategy, that is, a lossless or quasi-lossless encoding method is used.
[0140] Reference Figure 5 , shows an image coding comparison diagram provided by an embodiment of the present invention, which is as follows:
[0141] In the figure, A is the image compressed by the encoding method of the present application, and B is the image compressed by the JPEG encoding method. A1 and B1 are the decoding results of encoding and decoding the macroblocks at the same position by using A and B respectively. It can be seen that there is obvious lossy pixel diffusion at the border between the flower edge and the background of B1, while the image quality of A1 is obviously much higher.
[0142] Taking actual data as an example, refer to Figure 6 , shows a comparison diagram of peak signal-to-noise ratio values of different image encoding methods provided by an embodiment of the present invention, as follows:
[0143] Peak Signal-to-Noise Ratio (PSNR) is an indicator used to evaluate the quality of an image or video. It is usually used to measure the difference between the original image and the compressed or processed image. The higher the PSNR value, the better the image quality and the less distortion. The PSNR value range is 0 to infinity.
[0144] As can be seen from the figure, the image quality using the encoding method of the present application is higher than that of JPEG lossy encoding.
[0145] Reference Figure 7 , shows a comparison diagram of structural similarity index values of different image encoding methods provided by an embodiment of the present invention, as follows:
[0146] Structural Similarity Index (SSIM) is an indicator used to evaluate the quality of images or videos. It is usually used to measure the similarity between the original image and the compressed or processed image. The higher the SSIM value, the better the image quality and the less distortion. The SSIM value range is 0 to 1.
[0147] As can be seen from the figure, the image quality using the encoding method of the present application is higher than that of JPEG lossy encoding.
[0148] Reference Figure 8 , shows a comparison diagram of coding volumes of different image coding methods provided by an embodiment of the present invention, as follows:
[0149] As can be seen from the figure, the encoding volume using the encoding method of the present application is much smaller than that of PNG lossless encoding.
[0150] Therefore, the present application divides the image into multiple different connected areas and uses appropriate encoding methods to encode different areas in a targeted manner, which not only ensures the image encoding volume and encoding real-time performance, but also greatly improves the image encoding quality.
[0151] Reference Figure 3 , shows a flow chart of an image encoding method provided by an embodiment of the present invention, which is as follows:
[0152] Image preprocessing: Convert the image to be encoded into grayscale format to reduce complexity, then divide the image into multiple macroblocks, using the macroblock as the basic unit, and convert the two-dimensional pixel information of the macroblock into a one-dimensional array.
[0153] Image segmentation: Take macroblock as the basic unit, calculate the variance of macroblock, compare the variance of macroblock with the preset variance threshold, get the segmentation function value of macroblock, and mark the macroblock into two parts.
[0154] Image area detection: Set a fixed-size sliding window for the image according to the image resolution, and determine whether the segmentation function values of the macroblocks in the sliding window are the same.
[0155] Adaptive coding: Determine the region type in the sliding window based on the F value of the macroblocks in the sliding window. If the F values of the macroblocks in the sliding window are all 0, the region in the sliding window is a non-uniform region; if the F values of the macroblocks in the sliding window are all 1, the region in the sliding window is a uniform region; if the F values of the macroblocks in the sliding window are 0 and 1 alternately, the region in the sliding window is an edge region. For uniform regions (the F values of the macroblocks in the region are all 1), a lossless coding method is used; for non-uniform regions (the F values of the macroblocks in the region are all 0), a lossy coding method is used; for edge regions, that is, the F values of the macroblocks in the region are 0 and 1 alternately, a high bit depth and high fidelity coding strategy is used, that is, a lossless or quasi-lossless coding method.
[0156] Reference Fig. 9 , shows a schematic diagram of the structure of an image encoding device provided by an embodiment of the present invention, the device comprising:
[0157] The image acquisition module 201 is used to acquire the image to be encoded and convert the image to be encoded into a grayscale format;
[0158] A macroblock division module 202, configured to divide the converted image to be encoded into a plurality of macroblocks;
[0159] A calculation module 203, configured to calculate the variance of the macroblock according to the pixel values of the pixels in the macroblock;
[0160] A region division module 204, configured to divide the image to be encoded into different regions based on the variance of the macroblock;
[0161] The image encoding module 205 is used to encode the image using different encoding methods for different regions of the image to be encoded.
[0162] In an optional embodiment of the present invention, the image acquisition module 201 includes:
[0163] A channel value acquisition module, used to acquire the values of the three channels of the pixels in the image to be encoded;
[0164] A gray value calculation module, used for obtaining the gray value of the pixel according to the values of the three channels;
[0165] A grayscale format conversion module is used to obtain the grayscale format of the image to be encoded according to the grayscale value.
[0166] In an optional embodiment of the present invention, the macroblock division module 202 includes:
[0167] A macroblock size determination module, used to determine the size of the macroblock;
[0168] The traversal module is used to traverse the image to be encoded and divide the image to be encoded into multiple macroblocks according to the size of the macroblock.
[0169] In an optional embodiment of the present invention, the calculation module 203 includes:
[0170] An average pixel value calculation module, used for calculating an average pixel value of pixels in the macroblock based on the pixel values of the pixels in the macroblock;
[0171] The variance calculation module is used to calculate the variance of the macroblock based on the average pixel value and the pixel values of the pixels in the macroblock.
[0172] In an optional embodiment of the present invention, the region division module 204 includes:
[0173] A first comparison module, used for comparing the variance value of the macroblock with a preset variance threshold, and if the variance of the macroblock is greater than the preset variance threshold, determining that the macroblock is a non-uniform macroblock;
[0174] A second comparison module, configured to determine that the macroblock is a uniform macroblock if the value of the variance of the macroblock is less than the preset variance threshold;
[0175] The region determination module is used to divide the image to be encoded into different regions according to the uniform macroblocks and the non-uniform macroblocks.
[0176] In an optional embodiment of the present invention, the region determination module includes:
[0177] Sliding window setting module, used to set a sliding window of fixed size;
[0178] A uniform region determination module, configured to use the sliding window in the image to be encoded, and if a macroblock in the sliding window is a uniform macroblock, then the region in the sliding window is a uniform region;
[0179] A non-uniform region determination module, configured to determine that if a macroblock in the sliding window is a non-uniform macroblock, then the region in the sliding window is a non-uniform region;
[0180] The edge area determination module is used to determine that if the macroblocks in the sliding window are uniform macroblocks and non-uniform macroblocks, then the area in the sliding window is an edge area.
[0181] In an optional embodiment of the present invention, the image encoding module 205 includes:
[0182] A first encoding module is used to encode the image in a lossless encoding manner for a uniform area;
[0183] The second encoding module is used to encode the image in a lossy encoding manner for the non-uniform area;
[0184] The third encoding module is used to perform image encoding in the edge area by adopting a lossless encoding method or a quasi-lossless encoding method.
[0185] In an embodiment of the present invention, an image to be encoded is obtained, the image to be encoded is converted into a grayscale format, the converted image to be encoded is divided into a plurality of macroblocks, the variance of the macroblock is calculated according to the pixel values of the pixels in the macroblock, the image to be encoded is divided into different regions based on the variance of the macroblock, and different encoding methods are used for image encoding for different regions of the image to be encoded. The embodiment of the present invention takes into account both the encoding volume and the encoding and decoding quality of the image, greatly reduces the encoding volume compared to the single use of lossless encoding format, and significantly improves the image encoding and decoding quality compared to the single use of lossy encoding. By analyzing, identifying and dividing the image, the image is divided into different regions, so that a suitable encoding method can be used for encoding in a more targeted manner, and the image encoding quality is greatly improved while ensuring the image encoding volume and encoding real-time performance, and the user experience is optimized.
[0186] An embodiment of the present invention further provides an electronic device, including a processor, a memory, and a computer program stored in the memory and capable of running on the processor, wherein the computer program implements the above-mentioned image encoding method when executed by the processor.
[0187] The memory may include a random access memory (RAM) or a non-volatile memory, such as at least one disk memory. Optionally, the memory may also be at least one storage device located away from the aforementioned processor.
[0188] The processor can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it can also be a digital signal processor (DSP), an application specific integrated circuit (ASIC), a field programmable gate array (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components.
[0189] An embodiment of the present invention further provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the image encoding method described above is implemented.
[0190] As for the device embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment.
[0191] In the above embodiments, it can be implemented in whole or in part by software, hardware, firmware or any combination thereof. When implemented by software, it can be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the process or function described in the embodiment of the present invention is generated in whole or in part. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions can be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium. For example, the computer instructions can be transmitted from a website site, computer, server or data center to another website site, computer, server or data center by wired (e.g., coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.). The computer-readable storage medium can be any available medium that a computer can access or a data storage device such as a server or data center that includes one or more available media integrated. The available medium can be a magnetic medium (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a DVD), or a semiconductor medium (e.g., a solid-state hard disk Solid State Disk (SSD)), etc.
[0192] It should be noted that, in this article, relational terms such as first and second, etc. 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.
[0193] Each embodiment in this specification is described in a related manner, and the same or similar parts between the embodiments can be referred to each other, and each embodiment focuses on the differences from other embodiments. In particular, for the system embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment.
[0194] The above description is only a preferred embodiment of the present invention and is not intended to limit the protection scope of the present invention. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention are included in the protection scope of the present invention.
Claims
1. An image coding method, characterized in that: The method comprises: Acquire an image to be encoded, and convert the image to be encoded into a grayscale format; Dividing the converted image to be encoded into a plurality of macroblocks; Calculating the variance of the macroblock based on pixel values of pixels in the macroblock; Dividing the image to be encoded into different regions based on the variance of the macroblock; Different encoding modes are used to encode the image in different areas of the image to be encoded.
2. The method according to claim 1, characterized in that The image to be encoded is a color image, and the color image includes three channels: red, green and blue; The step of converting the image to be encoded into a grayscale format comprises: Obtaining the values of the three channels of pixels in the image to be encoded; Obtaining the grayscale value of the pixel according to the values of the three channels; The grayscale format of the image to be encoded is obtained according to the grayscale value.
3. The method according to claim 1, characterized in that The step of dividing the converted image to be encoded into a plurality of macroblocks comprises: Determine the size of the macroblock; The to-be-encoded image is traversed, and the to-be-encoded image is divided into a plurality of macroblocks according to the size of the macroblock.
4. The method according to claim 1, characterized in that: The calculating the variance of the macroblock according to the pixel values of the pixels in the macroblock comprises: Calculating an average pixel value of pixels in the macroblock based on the pixel values of the pixels in the macroblock; The variance of the macroblock is calculated based on the average pixel value and the pixel values of the pixels in the macroblock.
5. The method according to claim 1, characterized in that The step of dividing the image to be encoded into different regions based on the variance of the macroblock includes: Comparing the value of the variance of the macroblock with a preset variance threshold, and if the variance of the macroblock is greater than the preset variance threshold, determining that the macroblock is a non-uniform macroblock; If the value of the variance of the macroblock is less than the preset variance threshold, determining that the macroblock is a uniform macroblock; The image to be encoded is divided into different areas according to the uniform macroblocks and the non-uniform macroblocks.
6. The method according to claim 5, characterized in that The step of dividing the image to be encoded into different regions according to the uniform macroblocks and the non-uniform macroblocks further comprises: Set a fixed-size sliding window; The sliding window is used in the image to be encoded, and if the macroblock in the sliding window is a uniform macroblock, then the area in the sliding window is a uniform area; If the macroblock in the sliding window is a non-uniform macroblock, then the area in the sliding window is a non-uniform area; If the macroblocks in the sliding window are uniform macroblocks and non-uniform macroblocks, the area in the sliding window is an edge area.
7. The method according to claim 1 or 6, characterized in that: The adopting different encoding modes to encode the image for different regions of the image to be encoded includes: For uniform areas, lossless coding is used for image encoding; For non-uniform areas, lossy coding is used for image encoding; For edge areas, a lossless coding method or a quasi-lossless coding method is used to encode the image.
8. An image encoding device, characterized in that The device comprises: An image acquisition module, used for acquiring an image to be encoded and converting the image to be encoded into a grayscale format; A macroblock division module, used for dividing the converted image to be encoded into a plurality of macroblocks; A calculation module, used for calculating the variance of the macroblock according to the pixel values of the pixels in the macroblock; A region division module, used for dividing the image to be encoded into different regions based on the variance of the macroblock; The image encoding module is used to encode the image using different encoding methods for different areas of the image to be encoded.
9. An electronic device, characterized in that: The invention comprises a processor, a memory and a computer program stored in the memory and capable of running on the processor, wherein the computer program implements the image encoding method according to any one of claims 1 to 7 when executed by the processor.
10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the image encoding method according to any one of claims 1 to 7 is implemented.