Disclosure of Invention
The invention aims to provide a method, a device and related components for repairing a JPEG photo, and aims to solve the problem that the existing superresolution graph cannot utilize reserved data to the greatest extent to acquire effective information as comprehensive as possible.
In order to solve the technical problems, the aim of the invention is realized by the following technical scheme: provided is a method for repairing a JPEG photo, comprising:
Extracting a thumbnail of a JPEG photo, and performing superprocessing on the thumbnail to obtain a superresolution picture;
Reading an original image of the JPEG photo, and filling lost pixels in the original image to fill the pixel value of each lost pixel point to 128;
acquiring an initial mask of the filled original graph, 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 0;
Filling the corresponding position of the initial mask after the initial mask is set to 0 based on 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 the pixel value of 0 and the pixel value of 255 in the target mask, converting the pixel point with the pixel value of 0 into the pixel point with the pixel value of 255, and converting the pixel point with the pixel value of 255 into the pixel point with the pixel value of 0 to obtain a matching mask;
updating the pixel values of all the pixel points in the region with the pixel value of 255 in the target mask corresponding to the original image to be 0, and updating the pixel values of all the pixel points in the region with the pixel value of 255 in the matching mask corresponding to the super-resolution image to be 0;
And carrying out matrix addition on the updated superdivision graph and the original graph, and outputting a target photo.
In addition, the technical problem to be solved by the invention is to provide a repairing device for JPEG photo, comprising:
The superdivision unit is used for extracting the thumbnail of the JPEG photo and performing superprocessing on the thumbnail to obtain a superdivision diagram;
The filling unit is used for reading an original image of the JPEG photo, filling lost pixels in the original image, and filling pixel values of lost pixels to 128;
The searching unit is used for 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 0;
an updating unit, configured to fill the corresponding position of the initial mask after the position 0 is set based on the recorded damage target area, so that the pixel value of each pixel point of the damage target area is updated to 255, and a target mask is obtained;
a first conversion unit, configured to obtain a matching mask of the hyperresolution graph, update, based on the recorded damage target area, a pixel value of each pixel of an area corresponding to the damage target area in the matching mask to be 0, and update a pixel value of each pixel of the remaining areas in the matching mask to be 255;
A second conversion unit, configured to update the pixel values of all the pixel points in the area where the pixel value in the original image corresponds to 255 in the target mask to 0, and update the pixel values of all the pixel points in the area where the pixel value in the super-resolution image corresponds to 255 in the matching mask to 0;
And the output unit is used for carrying out matrix addition on the updated superdivision graph and the original graph and outputting a 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 in the memory and capable of running on the processor, where the processor implements the method for repairing a JPEG photograph 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, where the computer program when executed by a processor causes the processor to execute the method for repairing a JPEG photograph according to the first aspect.
The embodiment of the invention discloses a method, a device and related components for repairing a JPEG photo, wherein the method comprises the following steps: extracting a thumbnail of a JPEG photo, and performing superprocessing on the thumbnail to obtain a superresolution picture; reading an original image of the JPEG photo, and filling lost pixels in the original image to fill the pixel value of each lost pixel point to 128; acquiring an initial mask of the filled original graph, 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 0; filling the corresponding position of the initial mask after the initial mask is set to 0 based on 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 the pixel value of 0 and the pixel value of 255 in the target mask, converting the pixel point with the pixel value of 0 into the pixel point with the pixel value of 255, and converting the pixel point with the pixel value of 255 into the pixel point with the pixel value of 0 to obtain a matching mask; updating the pixel values of all the pixel points in the region with the pixel value of 255 in the target mask corresponding to the original image to be 0, and updating the pixel values of all the pixel points in the region with the pixel value of 255 in the matching mask corresponding to the super-resolution image to be 0; matrix adding the updated superdivision graph and the original graph, and outputting a target photo; the method comprises the steps of reserving a non-damaged area in an original image, selecting a damaged area in the original image from a superdivision image, and combining the two partial areas, so that a better visual effect is obtained compared with the superdivision image, and the reserved data is utilized to obtain and display the effective information as comprehensively as possible to the greatest extent.
Detailed Description
The following description of the embodiments of the present invention will be made clearly and fully with reference to the accompanying drawings, in which it is evident that the embodiments described are some, but not all embodiments of the invention. All other embodiments, which can be made by those skilled in the art based on the embodiments of the invention without making any inventive effort, are intended to be within the scope of the invention.
It should be understood that the terms "comprises" and "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 this specification 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 the present specification and the appended claims refers to any and all possible combinations of one or more of the associated listed items, and includes such combinations.
Referring to fig. 1, fig. 1 is a flow chart of a method for repairing a JPEG photograph according to an embodiment of the present invention;
as shown in fig. 1, the method includes steps S101 to S107.
S101, extracting a thumbnail of a JPEG photo, and performing superprocessing on the thumbnail to obtain a superresolution diagram;
s102, reading an original image of the JPEG photo, and filling lost pixels in the original image to enable pixel values of lost pixels to be 128;
S103, acquiring an initial mask of the original image after filling, 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 0;
S104, filling the corresponding positions of the initial masks after the positions of 0 are set on the basis of the recorded damage target areas, and updating the pixel values of all pixel points of the damage target areas to 255 to obtain target masks;
S105, performing pixel value conversion on all the pixels with the pixel value of 0 and the pixel value of 255 in the target mask, converting the pixel with the pixel value of 0 into the pixel with the pixel value of 255, and converting the pixel with the pixel value of 255 into the pixel with the pixel value of 0 to obtain a matching mask;
s106, updating the pixel values of all the pixel points in the region with the pixel value of 255 in the target mask corresponding to the original image to be 0, and updating the pixel values of all the pixel points in the region with the pixel value of 255 in the matching mask corresponding to the super-resolution image to be 0;
107. and carrying out matrix addition on the updated superdivision graph and the original graph, and outputting a target photo.
In the prior art, only the thumbnail of the JPEG photo is processed in a super-division manner to obtain a corresponding super-division map, and the corresponding super-division map is output, because the detail loss in the thumbnail is serious, even if the super-division map with good visual effect is obtained after the thumbnail is processed by the super-resolution technology, the output requirement of the image cannot be met, so that in order to maximally utilize the reserved data to acquire and display the effective information as comprehensively as possible, the application searches the SOI (FFD 8) and the EOI (FFD 9) according to the identifier markers of the JPEG standard to obtain the corresponding thumbnail of the JPEG photo (shown in fig. 2), and then the super-resolution technology is utilized to perform the super-division processing on the obtained thumbnail to obtain the corresponding super-division map (shown in fig. 4), and the description is that the super-resolution technology is a depth learning algorithm such as realsr.
Reading an original image (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 local damage, that is, an area where a part of lost pixels exist in the original image, and in the opencv library, the original image is also a data type of matrix Mat, and has various attributes such as size, element type, etc.; the application fills the areas of the missing pixels with gray RGB (128, 128, 128), where the boundary between the areas of the missing pixels and the areas of the non-missing pixels will be different, but also gray, and the gray values will float in a small range.
In order to initially divide the gray area, namely the damaged area, the application firstly obtains the initial mask of the original image after filling, and because the initial mask can have more or less noise points in the corresponding undamaged area, each gray area needs to be found in the initial mask, and the gray area with the largest area is taken as the damaged target area based on the condition that the possibility of the occurrence of a large pure gray area in an actual photo is small, so that the original image is eliminated from the gray area, and the damaged part is circled; and simultaneously recording the damage target area, setting the pixel value of each pixel point in the initial mask to 0, wherein the whole initial mask is black, and updating the pixel value of each pixel point in the damage target area of the initial mask from 0 to 255 based on the previously recorded damage target area, wherein the damage target area of the initial mask becomes white to obtain a target mask, namely, the target mask becomes an image with black and white phases.
Then, performing pixel value conversion on all pixel points with the pixel value of 0 and the 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, thereby obtaining a matched mask; and updating the pixel values of all the pixel points in the region with the pixel value of 255 in the target mask corresponding to the original image to be 0, updating the pixel values of all the pixel points in the region with the pixel value of 255 in the matching mask corresponding to the superdivision image to be 0, namely setting the region with the superdivision image corresponding to the damage target region to be 0, for the original image, selecting the region with the pixel value of 255 in each pixel point in the target mask, finally integrally adding the processed original image and the superdivision image, wherein the region with the pixel value of 0 in the original image and the superdivision image are just not overlapped, namely the region with the pixel value of 0 in the original image does not change the value of the region corresponding to the superdivision image, and the region with the pixel value of 0 in the superdivision image does not change the value of the region corresponding to the original image.
The target photo (shown in figure 6) obtained by the JPEG photo restoration method can furthest utilize the reserved data to acquire and display the effective information as comprehensively as possible, and the photo detail is far better than the superresolution graph.
In one embodiment, the step S102 includes the steps of:
S10, reading an original graph by utilizing imread functions;
And S11, filling the lost pixels in the original graph by utilizing imread functions.
It should be noted that the imread function in this embodiment is a function in the opencv library, which is used to read the data in the picture file, and the gray value of the actual data loss may be filled with the imread function.
In an embodiment, before the step S103, the method includes the following steps:
S20, downsampling the original image by utilizing pyrDown functions to enable the size of the original image to reach a preset threshold;
S21, adjusting the size of the original image after downsampling to enable the size of the original image to be the same as that of the super-resolution image;
S22, converting the original graph into an HSV color space by utilizing cvtColor functions.
It should be noted that pyrDown is a downsampling operation, and the size of the resize is a size-adjusting operation, because the aspect ratio of the thumbnail (corresponding super-resolution image is the same) is not necessarily the same as that of the original image, if the thumbnail is directly resize to be combined, there may be a phenomenon that the boundary is obvious and cannot be found, so the application downsamples the original image through pyrDown function, the size of the original image is close to the size of the super-resolution image, or one or an integer multiple is larger than the super-resolution image, but the general multiple is not more than 4, and then the downsampled original image resize is up to the target size, namely the size of the super-resolution image, so as to avoid the problem that the boundary is not found when the thumbnail is combined.
In step S22, cvCvtColor is a color space conversion function in Opencv, which can realize conversion from RGB colors to HSV, HSI and other color spaces, and also can convert the RGB colors to gray images, and the parameters used in the present application are BRG2HSV, and it should be noted that the HSV color space is easy to quickly divide different color areas, and the RGB space is difficult to divide different color areas.
In one embodiment, the step S103 includes the steps of:
s30, acquiring an initial mask of the original graph by utilizing inRange functions;
S31, searching all gray areas of the initial mask by utilizing findContours functions;
S32, calculating the area of each gray area by utilizing contourArea functions;
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" can implement a binarization function, setting the pixel values within the two thresholds to white (255) and the pixel values not within the threshold section to black (0), thereby preliminarily dividing the gray region.
It should be noted that FindContuors, contourArea is a function in the opencv library, the former finds the outline of the possible region in the picture, and the latter calculates the area of each region, and thus finds the largest, i.e., the actual data loss portion. Considering that in an actual photograph, there is a small possibility that a large block of pure gray regions appear, the gray region with the largest area is taken as the damage target region.
In one embodiment, the step S104 includes:
and S40, filling the corresponding positions of the initial mask by utilizing drawContours functions, and updating the pixel value of each pixel point of the damage target area to 255.
In this embodiment, the drawContours functions are functions in the opencv library, and the noise points in the initial mask can be removed by filling drawContours functions in the corresponding positions of the initial mask, so as to obtain the target mask.
Referring to fig. 5, although most of the noise points in the initial mask have been removed, there is a residual at the boundary line, and if the target mask processed in step S40 is directly used for merging, the two parts of the boundary of the merged image will have gray lines with different degrees of depth, so in this embodiment, the following steps in step S104 include:
s50, performing boundary line correction processing on the target mask by utilizing dilate functions.
In this embodiment, dilate functions are functions in the opencv library, and the dilate functions can be used to slightly push the boundary line to the non-lost image portion, that is, the white area slightly expands, the black area slightly contracts, for example, after the graph of the white background and the black word is processed by using the dilate functions, the black word is thinned, so that the noise point of the boundary line of the present application is eliminated.
In one embodiment, the step S105 includes:
And S60, based on the recorded damage target area, updating the pixel value of each pixel point of the area corresponding to the damage target area in the coordination mask to be 0 by using a complexent function or bitwise _not function, and updating the pixel value of each pixel point of the rest area in the coordination mask to be 255.
In this embodiment, the complex function or bitwise _not function is a function in the opencv library, and the pixel value of each pixel point of the region corresponding to the damage target region in the cooperation mask can be updated to 0 through both functions, and the pixel value of each pixel point of the remaining region in the cooperation mask is updated to 255.
In one embodiment, the step S107 includes:
and S70, performing matrix addition on the updated superdivision graph and the original graph, and storing the superdivision graph and the original graph in a preset format by utilizing imwrite functions to obtain a target photo in a corresponding format.
It should be noted that imwrite functions are functions in opencv library, and the present application can save the target photo in various formats, such as jpg, jpeg, etc.
The embodiment of the invention also provides a device for repairing the JPEG photo, which is used for executing any embodiment of the method for repairing the JPEG photo. Specifically, referring to fig. 7, fig. 7 is a schematic block diagram of a repair device for a JPEG photograph according to an embodiment of the present invention.
As shown in fig. 7, a repair device 500 of a JPEG photograph includes:
the superdivision unit 501 extracts a thumbnail of the JPEG photo and performs superdivision processing on the thumbnail to obtain a superdivision diagram;
A filling unit 502, configured to read an original image of the JPEG photograph, fill lost pixels in the original image, and fill pixel values of each lost pixel point to 128;
A searching unit 503, configured to obtain an initial mask of the original image after filling, find and record a damage target area with the largest area in the initial mask, and set a pixel value of each pixel point in the initial mask to 0;
Updating unit 504, configured to fill the corresponding position of the initial mask after the position 0 is set based on the recorded damage target area, so that the pixel value of each pixel point of the damage target area is updated to 255, and a target mask is obtained;
A first conversion unit 505, configured to perform pixel value conversion on all pixels with pixel values of 0 and 255 in the target mask, convert the pixels with pixel values of 0 into pixels with pixel values of 255, and convert the pixels with pixel values of 255 into pixels with pixel values of 0, so as to obtain a matching mask;
A second converting unit 506, configured to update the pixel values of all the pixels in the region with the pixel value of 255 in the target mask corresponding to the original graph to be 0, and update the pixel values of all the pixels in the region with the pixel value of 255 in the matching mask corresponding to the super-resolution graph to be 0;
And an output unit 507 for matrix-adding the updated superdivision graph and the original graph, and outputting the target photo.
The target photo obtained by the device can be used for obtaining and displaying the effective information as comprehensively as possible by utilizing the reserved data to the greatest extent, and the photo detail is far better than the superresolution graph.
It will be clear to those skilled in the art that, for convenience and brevity of description, specific working procedures of the apparatus and units described above may refer to corresponding procedures in the foregoing method embodiments, which are not described herein again.
The repair device of a JPEG photograph as described above may be implemented in the form of a computer program which may 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 a stand-alone server or a server cluster formed by a plurality of servers.
With reference to FIG. 8, the computer device 1100 includes a processor 1102, memory, and a network interface 1105 connected through a system bus 1101, wherein the memory may include a non-volatile storage medium 1103 and an 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, causes the processor 1102 to perform a method for repairing a JPEG photograph.
The processor 1102 is operable to provide computing and control capabilities to support the operation of the overall computer device 1100.
The internal memory 1104 provides an environment for the execution of a computer program 11032 in the nonvolatile storage medium 1103, which computer program 11032, when executed by the processor 1102, causes the processor 1102 to execute a method for repairing a JPEG photograph.
The network interface 1105 is used for network communication such as providing transmission of data information, etc. It will be appreciated by those skilled in the art that the architecture shown in fig. 8 is merely a block diagram of some of the architecture relevant to the present inventive arrangements and is not limiting of the computer device 1100 to which the present inventive arrangements may be implemented, and that a particular computer device 1100 may include more or fewer components than shown, or may combine some of the components, or have a different arrangement of components.
Those skilled in the art will appreciate that the embodiment of the computer device shown in fig. 8 is not limiting of the specific construction of the computer device, and in other embodiments, the computer device may include more or less components than those shown, or certain components may be combined, or a different arrangement of components. For example, in some embodiments, the computer device may include only a memory and a processor, and in such embodiments, the structure and function of the memory and the processor are consistent with the embodiment shown in fig. 8, and will not be described again.
It should be appreciated that in an embodiment of the invention, the Processor 1102 may be a central processing unit (Central Processing Unit, CPU), the Processor 1102 may also be other general purpose processors, digital signal processors (DIGITAL SIGNAL processors, DSPs), application SPECIFIC INTEGRATED Circuits (ASICs), off-the-shelf Programmable gate arrays (Field-Programmable GATE ARRAY, FPGA) or other Programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, or the like. Wherein the 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 photograph according to an embodiment of the present invention.
The storage medium is a physical, non-transitory storage medium, and may be, for example, a U-disk, a removable hard disk, a Read-Only Memory (ROM), a magnetic disk, or an optical disk.
It will be clearly understood by those skilled in the art that, for convenience and brevity of description, specific working procedures of the apparatus, device and unit described above may refer to corresponding procedures in the foregoing method embodiments, which are not repeated herein.
While the invention has been described with reference to certain preferred embodiments, it will be understood by those skilled in the art that various changes and substitutions of equivalents may be made and equivalents will be apparent to those skilled in the art without departing from the scope of the invention. Therefore, the protection scope of the invention is subject to the protection scope of the claims.