Disclosure of Invention
The invention aims to provide a JPEG photo repairing method, a JPEG photo repairing device and related components, and aims to solve the problem that the existing hyper-resolution image cannot utilize reserved data to the maximum extent to obtain effective information which is as comprehensive as possible.
In order to solve the technical problems, the invention aims to realize the following technical scheme: provided is a JPEG photo repairing method, which comprises the following steps:
extracting thumbnails of JPEG pictures, and carrying out super-resolution processing on the thumbnails to obtain super-resolution pictures;
reading an original image of the JPEG photo, and filling lost pixels in the original image to enable the pixel value filling of each lost pixel point to be 128;
acquiring an initial mask of the filled original image, searching and recording a damage target area with the largest area in the initial mask, and setting the pixel value of each pixel point in the initial mask to be 0;
filling corresponding positions of the initial mask after 0 is arranged on the basis of the recorded damage target area, and updating the pixel value of each pixel point of the damage target area to 255 to obtain a target mask;
performing pixel value conversion on all pixel points with pixel values of 0 and 255 in the target mask to convert the pixel points with the pixel values of 0 into the pixel points with the pixel values of 255, and converting the pixel points with the pixel values of 255 into the pixel points with the pixel values of 0 to obtain a matching mask;
updating the pixel values of all pixel points in the region of the original graph corresponding to the pixel value of 255 in the target mask to be 0, and updating the pixel values of all pixel points in the region of the hyper-map corresponding to the pixel value of 255 in the matching mask to be 0;
and performing matrix addition on the updated super-resolution image and the original image, and outputting a target photo.
Another object of the present invention is to provide a JPEG still image restoration apparatus, including:
the super-dividing unit is used for extracting thumbnails of JPEG pictures and carrying out super-dividing processing on the thumbnails to obtain super-divided pictures;
the filling unit is used for reading the original image of the JPEG photo and filling lost pixels in the original image to enable the pixel value of each lost pixel point to be 128;
the searching unit is used for acquiring the filled initial mask of the original image, searching and recording a damage target area with the largest area in the initial mask, and setting the pixel value of each pixel point in the initial mask to be 0;
the updating unit is used for filling corresponding positions of the initial mask after 0 is arranged on the basis of the recorded damage target area, so that the pixel value of each pixel point of the damage target area is updated to be 255, and a target mask is obtained;
a first conversion unit, configured to obtain a matching mask of the super-resolution map, update a pixel value of each pixel point of an area corresponding to the damage target area in the matching mask to 0 based on the recorded damage target area, and update a pixel value of each pixel point of a remaining area in the matching mask to 255;
a second conversion unit, configured to update pixel values of all pixel points in a region, where the pixel value in the target mask is 255, of the original graph to 0, and update pixel values of all pixel points in a region, where the pixel value in the matching mask is 255, of the hyper-resolution graph to 0;
and the output unit is used for performing matrix addition on the updated super-resolution image and the original image and outputting the target photo.
In addition, an embodiment of the present invention further provides a computer device, which includes a memory, a processor, and a computer program stored on the memory and executable on the processor, where the processor implements the method for repairing a JPEG photo according to the first aspect when executing the computer program.
In addition, an embodiment of the present invention further provides a computer-readable storage medium, where the computer-readable storage medium stores a computer program, and the computer program, when executed by a processor, causes the processor to execute the method for repairing a JPEG photo according to the first aspect.
The embodiment of the invention discloses a method, a device and related components for repairing a JPEG (joint photographic experts group) photo, wherein the method comprises the following steps: extracting thumbnails of JPEG pictures, and carrying out super-resolution processing on the thumbnails to obtain super-resolution pictures; reading an original image of the JPEG photo, and filling lost pixels in the original image to enable the pixel value filling of each lost pixel point to be 128; acquiring an initial mask of the filled original image, searching and recording a damage target area with the largest area in the initial mask, and setting the pixel value of each pixel point in the initial mask to be 0; filling corresponding positions of the initial mask after 0 is arranged on the basis of the recorded damage target area, and updating the pixel value of each pixel point of the damage target area to 255 to obtain a target mask; performing pixel value conversion on all pixel points with pixel values of 0 and 255 in the target mask to convert the pixel points with the pixel values of 0 into the pixel points with the pixel values of 255, and converting the pixel points with the pixel values of 255 into the pixel points with the pixel values of 0 to obtain a matching mask; updating the pixel values of all pixel points in the region of the original graph corresponding to the pixel value of 255 in the target mask to be 0, and updating the pixel values of all pixel points in the region of the hyper-map corresponding to the pixel value of 255 in the matching mask to be 0; performing matrix addition on the updated super-resolution image and the original image, and outputting a target photo; the method reserves the non-damaged area in the original image, selects the damaged area in the original image from the hyper-map, and combines the two areas, thereby obtaining better visual effect than the hyper-map and obtaining and displaying effective information as comprehensive as possible by utilizing the reserved data to the maximum extent.
Detailed Description
The technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the drawings in the embodiments of the present invention, and it is obvious that the described embodiments are some, not all, embodiments of the present invention. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
It will be understood that the terms "comprises" and/or "comprising," when used in this specification and the appended claims, specify the presence of stated features, integers, steps, operations, elements, and/or components, but do not preclude the presence or addition of one or more other features, integers, steps, operations, elements, components, and/or groups thereof.
It is also to be understood that the terminology used in the description of the invention herein is for the purpose of describing particular embodiments only and is not intended to be limiting of the invention. As used in the specification of the present invention and the appended claims, the singular forms "a," "an," and "the" are intended to include the plural forms as well, unless the context clearly indicates otherwise.
It should be further understood that the term "and/or" as used in this specification and the appended claims refers to and includes any and all possible combinations of one or more of the associated listed items.
Referring to fig. 1, fig. 1 is a schematic flow chart illustrating a JPEG photo restoration method according to an embodiment of the present invention;
as shown in fig. 1, the method includes steps S101 to S107.
S101, extracting thumbnails of JPEG pictures, and carrying out super-division processing on the thumbnails to obtain super-division pictures;
s102, reading an original image of the JPEG photo, and filling lost pixels in the original image to enable the pixel value filling of each lost pixel point to be 128;
s103, acquiring the filled initial mask of the original image, searching and recording a damage target region with the largest area in the initial mask, and setting the pixel value of each pixel point in the initial mask to be 0;
s104, based on the recorded damage target area, filling corresponding positions of the initial mask after being aligned to 0, and updating the pixel value of each pixel point of the damage target area to 255 to obtain a target mask;
s105, performing pixel value conversion on all pixel points with pixel values of 0 and 255 in the target mask to convert the pixel points with the pixel values of 0 into the pixel points with the pixel values of 255, and converting the pixel points with the pixel values of 255 into the pixel points with the pixel values of 0 to obtain a matching mask;
s106, updating the pixel values of all pixel points in the area, corresponding to the pixel value of 255, in the target mask to 0, of the original graph, and updating the pixel values of all pixel points in the area, corresponding to the pixel value of 255, in the matching mask to 0, of the hyper-map;
107. and performing matrix addition on the updated super-resolution image and the original image, and outputting a target photo.
In the prior art, only the thumbnails of the JPEG photos are subjected to super-resolution processing, and the corresponding super-resolution images are output, because the detail loss in the thumbnails is serious, even if the super-resolution images with good visual effects are obtained after the thumbnails are processed by the super-resolution technology, the output requirements of the images cannot be met, so that in order to obtain effective information which is displayed as comprehensively as possible by using the retained data to the maximum extent, according to the application, the SOI (FFD8) and the EOI (FFD9) are searched for by using identifiers of JPEG standards, thumbnails corresponding to the JPEG photos (as shown in fig. 2) are obtained, then the super-resolution technology is used for performing super-resolution processing on the obtained thumbnails to obtain the corresponding super-resolution images (as shown in fig. 4), and it needs to be noted that the super-resolution technology in the application is a class of depth learning algorithms such as realsr.
Reading an original image (as shown in fig. 3) of the JPEG photo, it should be noted that the original image of the present application is an original image with a local damage, that is, the original image has an area with a part of lost pixels, and in an opencv library, the original image is also a data type of a matrix Mat, and has various attributes such as size and element type; the present application fills the area of the missing pixels with a gray RGB (128, 128, 128), where the boundary between the area of the missing pixels and the area of the non-missing pixels is different, but is also gray, and the value of the gray is floating within a small range.
In order to initially divide a gray area, namely a damaged area, the method first obtains an initial mask of the original image after filling, and because the initial mask has more or less noise points in the corresponding undamaged area, each gray area needs to be found in the initial mask, and based on the condition that the probability of a large pure gray area appearing in an actual picture is very low, the gray area with the largest area is taken as a damaged target area to exclude the gray area of the original image and circle out a damaged part; and simultaneously recording the damaged target area, setting the pixel value of each pixel point in the initial mask to be 0, wherein the whole initial mask is black, updating the pixel value of each pixel point in the damaged target area of the initial mask from 0 to 255 based on the previously recorded damaged target area, wherein the damaged target area of the initial mask is white to obtain a target mask, namely, the target mask is changed into a black-white image.
Then, pixel value conversion is carried out on all pixel points with the pixel values of 0 and 255 in the target mask, so that the pixel points with the pixel values of 0 are converted into the pixel points with the pixel values of 255, the pixel points with the pixel values of 255 are converted into the pixel points with the pixel values of 0, and a matching mask is obtained; and then updating the pixel values of all pixel points in the region of the original image corresponding to the pixel value of 255 in the target mask to 0, updating the pixel values of all pixel points in the region of the hyper-map corresponding to the pixel value of 255 in the matching mask to 0, namely, setting the region of the hyper-map corresponding to the damaged target region to 0, selecting the region of the original image with the pixel value of 255 of each pixel point in the target mask, and finally adding the processed original image and the hyper-map together, wherein the region of 0 in the original image and the hyper-map are just not overlapped, namely the region of 0 in the original image does not change the value of the region corresponding to the hyper-map, and the region of 0 in the hyper-map does not change the value of the region corresponding to the original image.
The target photo (shown in fig. 6) obtained by the JPEG photo restoration method can acquire and display effective information as comprehensive as possible by utilizing the reserved data to the maximum extent, and the details of the photo are far better than those of a hyper-map.
In one embodiment, the step S102 includes the following steps:
s10, reading the original graph by using an imread function;
s11, filling the lost pixels in the original graph by using an imread function.
It should be noted that the imread function in this embodiment is a function in the opencv library, and is used to read data in a picture file, and the gray value of actual data loss may be filled in with the imread function.
In an embodiment, before the step S103, the method includes the following steps:
s20, downsampling the original graph by using a pyrDOWN function to enable the size of the original graph to reach a preset threshold value;
s21, adjusting the size of the original graph after down sampling to make the size of the original graph and the size of the super graph the same;
s22, converting the original graph into an HSV color space by using a cvtColor function.
It should be noted that, pyrDOWN is a downsampling operation, resize is a resizing operation, since the aspect ratio of the thumbnail (and the corresponding hyper-map are the same) is not necessarily the same as that of the original image, and if direct resize merging is performed, there may be a phenomenon that the boundary is not correct, the present application first downsamples the original image through a pyrDOWN function, makes the size of the original image approach the size of the hyper-map, or makes one time or an integral multiple of the original image larger than the hyper-map, but generally makes the multiple not exceed 4, and then resize the downsampled original image to the target size, that is, the size of the hyper-map, so as to avoid the problem of boundary mismatch when merging occurs.
In step S22, cvcvcvtcolor is a color space conversion function in Opencv, and can implement conversion from RGB colors to color spaces such as HSV and HSI, and can also be converted into grayscale images, where the parameter used in this application is BRG2HSV, and it should be noted that HSV color spaces are color regions that are easy to rapidly segment, and RGB spaces are difficult to segment different color regions.
In one embodiment, the step S103 includes the following steps:
s30, acquiring an initial mask of the original image by using an inRange function;
s31, searching all gray areas of the initial mask by using a findContours function;
s32, calculating the area of each gray area by using a contourArea function;
and S33, comparing and judging the area of each gray area, finding the gray area with the largest area, and taking the gray area as a damage target area.
In the present embodiment, the "inRange function" may implement a binarization function, and set pixel values within two threshold values to white (255) and pixel values not within a threshold interval to black (0), thereby preliminarily dividing a gray region.
It should be noted that findcontours and contourArea are both functions in the opencv library, the former finds the outline of a region that may exist in a picture, and the latter calculates the area of each region, and accordingly finds the largest, and also the actual data loss part. Considering that in an actual photograph, a region where a large block of pure gray appears is likely to be small, a gray region having the largest area is taken as a damage target region.
In one embodiment, the step S104 includes:
and S40, filling the corresponding position of the initial mask by using a drawContours function, and updating the pixel value of each pixel point of the damage target area to 255.
In this embodiment, the drawContours function is a function in the opencv library, and the noise point in the initial mask may be removed by filling the drawContours function in the corresponding position of the initial mask, so as to obtain the target mask.
With reference to fig. 5, although most of the noise points in the initial mask are removed, there will be residual at the boundary line, and if the target mask processed in step S40 is directly used for merging, there will be gray lines with different shades at the boundary between two parts of the merged image, so in this embodiment, the step S104 includes:
and S50, performing boundary line correction processing on the target mask by using a dilate function.
In this embodiment, the dilate function is a function in the opencv library, and the boundary line can be pushed to the non-lost image part slightly by using the dilate function, that is, the white area is slightly expanded, and the black area is slightly contracted, for example, after the graph of the white-background black word is processed by using the dilate function, the black word is slightly thinned, so as to eliminate the noise point of the boundary line in the present application.
In one embodiment, the step S105 includes:
and S60, updating the pixel value of each pixel point of the region corresponding to the damage target region in the matching mask to 0 by using a complete function or a bitwise _ not function based on the recorded damage target region, and updating the pixel value of each pixel point of the remaining region in the matching mask to 255.
In this embodiment, the complete function or the bitwise _ not function are both functions in the opencv library, and the pixel values of the pixels in the region corresponding to the damage target region in the matching mask can be updated to 0 through both the functions, and the pixel values of the pixels in the remaining region in the matching mask can be updated to 255.
In one embodiment, the step S107 includes:
and S70, performing matrix addition on the updated supergraph and the original graph, and storing the supergraph and the original graph into a preset format by using an imwrite function to obtain a target photo in a corresponding format.
It should be noted that the imwrite function is a function in the opencv library, and the application can store the target photo in various formats such as jpg, Jpeg, and the like.
The embodiment of the invention also provides a device for repairing the JPEG photo, which is used for executing any embodiment of the repairing method of the JPEG photo. Specifically, referring to fig. 7, fig. 7 is a schematic block diagram of a JPEG photo restoration apparatus according to an embodiment of the present invention.
As shown in fig. 7, the JPEG photograph restoration apparatus 500 includes:
a super-segmentation unit 501, configured to extract thumbnails of JPEG photos, and perform super-segmentation processing on the thumbnails to obtain super-segmentation maps;
a filling unit 502, configured to read an original image of the JPEG photo, fill missing pixels in the original image, and make the pixel value of each missing pixel point be 128;
a searching unit 503, configured to obtain an initial mask of the filled original image, find and record a damage target region with a largest area in the initial mask, and set a pixel value of each pixel in the initial mask to 0;
an updating unit 504, configured to fill, based on the recorded damage target region, a corresponding position of the initial mask after being set to 0, so that a pixel value of each pixel point of the damage target region is updated to 255, and a target mask is obtained;
a first conversion unit 505, configured to perform pixel value conversion on all pixel points with a pixel value of 0 and a pixel value of 255 in the target mask, so that the pixel point with the pixel value of 0 is converted into the pixel point with the pixel value of 255, and the pixel point with the pixel value of 255 is converted into the pixel point with the pixel value of 0, so as to obtain a matching mask;
a second converting unit 506, configured to update pixel values of all pixel points in a region, where the pixel value in the target mask is 255, of the original graph to 0, and update pixel values of all pixel points in a region, where the pixel value in the matching mask is 255, of the hyper-resolution graph to 0;
and an output unit 507, configured to perform matrix addition on the updated hyper-map and the original map, and output a target photo.
The target photo obtained by the device can acquire and display effective information as comprehensive as possible by utilizing the reserved data to the maximum extent, and the details of the photo are far superior to those of a super-resolution map.
It is clear to those skilled in the art that, for convenience and brevity of description, the specific working processes of the above-described apparatuses and units may refer to the corresponding processes in the foregoing method embodiments, and are not described herein again.
The JPEG photo restoration apparatus described above may be implemented in the form of a computer program that can be run on a computer device as shown in fig. 8.
Referring to fig. 8, fig. 8 is a schematic block diagram of a computer device according to an embodiment of the present invention. The computer device 1100 is a server, and the server may be an independent server or a server cluster including a plurality of servers.
Referring to fig. 8, the computer device 1100 includes a processor 1102, memory and network interface 1105 connected by a system bus 1101, where the memory may include non-volatile storage media 1103 and internal memory 1104.
The non-volatile storage medium 1103 may store an operating system 11031 and computer programs 11032. The computer program 11032, when executed, may cause the processor 1102 to perform a method of repairing JPEG photos.
The processor 1102 is configured to provide computing and control capabilities that support the operation of the overall computing device 1100.
The internal memory 1104 provides an environment for running the computer program 11032 in the nonvolatile storage medium 1103, and when the computer program 11032 is executed by the processor 1102, the processor 1102 may be caused to execute a JPEG photograph restoration method.
The network interface 1105 is used for network communications, such as to provide for the transmission of data information. Those skilled in the art will appreciate that the configuration shown in fig. 8 is a block diagram of only a portion of the configuration associated with aspects of the present invention and is not intended to limit the computing device 1100 to which aspects of the present invention may be applied, and that a particular computing device 1100 may include more or less components than those shown, or may combine certain components, or have a different arrangement of components.
Those skilled in the art will appreciate that the embodiment of a computer device illustrated in fig. 8 does not constitute a limitation on the specific construction of the computer device, and that in other embodiments a computer device may include more or fewer components than those illustrated, or some components may be combined, or a different arrangement of components. For example, in some embodiments, the computer device may only include a memory and a processor, and in such embodiments, the structures and functions of the memory and the processor are consistent with those of the embodiment shown in fig. 8, and are not described herein again.
It should be appreciated that in embodiments of the present invention, the Processor 1102 may be a Central Processing Unit (CPU), and the Processor 1102 may also be 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. Wherein a general purpose processor may be a microprocessor or the processor may be any conventional processor or the like.
In another embodiment of the invention, a computer-readable storage medium is provided. The computer readable storage medium may be a non-volatile computer readable storage medium. The computer readable storage medium stores a computer program, wherein the computer program, when executed by a processor, implements a method for repairing a JPEG photo according to an embodiment of the present invention.
The storage medium is an entity and non-transitory storage medium, and may be various entity storage media capable of storing program codes, such as a usb disk, a removable hard disk, a Read-Only Memory (ROM), a magnetic disk, or an optical disk.
It is clear to those skilled in the art that, for convenience and brevity of description, the specific working processes of the above-described apparatuses, devices and units may refer to the corresponding processes in the foregoing method embodiments, and are not described herein again.
While the invention has been described with reference to specific embodiments, the invention is not limited thereto, and various equivalent modifications and substitutions can be easily made by those skilled in the art within the technical scope of the invention. Therefore, the protection scope of the present invention shall be subject to the protection scope of the claims.