An image processing method and apparatus
By obtaining the edge distance of image pixels and determining the opacity, and combining it with color values to generate a rotated image, the problem of jagged edges after image rotation is solved, thus improving the visual effect.
Patent Information
- Application Number
- CN202210589517.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-05-26
- Publication Date
- 2025-10-28
- Estimated Expiration
- 2042-05-26
AI Technical Summary
In existing technologies, during image rotation, jagged edges are prone to appear on the edges of the rotated image, affecting the visual experience.
By obtaining the edge distance of image pixels, the opacity is determined based on the edge distance, and a rotated image is generated by combining the color values of the pixels and the background color values. Gradient changes in opacity and transparency are used to reduce jagged edges.
It effectively avoids jagged edges on rotated images, improving the visual effect of the images.
Smart Images

Figure CN117173026B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of image processing technology, and in particular to an image processing method and apparatus. Background Technology
[0002] Image processing often requires rotating images. For example, rotating an image 30° clockwise.
[0003] In existing technologies, the basic principle of image rotation transformation is to map the color values of each pixel in the original image to the color values of each pixel in the rotated image. However, since each pixel in the image is a rectangular pixel, and the floating-point pixels on the diagonal line generated by rotation are mapped to integer pixels in the original image, the rotation transformation method in existing technologies introduces error values, resulting in a jagged appearance in the rotated image. For example, when the original image is rotated by 30°, the edge of the image will form a diagonal line with an angle of 30° to the horizontal. Theoretically, only a portion of the pixels along this diagonal line are located within the area of the rotated image and are floating-point pixels. However, in existing image rotation methods, the pixels along this diagonal line are mapped as a whole to the color values of the corresponding pixels in the original image. Therefore, some of the pixels along this diagonal line will extend beyond the area of the rotated image, resulting in a jagged appearance at the edges of the rotated image. Summary of the Invention
[0004] In view of this, embodiments of the present invention provide an image processing method and apparatus for avoiding jagged edges on rotated images.
[0005] To achieve the above objectives, the embodiments of the present invention provide the following technical solutions:
[0006] In a first aspect, embodiments of the present invention provide an image processing method, comprising:
[0007] Obtain the original image;
[0008] The original image is rotated to obtain the target image;
[0009] Obtain the edge distance of at least one pixel in the target image, wherein the edge distance of any pixel is the distance from that pixel to the edge of the target image;
[0010] The opacity of the at least one pixel is determined based on the edge distance of the at least one pixel; the opacity of any pixel is positively correlated with the edge distance of that pixel;
[0011] A rotated image corresponding to the original image is generated based on the color values of pixels in the target image, the opacity of at least one pixel, and the color values of pixels in the image background corresponding to the at least one pixel.
[0012] As an optional implementation of this invention, the step of rotating the original image to obtain the target image includes:
[0013] The target region is determined based on the rotation angle and the size of the original image, and the target region is the image region corresponding to the target image.
[0014] Obtain the original pixel points corresponding to each pixel point in the target area, wherein the original pixel point corresponding to any pixel point in the target area is the pixel point corresponding to that pixel point in the original image;
[0015] Calculate the color value of each pixel in the target area based on the color value of the original pixel corresponding to each pixel in the target area;
[0016] The target image is generated based on the color values of each pixel within the target area.
[0017] As an optional implementation of this invention, obtaining the original pixel points corresponding to each pixel point within the target area includes:
[0018] The original coordinate values corresponding to each pixel in the target area are calculated by the reverse wrapping algorithm. The original coordinate value corresponding to any pixel in the target area is the coordinate value of that pixel in the original image.
[0019] The pixels within a preset range of the original coordinate values corresponding to each pixel in the target area are determined as the original pixels corresponding to each pixel in the target area.
[0020] As an optional implementation of this invention, the step of calculating the color value of each pixel in the target area based on the color value of the original pixel corresponding to each pixel in the target area includes:
[0021] The color value of each pixel in the target area is calculated based on the bilinear sampling algorithm and the original pixel color value corresponding to each pixel in the target area.
[0022] As an optional implementation of this invention, obtaining the edge distance of at least one pixel in the target image includes:
[0023] Obtain the distance from the at least one pixel to each edge of the target image;
[0024] The minimum value among the distances from the at least one pixel to each edge of the target image is determined as the edge distance of the at least one pixel.
[0025] As an optional implementation of this invention, determining the opacity of the at least one pixel based on the edge distance of the at least one pixel includes:
[0026] Determine whether the edge distance of the first pixel in the at least one pixel is less than a threshold distance;
[0027] If the edge distance of the first pixel is greater than or equal to the threshold distance, then the opacity of the first pixel is set to a preset opacity.
[0028] If the edge distance of the first pixel is less than the threshold distance, then the opacity of the first pixel is set to the ratio of the edge distance to the threshold distance.
[0029] As an optional implementation of this invention, determining the opacity of the at least one pixel based on the edge distance of the at least one pixel includes:
[0030] The length and width of the target image are normalized.
[0031] Determine whether the first coordinate value of the second pixel in the at least one pixel is less than a preset threshold;
[0032] If the first coordinate value of the second pixel is less than the preset threshold, then the opacity of the second pixel is set to the ratio of the first coordinate value to the preset threshold.
[0033] If the first coordinate value of the second pixel is greater than or equal to the preset threshold, then it is determined whether the first calculated value is less than the preset threshold. The first calculated value is the difference between 1 and the first coordinate value.
[0034] If the first calculated value is less than the preset threshold, then the opacity of the second pixel is set to the ratio of the first calculated value to the preset threshold.
[0035] If the first calculated value is greater than or equal to the preset threshold, then determine whether the second coordinate value of the second pixel is less than the preset threshold;
[0036] If the second coordinate value of the second pixel is less than the preset threshold, then the opacity of the second pixel is set to the ratio of the second coordinate value to the preset threshold.
[0037] If the second coordinate value of the second pixel is greater than or equal to the preset threshold, then it is determined whether the second calculated value is less than the preset threshold. The second calculated value is the difference between 1 and the second coordinate value of the second pixel.
[0038] If the second calculated value is less than the preset threshold, then the opacity of the second pixel is set to the ratio of the second calculated value to the preset threshold;
[0039] If the second calculated value is greater than or equal to the preset threshold, then the opacity of the second pixel is set to the preset opacity.
[0040] As an optional implementation of this invention, generating a rotated image corresponding to the original image based on the color values of pixels in the target image, the opacity of at least one pixel, and the color values of pixels in the image background corresponding to the at least one pixel includes:
[0041] Calculate the foreground color value of the pixel in the rotated image corresponding to the at least one pixel based on the color value of the pixel in the target image corresponding to the at least one pixel and the opacity of the at least one pixel;
[0042] Calculate the background color value of the pixel in the rotated image corresponding to the at least one pixel based on the color value of the pixel in the image background corresponding to the at least one pixel and the transparency of the at least one pixel;
[0043] Based on the foreground color value and background color value of the pixel in the rotated image corresponding to the at least one pixel, obtain the color value of the pixel in the rotated image corresponding to the at least one pixel;
[0044] The rotating image is generated based on the color value of the pixel in the rotating image corresponding to the at least one pixel.
[0045] As an optional implementation of the present invention,
[0046] The foreground color value of the pixel in the rotated image corresponding to the third pixel in at least one pixel is the product of the color value of the pixel in the target image corresponding to the third pixel and the opacity of the third pixel.
[0047] The background color value of the pixel in the rotated image corresponding to the third pixel is the product of the color value of the pixel in the background of the image corresponding to the third pixel and the transparency of the third pixel.
[0048] In a second aspect, embodiments of the present invention provide an image processing apparatus, comprising:
[0049] The acquisition unit is used to acquire the original image;
[0050] A rotation unit is used to rotate the original image to obtain the target image;
[0051] The processing unit is configured to obtain the edge distance of at least one pixel in the target image, wherein the edge distance of any pixel is the distance from the pixel to the edge of the target image;
[0052] A determining unit is configured to determine the opacity of the at least one pixel based on the edge distance of the at least one pixel; the opacity of any pixel is positively correlated with the edge distance of that pixel.
[0053] The generation unit is configured to generate a rotated image corresponding to the original image based on the color values of pixels in the target image, the opacity of at least one pixel, and the color values of pixels in the image background corresponding to the at least one pixel.
[0054] As an optional implementation of this invention, the rotation unit is specifically used to determine a target region based on the rotation angle and the size of the original image, wherein the target region is an image region corresponding to the target image; obtain the original pixel points corresponding to each pixel point in the target region, wherein the original pixel point corresponding to any pixel point in the target region is the pixel point corresponding to that pixel point in the original image; calculate the color value of each pixel point in the target region based on the color value of the original pixel points corresponding to each pixel point in the target region; and generate the target image based on the color values of each pixel point in the target region.
[0055] As an optional implementation of the present invention, the rotation unit is specifically used to calculate the original coordinate values corresponding to each pixel in the target area through a reverse wrapping algorithm, wherein the original coordinate value corresponding to any pixel in the target area is the coordinate value corresponding to that pixel in the original image; and to determine the pixels within a preset range of the original coordinate values corresponding to each pixel in the target area as the original pixels corresponding to each pixel in the target area.
[0056] As an optional implementation of the present invention, the rotation unit is specifically used to calculate the color value of each pixel in the target area based on the bilinear sampling algorithm and the color value of the original pixel corresponding to each pixel in the target area.
[0057] As an optional implementation of this invention, the processing unit is specifically used to obtain the distance from the at least one pixel to each edge of the target image;
[0058] The minimum value among the distances from the at least one pixel to each edge of the target image is determined as the edge distance of the at least one pixel.
[0059] As an optional implementation of the present invention, the determining unit is specifically used to determine whether the edge distance of the first pixel among the at least one pixel is less than a threshold distance; if the edge distance of the first pixel is greater than or equal to the threshold distance, the opacity of the first pixel is set to a preset opacity; if the edge distance of the first pixel is less than the threshold distance, the opacity of the first pixel is set to the ratio of the edge distance to the threshold distance.
[0060] As an optional implementation of this invention, the determining unit is specifically used to normalize the length and width of the target image; determine whether the first coordinate value of the second pixel in the at least one pixel is less than a preset threshold; if the first coordinate value of the second pixel is less than the preset threshold, then set the opacity of the second pixel to the ratio of the first coordinate value to the preset threshold; if the first coordinate value of the second pixel is greater than or equal to the preset threshold, then determine whether the first calculated value is less than the preset threshold, wherein the first calculated value is the difference between 1 and the first coordinate value; if the first calculated value is less than the preset threshold, then set the opacity of the second pixel to the ratio of the first calculated value to the preset threshold ... If the second coordinate value of the second pixel is greater than or equal to the preset threshold, then it is determined whether the second coordinate value of the second pixel is less than the preset threshold; if the second coordinate value of the second pixel is less than the preset threshold, then the opacity of the second pixel is set to the ratio of the second coordinate value to the preset threshold; if the second coordinate value of the second pixel is greater than or equal to the preset threshold, then it is determined whether the second calculated value is less than the preset threshold, the second calculated value being the difference between 1 and the second coordinate value of the second pixel; if the second calculated value is less than the preset threshold, then the opacity of the second pixel is set to the ratio of the second calculated value to the preset threshold; if the second calculated value is greater than or equal to the preset threshold, then the opacity of the second pixel is set to a preset opacity.
[0061] As an optional implementation of this invention, the generation unit is specifically configured to: calculate the foreground color value of a pixel in the rotated image corresponding to the at least one pixel based on the color value of a pixel in the target image corresponding to the at least one pixel and the opacity of the at least one pixel; calculate the background color value of a pixel in the rotated image corresponding to the at least one pixel based on the color value of a pixel in the image background corresponding to the at least one pixel and the opacity of the at least one pixel; obtain the color value of a pixel in the rotated image corresponding to the at least one pixel based on the foreground color value and the background color value of the pixel in the rotated image corresponding to the at least one pixel; and generate the rotated image based on the color value of the pixel in the rotated image corresponding to the at least one pixel.
[0062] As an optional implementation of the present invention,
[0063] The foreground color value of the pixel in the rotated image corresponding to the third pixel in at least one pixel is the product of the color value of the pixel in the target image corresponding to the third pixel and the opacity of the third pixel.
[0064] The background color value of the pixel in the rotated image corresponding to the third pixel is the product of the color value of the pixel in the background of the image corresponding to the third pixel and the transparency of the third pixel.
[0065] Thirdly, embodiments of the present invention provide an electronic device, including: a memory and a processor, wherein the memory is used to store a computer program; and the processor is used to cause the electronic device to implement the image processing method described in any of the above embodiments when executing the computer program.
[0066] Fourthly, embodiments of the present invention provide a computer-readable storage medium that, when executed by a computing device, causes the computing device to implement the image processing method described in any of the above embodiments.
[0067] Fifthly, embodiments of the present invention provide a computer program product that, when run on a computer, enables the computer to implement the image processing method described in any of the above embodiments.
[0068] The image processing method provided in this embodiment of the invention first acquires an original image, then rotates the original image to obtain a target image. Next, it obtains the distance from at least one pixel in the target image to the edge of the target image as the edge distance of the at least one pixel. Then, it determines the opacity of the at least one pixel based on the edge distance. Finally, it generates a rotated image corresponding to the original image based on the color value of the pixel in the target image, the opacity of the at least one pixel, and the color value of the pixel in the image background corresponding to the at least one pixel. Since the image processing method provided in this embodiment of the invention further determines the opacity of the at least one pixel based on the edge distance of the at least one pixel in the target image after rotating the original image to obtain the target image, and the opacity of any pixel in the at least one pixel is positively correlated with its edge distance, the image processing method provided in this embodiment of the invention can also increase the opacity of pixels closer to the edge in the rotated image, thereby avoiding jagged edges in the rotated image and improving the visual effect of the rotated image. Attached Figure Description
[0069] The accompanying drawings, which are incorporated in and form part of this specification, illustrate embodiments consistent with the invention and, together with the description, serve to explain the principles of the invention.
[0070] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the accompanying drawings that need to be called in the description of the embodiments or the prior art will be briefly introduced below. Obviously, for those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0071] Figure 1 A flowchart illustrating the steps of an image processing method provided in an embodiment of the present invention;
[0072] Figure 2 A schematic diagram of the target area provided in an embodiment of the present invention;
[0073] Figure 3 A schematic diagram of the original pixels corresponding to the pixels in the target area provided in an embodiment of the present invention;
[0074] Figure 4 This is a schematic diagram of the edge distance of pixels in a target image provided in an embodiment of the present invention;
[0075] Figure 5 This is a schematic diagram of the structure of an image processing apparatus provided in an embodiment of the present invention;
[0076] Figure 6This is a schematic diagram of the hardware structure of an electronic device provided in an embodiment of the present invention. Detailed Implementation
[0077] To better understand the above-mentioned objectives, features, and advantages of the present invention, the solutions of the present invention will be further described below. It should be noted that, unless otherwise specified, the embodiments of the present invention and the features thereof can be combined with each other.
[0078] Many specific details are set forth in the following description in order to provide a full understanding of the invention, but the invention may also be practiced in other ways different from those described herein; obviously, the embodiments in the specification are only some embodiments of the invention, and not all embodiments.
[0079] In embodiments of the present invention, the terms "exemplary" or "for example" are used to indicate that something is an example, illustration, or description. Any embodiment or design described as "exemplary" or "for example" in embodiments of the present invention should not be construed as being more preferred or advantageous than other embodiments or designs. Specifically, the use of the terms "exemplary" or "for example" is intended to present the relevant concepts in a specific manner. Furthermore, in the description of embodiments of the present invention, unless otherwise stated, "a plurality of" means two or more.
[0080] This invention provides an image processing method, referring to... Figure 1 As shown, the image processing method includes the following steps S11 to S15:
[0081] S11. Obtain the original image.
[0082] The original image in this embodiment of the invention can be any type of image. For example, the original image can be a binary image, a grayscale image, an RGB color image, a depth image, etc., and this embodiment of the invention does not limit this.
[0083] S12. Rotate the original image to obtain the target image.
[0084] As an optional implementation of this invention, the above step S12 (rotating the original image to obtain the target image) can be implemented in the following ways:
[0085] The target image is obtained by performing rotation mapping operations or matrix operations on each pixel in the original image one by one.
[0086] As an optional implementation of this invention, the above-mentioned step S12 (rotating the original image to obtain the target image) may include the following steps a to d:
[0087] Step a: Determine the target area based on the rotation angle and the size of the original image.
[0088] The target region is the image region corresponding to the target image.
[0089] For example, refer to Figure 2 As shown, the original image 21 has a length of H, a width of W, and a rotation angle of 30°. Therefore, the image region corresponding to the target image can be determined as the target region 22 based on the size and rotation angle of the original image.
[0090] Step b: Obtain the original pixel points corresponding to each pixel point in the target area.
[0091] Wherein, the original pixel corresponding to any pixel within the target area is the pixel corresponding to that pixel in the original image.
[0092] It should be noted that the original pixel corresponding to the pixel in the target area can be a single pixel or multiple pixels, and this embodiment of the invention does not limit this.
[0093] As an optional implementation of this invention, step b (obtaining the original pixel points corresponding to each pixel point in the target area) can be implemented in the following ways: Step 1 and Step 2:
[0094] Step 1: Calculate the original coordinate values of each pixel in the target area using the Reverse-Warp algorithm.
[0095] Wherein, the original coordinate value corresponding to any pixel point within the target area is the coordinate value corresponding to that pixel point in the original image.
[0096] Step 2: Determine the pixels within a preset range of the original coordinate values corresponding to each pixel in the target area as the original pixels corresponding to each pixel in the target area.
[0097] For example, the area occupied by a certain number of pixels can be preset within the preset range, such as the area occupied by 2*2 pixels, or the area occupied by 4*4 pixels.
[0098] For example, refer to Figure 3 As shown, Figure 3Taking a region occupied by 2*2 pixels as an example, the original coordinates of the pixel (x1, y1) in the target region 22 are calculated by the reverse wrapping algorithm. The pixels within the preset range of the original coordinates (x2, y2) include pixel A, pixel B, pixel C and pixel D. Therefore, the original pixels corresponding to the pixel (x1, y1) in the target region are determined to be pixel A, pixel B, pixel C and pixel D.
[0099] Step c: Calculate the color value of each pixel in the target area based on the color value of the original pixel corresponding to each pixel in the target area.
[0100] As an optional implementation of this invention, step c (calculating the color value of each pixel in the target area based on the color value of the original pixel corresponding to each pixel in the target area) includes:
[0101] The color value of each pixel in the target area is calculated based on the bilinear sampling algorithm and the original pixel color value corresponding to each pixel in the target area.
[0102] Step d: Generate the target image based on the color values of each pixel within the target area.
[0103] The commonly used image rotation mapping method in the prior art is to rotate and map each pixel in the original image to generate a rotated image. However, if a pixel in the original image is not mapped to a pixel in the rotated image, holes will appear in the rotated image. The above embodiment determines the original pixel corresponding to each pixel in the target area one by one, and calculates the color value of each pixel in the target area using the original pixel corresponding to the target pixel. Therefore, compared with the prior art, the above embodiment can ensure that each pixel in the target area has a corresponding original pixel, thereby avoiding holes in the target image.
[0104] S13. Obtain the edge distance of at least one pixel in the target image.
[0105] Wherein, the edge distance of any pixel is the distance from that pixel to the edge of the target image.
[0106] It should be noted that when obtaining the edge distance of at least one pixel in the target image, it can be to obtain the edge distance of every pixel in the target image, or to manually or automatically delineate an edge region near the edge on the target image, and only obtain the edge distance of the pixels located within the edge region.
[0107] As an optional implementation of this invention, step S13 (obtaining the edge distance of pixels in the target image) includes:
[0108] Obtain the distance from each pixel in the target image to each edge of the target image;
[0109] The minimum value among the distances from a pixel in the target image to each edge of the target image is determined as the edge distance of the pixel in the target image.
[0110] That is, the edge distance of any pixel is the minimum value among the distances from that pixel to all edges of the target image.
[0111] For example, refer to Figure 4 As shown, the distance from pixel A in target image 40 to the top edge of the target image is a, the distance to the bottom edge of the target image is b, the distance to the left edge of the target image is c, and the distance to the right edge of the target image is d, and b>d>a>c; since the minimum distance among the distances from pixel A in target image 40 to each edge of the target image is c, the edge distance of pixel A is determined to be c.
[0112] S14. Determine the opacity of the at least one pixel based on the edge distance of the at least one pixel.
[0113] The opacity of any pixel is positively correlated with its edge distance.
[0114] Since the opacity of any pixel in the at least one pixel is positively correlated with its edge distance, the transparency of any pixel in the at least one pixel is negatively correlated with its edge distance. That is, for any pixel in the at least one pixel, the greater the distance to the edge of the target image, the smaller the transparency and the greater the opacity; conversely, the smaller the distance to the edge of the target image, the greater the transparency and the smaller the opacity.
[0115] As an optional implementation of the present invention, step S14 (determining the opacity of the at least one pixel based on the edge distance of the at least one pixel) includes the following steps I to III:
[0116] Step I: Determine whether the edge distance of the first pixel in the at least one pixel is less than the threshold distance.
[0117] The threshold distance in this embodiment of the invention can be set according to the required transparency gradient range. For example, if a transparency gradient of 4 pixels is required, the threshold distance can be set to a distance of 4 pixels.
[0118] The first pixel is any pixel among the at least one pixel.
[0119] In step I above, if the edge distance of the first pixel is less than the threshold distance, then step II is executed.
[0120] Step II: Set the opacity of the first pixel to the preset opacity.
[0121] For example, the preset opacity can be 100%. That is, for the at least one pixel whose edge distance is greater than or equal to the threshold distance, the opacity is set to 100% and the transparency is 0.
[0122] In step I above, if the edge distance of the first pixel is less than the threshold distance, then step III is executed as follows.
[0123] Step III: Set the opacity of the first pixel to the ratio of the edge distance to the threshold distance.
[0124] That is, the opacity of a pixel in the at least one pixel can be determined by the following formula:
[0125]
[0126] Where, alpha i The opacity of pixel i in the at least one pixel is s. i is the edge distance of pixel i in the at least one pixel, and k is the threshold distance.
[0127] As an optional implementation of the present invention, step S14 (determining the opacity of the at least one pixel based on the edge distance of the at least one pixel) includes the following steps ① to ⑩:
[0128] Step ①: Normalize the length and width of the target image.
[0129] That is, the target image is processed into an image of [0,1]×[0,1].
[0130] Step 2: Determine whether the first coordinate value of the second pixel in the at least one pixel is less than a preset threshold.
[0131] Similarly, the threshold distance in the embodiments of the present invention can be set according to the desired range of transparency gradient.
[0132] In step ② above, if the first coordinate value of the second pixel is less than the preset threshold, then step ③ is executed; if the first coordinate value of the second pixel is greater than or equal to the preset threshold, then step ④ is executed.
[0133] Step 3: Set the opacity of the second pixel to the ratio of the first coordinate value to the preset threshold.
[0134] Step 4: Determine whether the first calculated value is less than the preset threshold.
[0135] The first calculated value is the difference between 1 and the first coordinate value.
[0136] In step ④ above, if the first calculated value is less than the preset threshold, then step ⑤ is executed; if the first calculated value is greater than or equal to the preset threshold, then step ⑥ is executed.
[0137] Step 5: Set the opacity of the second pixel to the ratio of the first calculated value to the preset threshold.
[0138] Step 6: Determine whether the second coordinate value of the second pixel is less than the preset threshold.
[0139] In step ⑥ above, if the second coordinate value of the second pixel is less than the preset threshold, then step ⑦ is executed; if the second coordinate value of the second pixel is greater than or equal to the preset threshold, then step ⑧ is executed.
[0140] Step 7: Set the opacity of the second pixel to the ratio of the second coordinate value to the preset threshold.
[0141] Step ⑧: Determine whether the second calculated value is less than the preset threshold.
[0142] The second calculated value is the difference between 1 and the second coordinate value of the second pixel.
[0143] In step ⑧ above, if the second calculated value is less than the preset threshold, then step ⑨ is executed; if the second calculated value is greater than or equal to the preset threshold, then step ⑩ is executed.
[0144] Step 9: Set the opacity of the second pixel to the ratio of the second calculated value to the preset threshold.
[0145] Step 10: Set the opacity of the second pixel to the preset opacity.
[0146] For example, the default opacity can be 100%.
[0147] Let the preset threshold be k and the preset opacity be 100%. For a pixel (x, y) in the target image, if x < k, then the opacity of pixel (x, y) is determined to be x / k; if x ≥ k and 1-x < k, then the opacity of pixel (x, y) is determined to be (1-x) / k; if x ≥ k, 1-x ≥ k, and y < k, then the opacity of pixel (x, y) is determined to be y / k; if x ≥ k, 1-x ≥ k, y ≥ k, and 1-x < k, then the opacity of pixel (x, y) is determined to be (1-y) / k; if x ≥ k, 1-x ≥ k, y ≥ k, and 1-x ≥ k, then the opacity of pixel (x, y) is determined to be 100%.
[0148] S15. Generate a rotated image corresponding to the original image based on the color value of the pixel in the target image, the opacity of the at least one pixel, and the color value of the pixel in the image background corresponding to the at least one pixel.
[0149] As an optional implementation of the present invention, step S15 (generating a rotated image corresponding to the original image based on the color value of the pixel in the target image, the opacity of the at least one pixel, and the color value of the pixel in the image background corresponding to the at least one pixel) includes the following steps 1 to 4:
[0150] Step 1: Calculate the foreground color value of the pixel in the rotated image corresponding to the at least one pixel based on the color value of the pixel in the target image corresponding to the at least one pixel and the opacity of the at least one pixel.
[0151] As an optional embodiment of the present invention, the foreground color value of the pixel in the rotated image corresponding to the third pixel in the at least one pixel is the product of the color value of the pixel in the target image corresponding to the third pixel and the opacity of the third pixel.
[0152] Let the color value of the pixel in the target image corresponding to the third pixel among the at least one pixel be RGB. x,y The opacity of the third pixel is alpha. x,y The foreground color value of the pixel in the rotated image corresponding to the third pixel is... Then we have:
[0153]
[0154] Step 2: Calculate the background color value of the pixel in the rotated image corresponding to the at least one pixel based on the color value of the pixel in the image background corresponding to the at least one pixel and the transparency of the at least one pixel.
[0155] As an optional embodiment of the present invention, the background color value of the pixel in the rotated image corresponding to the third pixel in the at least one pixel is the product of the color value of the pixel in the image background corresponding to the third pixel and the transparency of the third pixel.
[0156] Let the color value of the pixel in the image background corresponding to the third pixel of the at least one pixel be RGB. x,y The opacity of the third pixel is alpha. x,y The background color value of the pixel in the rotated image corresponding to the third pixel is Then we have:
[0157]
[0158] Step 3: Obtain the color value of the pixel in the rotated image corresponding to the at least one pixel based on the foreground color value and background color value of the pixel in the rotated image corresponding to the at least one pixel.
[0159] Specifically, the foreground color value and background color value of the pixel in the rotated image corresponding to the at least one pixel can be calculated to obtain the color value of the pixel in the rotated image corresponding to the at least one pixel.
[0160] Let the color value of the pixel (x, y) in the rotated image corresponding to the third pixel corresponding to the at least one pixel be: Then we have:
[0161]
[0162] Step 4: Generate the rotating image based on the color value of the pixel in the rotating image corresponding to the at least one pixel.
[0163] The image processing method provided in this embodiment of the invention first acquires an original image, then rotates the original image to obtain a target image. Next, it obtains the distance from at least one pixel in the target image to the edge of the target image as the edge distance of the at least one pixel. Then, it determines the opacity of the at least one pixel based on the edge distance. Finally, it generates a rotated image corresponding to the original image based on the color value of the pixel in the target image, the opacity of the at least one pixel, and the color value of the pixel in the image background corresponding to the at least one pixel. Since the image processing method provided in this embodiment of the invention further determines the opacity of the at least one pixel based on the edge distance of the at least one pixel in the target image after rotating the original image to obtain the target image, and the opacity of any pixel in the at least one pixel is positively correlated with its edge distance, the image processing method provided in this embodiment of the invention can also increase the opacity of pixels closer to the edge in the rotated image, thereby avoiding jagged edges in the rotated image and improving the visual effect of the rotated image.
[0164] Based on the same inventive concept, as an implementation of the above method, this embodiment of the invention also provides an image processing device, which corresponds to the aforementioned method embodiment. For ease of reading, this embodiment will not repeat the details of the aforementioned method embodiment one by one, but it should be clear that the image processing device in this embodiment can implement all the contents of the aforementioned method embodiment.
[0165] This invention provides an image processing apparatus. Figure 5 This is a schematic diagram of the image processing device, as shown below. Figure 5 As shown, the image processing apparatus 500 includes:
[0166] Acquisition unit 51 is used to acquire the original image;
[0167] Rotation unit 52 is used to rotate the original image to obtain the target image;
[0168] Processing unit 53 is used to obtain the edge distance of at least one pixel in the target image, wherein the edge distance of any pixel is the distance from the pixel to the edge of the target image;
[0169] The determining unit 54 is configured to determine the opacity of the at least one pixel based on the edge distance of the at least one pixel; the opacity of any pixel is positively correlated with the edge distance of that pixel.
[0170] The generation unit 55 is used to generate a rotated image corresponding to the original image based on the color value of a pixel in the target image, the opacity of the at least one pixel, and the color value of a pixel in the image background corresponding to the at least one pixel.
[0171] As an optional implementation of this invention, the rotation unit 52 is specifically used to determine a target region based on the rotation angle and the size of the original image, wherein the target region is the image region corresponding to the target image; obtain the original pixel points corresponding to each pixel point in the target region, wherein the original pixel point corresponding to any pixel point in the target region is the pixel point corresponding to that pixel point in the original image; calculate the color value of each pixel point in the target region based on the color value of the original pixel points corresponding to each pixel point in the target region; and generate the target image based on the color values of each pixel point in the target region.
[0172] As an optional implementation of the present invention, the rotation unit 52 is specifically used to calculate the original coordinate values corresponding to each pixel in the target area through a reverse wrapping algorithm, wherein the original coordinate value corresponding to any pixel in the target area is the coordinate value corresponding to that pixel in the original image; and to determine the pixels within a preset range of the original coordinate values corresponding to each pixel in the target area as the original pixels corresponding to each pixel in the target area.
[0173] As an optional implementation of the present invention, the rotation unit 52 is specifically used to calculate the color value of each pixel in the target area based on the bilinear sampling algorithm and the color value of the original pixel corresponding to each pixel in the target area.
[0174] As an optional implementation of the present invention, the processing unit 53 is specifically used to obtain the distance from the at least one pixel to each edge of the target image;
[0175] The minimum value among the distances from the at least one pixel to each edge of the target image is determined as the edge distance of the at least one pixel.
[0176] As an optional implementation of the present invention, the determining unit 54 is specifically used to determine whether the edge distance of the first pixel among the at least one pixel is less than a threshold distance; if the edge distance of the first pixel is greater than or equal to the threshold distance, the opacity of the first pixel is set to a preset opacity; if the edge distance of the first pixel is less than the threshold distance, the opacity of the first pixel is set to the ratio of the edge distance to the threshold distance.
[0177] As an optional implementation of this invention, the determining unit 54 is specifically used to normalize the length and width of the target image; determine whether the first coordinate value of the second pixel in the at least one pixel is less than a preset threshold; if the first coordinate value of the second pixel is less than the preset threshold, then set the opacity of the second pixel to the ratio of the first coordinate value to the preset threshold; if the first coordinate value of the second pixel is greater than or equal to the preset threshold, then determine whether the first calculated value is less than the preset threshold, wherein the first calculated value is the difference between 1 and the first coordinate value; if the first calculated value is less than the preset threshold, then set the opacity of the second pixel to the ratio of the first calculated value to the preset threshold ... If the value is greater than or equal to the preset threshold, then it is determined whether the second coordinate value of the second pixel is less than the preset threshold; if the second coordinate value of the second pixel is less than the preset threshold, then the opacity of the second pixel is set to the ratio of the second coordinate value to the preset threshold; if the second coordinate value of the second pixel is greater than or equal to the preset threshold, then it is determined whether the second calculated value is less than the preset threshold, the second calculated value being the difference between 1 and the second coordinate value of the second pixel; if the second calculated value is less than the preset threshold, then the opacity of the second pixel is set to the ratio of the second calculated value to the preset threshold; if the second calculated value is greater than or equal to the preset threshold, then the opacity of the second pixel is set to a preset opacity.
[0178] As an optional implementation of this invention, the generation unit 55 is specifically configured to: calculate the foreground color value of a pixel in the rotated image corresponding to the at least one pixel based on the color value of a pixel in the target image corresponding to the at least one pixel and the opacity of the at least one pixel; calculate the background color value of a pixel in the rotated image corresponding to the at least one pixel based on the color value of a pixel in the image background corresponding to the at least one pixel and the opacity of the at least one pixel; obtain the color value of a pixel in the rotated image corresponding to the at least one pixel based on the foreground color value and the background color value of the pixel in the rotated image corresponding to the at least one pixel; and generate the rotated image based on the color value of the pixel in the rotated image corresponding to the at least one pixel.
[0179] As an optional implementation of the present invention,
[0180] The foreground color value of the pixel in the rotated image corresponding to the third pixel in at least one pixel is the product of the color value of the pixel in the target image corresponding to the third pixel and the opacity of the third pixel.
[0181] The background color value of the pixel in the rotated image corresponding to the third pixel is the product of the color value of the pixel in the background of the image corresponding to the third pixel and the transparency of the third pixel.
[0182] The image processing apparatus provided in this embodiment can execute the image processing method provided in the above method embodiment. Its implementation principle and technical effect are similar, and will not be described again here.
[0183] Based on the same inventive concept, embodiments of the present invention also provide an electronic device. Figure 6 This is a schematic diagram of the structure of an electronic device provided in an embodiment of the present invention, such as... Figure 6 As shown, the electronic device provided in this embodiment includes a memory 601 and a processor 602. The memory 601 is used to store computer programs; the processor 602 is used to execute the image processing method provided in the above embodiment when executing the computer programs.
[0184] Based on the same inventive concept, embodiments of the present invention also provide a computer-readable storage medium storing a computer program, which, when executed by a processor, causes the computing device to implement the image processing method provided in the above embodiments.
[0185] Based on the same inventive concept, this embodiment of the invention also provides a computer program product, which, when run on a computer, enables the computing device to implement the image processing method provided in the above embodiments.
[0186] Those skilled in the art will understand that embodiments of the present invention can be provided as methods, systems, or computer program products. Therefore, the present invention can take the form of a completely hardware embodiment, a completely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, the present invention can take the form of a computer program product implemented on one or more computer-usable storage media containing computer-usable program code.
[0187] The processor can be a Central Processing Unit (CPU), or other general-purpose processors, digital signal processors (DSPs), application-specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. A general-purpose processor can be a microprocessor or any conventional processor.
[0188] Memory may include non-persistent memory in computer-readable media, such as random access memory (RAM) and / or non-volatile memory, like read-only memory (ROM) or flash RAM. Memory is an example of computer-readable media.
[0189] Computer-readable media include both permanent and non-permanent, removable and non-removable storage media. Storage media can store information using any method or technology; the information can be computer-readable instructions, data structures, program modules, or other data. Examples of computer storage media include, but are not limited to, phase-change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technologies, CD-ROM, digital versatile optical disc (DVD) or other optical storage, magnetic tape, disk storage or other magnetic storage devices, or any other non-transferable medium that can be used to store information accessible by a computing device. As defined herein, computer-readable media do not include transient computer-readable media, such as modulated data signals and carrier waves.
[0190] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, and not to limit them; although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some or all of the technical features; and these modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of the present invention.
Claims
1. An image processing method, characterized in that, include: Obtain the original image; The original image is rotated to obtain the target image; Obtain the edge distance of at least one pixel in the target image, wherein the edge distance of any pixel is the distance from that pixel to the edge of the target image; The opacity of the at least one pixel is determined based on the edge distance of the at least one pixel; the opacity of any pixel is positively correlated with the edge distance of that pixel; A rotated image corresponding to the original image is generated based on the color values of pixels in the target image, the opacity of at least one pixel, and the color values of pixels in the image background corresponding to at least one pixel. The step of rotating the original image to obtain the target image includes: The target region is determined based on the rotation angle and the size of the original image, and the target region is the image region corresponding to the target image. The original coordinate values corresponding to each pixel in the target area are calculated by the reverse wrapping algorithm. The original coordinate value corresponding to any pixel in the target area is the coordinate value of that pixel in the original image. The pixels within a preset range of the original coordinate values corresponding to each pixel in the target area are determined as the original pixels corresponding to each pixel in the target area. Calculate the color value of each pixel in the target area based on the color value of the original pixel corresponding to each pixel in the target area; The target image is generated based on the color values of each pixel within the target area.
2. The method according to claim 1, characterized in that, The step of calculating the color value of each pixel in the target area based on the color value of the original pixel corresponding to each pixel in the target area includes: The color value of each pixel in the target area is calculated based on the bilinear sampling algorithm and the original pixel color value corresponding to each pixel in the target area.
3. The method according to claim 1, characterized in that, The step of obtaining the edge distance of at least one pixel in the target image includes: Obtain the distance from the at least one pixel to each edge of the target image; The minimum value among the distances from the at least one pixel to each edge of the target image is determined as the edge distance of the at least one pixel.
4. The method according to claim 1, characterized in that, Determining the opacity of the at least one pixel based on its edge distance includes: Determine whether the edge distance of the first pixel in the at least one pixel is less than a threshold distance; If the edge distance of the first pixel is greater than or equal to the threshold distance, then the opacity of the first pixel is set to a preset opacity. If the edge distance of the first pixel is less than the threshold distance, then the opacity of the first pixel is set to the ratio of the edge distance to the threshold distance.
5. The method according to claim 1, characterized in that, Determining the opacity of the at least one pixel based on its edge distance includes: The length and width of the target image are normalized. Determine whether the first coordinate value of the second pixel in the at least one pixel is less than a preset threshold; If the first coordinate value of the second pixel is less than the preset threshold, then the opacity of the second pixel is set to the ratio of the first coordinate value to the preset threshold. If the first coordinate value of the second pixel is greater than or equal to the preset threshold, then it is determined whether the first calculated value is less than the preset threshold. The first calculated value is the difference between 1 and the first coordinate value. If the first calculated value is less than the preset threshold, then the opacity of the second pixel is set to the ratio of the first calculated value to the preset threshold. If the first calculated value is greater than or equal to the preset threshold, then determine whether the second coordinate value of the second pixel is less than the preset threshold; If the second coordinate value of the second pixel is less than the preset threshold, then the opacity of the second pixel is set to the ratio of the second coordinate value to the preset threshold. If the second coordinate value of the second pixel is greater than or equal to the preset threshold, then it is determined whether the second calculated value is less than the preset threshold. The second calculated value is the difference between 1 and the second coordinate value of the second pixel. If the second calculated value is less than the preset threshold, then the opacity of the second pixel is set to the ratio of the second calculated value to the preset threshold; If the second calculated value is greater than or equal to the preset threshold, then the opacity of the second pixel is set to the preset opacity.
6. The method according to any one of claims 1-5, characterized in that, The step of generating a rotated image corresponding to the original image based on the color values of pixels in the target image, the opacity of at least one pixel, and the color values of pixels in the image background corresponding to the at least one pixel includes: Calculate the foreground color value of the pixel in the rotated image corresponding to the at least one pixel based on the color value of the pixel in the target image corresponding to the at least one pixel and the opacity of the at least one pixel; Calculate the background color value of the pixel in the rotated image corresponding to the at least one pixel based on the color value of the pixel in the image background corresponding to the at least one pixel and the transparency of the at least one pixel; Based on the foreground color value and background color value of the pixel in the rotated image corresponding to the at least one pixel, obtain the color value of the pixel in the rotated image corresponding to the at least one pixel; The rotating image is generated based on the color value of the pixel in the rotating image corresponding to the at least one pixel.
7. The method according to claim 6, characterized in that, The foreground color value of the pixel in the rotated image corresponding to the third pixel in at least one pixel is the product of the color value of the pixel in the target image corresponding to the third pixel and the opacity of the third pixel. The background color value of the pixel in the rotated image corresponding to the third pixel is the product of the color value of the pixel in the background of the image corresponding to the third pixel and the transparency of the third pixel.
8. An image processing apparatus, characterized in that, include: The acquisition unit is used to acquire the original image; A rotation unit is used to rotate the original image to obtain the target image; The processing unit is configured to obtain the edge distance of at least one pixel in the target image, wherein the edge distance of any pixel is the distance from the pixel to the edge of the target image; A determining unit is configured to determine the opacity of the at least one pixel based on the edge distance of the at least one pixel; The opacity of any pixel is positively correlated with its edge distance. The generation unit is configured to generate a rotated image corresponding to the original image based on the color values of pixels in the target image, the opacity of at least one pixel, and the color values of pixels in the image background corresponding to the at least one pixel. The acquisition unit is specifically configured to: determine a target region based on the rotation angle and the size of the original image, wherein the target region is the image region corresponding to the target image; calculate the original coordinate values corresponding to each pixel in the target region using a reverse wrapping algorithm, wherein the original coordinate values corresponding to any pixel in the target region are the coordinate values corresponding to that pixel in the original image; determine the pixels within a preset range of the original coordinate values corresponding to each pixel in the target region as the original pixels corresponding to each pixel in the target region; calculate the color values of each pixel in the target region based on the color values of the original pixels corresponding to each pixel in the target region; and generate the target image based on the color values of each pixel in the target region.
9. An electronic device, characterized in that, include: A memory and a processor, the memory being used to store a computer program; the processor being used to cause the electronic device to implement the image processing method according to any one of claims 1-7 when executing the computer program.
10. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program that, when executed by a computing device, causes the computing device to implement the image processing method according to any one of claims 1-7.
11. A computer program product, characterized in that, When the computer program product is run on a computer, the computer enables the computer to implement the image processing method as described in any one of claims 1-7.
Citation Information
Patent Citations
Anti-aliasing method and anti-aliasing system based on texture mapping
CN106600544A
Image rotation method, device and image formation equipment
CN106780352A
Video fusion method, electronic equipment and storage medium
CN110971839A