Disclosure of Invention
The present invention is directed to solve the deficiencies of the prior art, and provides an image filtering method, an image filtering device and a storage medium for preserving image edges, which can obtain a restored image with complete edges.
In order to achieve the purpose, the invention adopts the following technical scheme:
an image filtering method for preserving image edges is provided, comprising:
step 101, acquiring a target image, extracting a contour line in the target image, and forming a first image by the extracted contour line and an empty template image with the same size as an original image;
102, performing image restoration on a target image to obtain a second image;
103, performing image enhancement processing on the repaired second image to obtain a third image;
and 104, performing image superposition on the first image and the third image to obtain a fourth image, and outputting the fourth image as a target image.
Further, the above-mentioned manner of extracting the contour line in the target image is to extract the image contour feature by a Canny operator, and specifically includes the following steps:
step 201, performing image smoothing processing on a target image by using a Gaussian filter;
step 202, calculating the amplitude and direction of the gradient of the smoothed image by using finite difference of first-order partial derivatives;
step 203, carrying out non-maximum suppression processing on the gradient amplitude of the smoothed image;
and step 204, carrying out double-threshold algorithm detection and connection edge processing on the image processed in the step 203.
Further, the above-mentioned manner of performing image restoration on the target image to obtain the second image specifically includes the following:
step 301, representing a complete region of a target image by P, determining that Q is a region to be repaired of the target image, and then P-Q is a target region which does not need to be repaired;
step 302, calculating each pixel point a at the edge of the region Q to be repaired of the target image, and calculating a block M to be repaired with the pixel point a as the centeraRepair priority of (a):
A(a)=C(a)D(a),
wherein C (a) designs the block E to be repairedaD (a) is the block E to be repairedaThe data factor item of (1), whereinIn the formula | MaL is the block M to be repairedaα is a normalization factor;a unit vector which is perpendicular to the edge of the area Q to be repaired at the point a is represented;
step 303, finding out pixel points corresponding to the MAX a (a) value from the edge points of the area Q to be repaired, repairing the block to be repaired with the pixel points corresponding to the MAX a (a) value as the center, and simultaneously repairing the repaired block M to be repairedaThe repair priority of (1) is set to 0;
and step 304, repeating the step 303 until the area Q to be repaired is completely repaired to obtain a second image.
Further, the repairing process in step 303 includes the following steps:
step 401, calculate the block M to be repairedaThe gray value of the known pixel and the Euclidean distance d of the gray value in the corresponding block in the image which does not need to be repaired, and the block with the minimum Euclidean distance is taken as the block M to be repairedaMost matched image block Na;
Step 402, according to the best matching image block NaGray value of middle pixel point to block M to be repairedaThe gray values in the pixel points are filled, and after the filling is finished, the gray values are ready to be filledRepair block MaMarking as repair-free areas;
and step 403, processing the image processed in the step 402 again according to the steps 401 to 402 until the area to be repaired is completely repaired.
Further, the specific manner of superimposing the first image and the third image to obtain the fourth image includes the following steps:
the first image and the third image are subjected to an image superimposing operation by matlab, and the RGB values of the blank template image of the first image excluding the extracted contour line portion are set to 0.
An image filtering apparatus for preserving image edges is also provided, to which the image filtering method for preserving image edges is applied, including:
the contour line extraction module is used for extracting contour lines in the target image and enabling the extracted contour lines and the blank template image with the same size as the original image to jointly form a first image;
the image restoration module is used for restoring the target image to obtain a second image;
the image enhancement module is used for carrying out image enhancement processing on the repaired second image to obtain a third image;
and the image superposition module is used for carrying out image superposition on the first image and the third image to obtain a fourth image, and outputting the fourth image as a target image.
Further, the image inpainting module includes:
a to-be-repaired area determining subunit, configured to determine a to-be-repaired area of the target image;
the pixel point calculation subunit is used for calculating each pixel point a of the edge of the region Q to be repaired of the target image;
a repair priority calculation subunit for calculating a block M to be repaired centered on the pixel point aaRepair priority a.
A computer-readable storage medium is also proposed, in which a computer program is stored which, when being executed by a processor, carries out the steps of the image filtering method preserving image edges.
The invention has the beneficial effects that:
according to the invention, the image edge is extracted, and then the edge image of the image and the repaired image are subjected to image superposition to obtain the repaired image with complete edge, so that the damage to the image edge in the image repairing process can be avoided, and the clear and complete repaired image can be obtained.
Detailed Description
The conception, the specific structure and the technical effects of the present invention will be clearly and completely described in conjunction with the embodiments and the accompanying drawings to fully understand the objects, the schemes and the effects of the present invention. It should be noted that the embodiments and features of the embodiments in the present application may be combined with each other without conflict. The same reference numbers will be used throughout the drawings to refer to the same or like parts.
With reference to fig. 1, the present invention provides an image filtering method for preserving image edges, including:
step 101, acquiring a target image, extracting a contour line in the target image, and forming a first image by the extracted contour line and an empty template image with the same size as an original image;
102, performing image restoration on a target image to obtain a second image;
103, performing image enhancement processing on the repaired second image to obtain a third image;
and 104, performing image superposition on the first image and the third image to obtain a fourth image, and outputting the fourth image as a target image.
According to the invention, the image edge is extracted, and then the edge image of the image and the repaired image are subjected to image superposition to obtain the repaired image with complete edge, so that the damage to the image edge in the image repairing process can be avoided, and the clear and complete repaired image is output.
As a preferred embodiment of the present invention, the method for extracting the contour line in the target image is to extract the image contour feature by using a Canny operator, and specifically includes the following steps:
step 201, performing image smoothing processing on a target image by using a Gaussian filter;
step 202, calculating the amplitude and direction of the gradient of the smoothed image by using finite difference of first-order partial derivatives;
step 203, carrying out non-maximum suppression processing on the gradient amplitude of the smoothed image;
and step 204, carrying out double-threshold algorithm detection and connection edge processing on the image processed in the step 203.
According to the method, the Canny operator is adopted to extract the edge of the contour line of the target image, and the Canny improves the non-maximum suppression and the double threshold on the basis of the first-order differential operator. By utilizing non-maximum suppression, not only can the multi-response edge be effectively suppressed, but also the positioning accuracy of the edge can be improved; the missing rate of the edge can be effectively reduced by using the double thresholds.
As a preferred embodiment of the present invention, the method for performing image restoration on the target image to obtain the second image specifically includes the following steps:
step 301, representing a complete region of a target image by P, determining that Q is a region to be repaired of the target image, and then P-Q is a target region which does not need to be repaired;
step 302, calculating each pixel point a at the edge of the region Q to be repaired of the target image, and calculating a block M to be repaired with the pixel point a as the centeraRepair priority of (a):
A(a)=C(a)D(a),
wherein C (a) designs the block E to be repairedaD (a) is the block E to be repairedaThe data factor item of (1), whereinIn the formula | MaL is the block M to be repairedaα is a normalization factor;a unit vector which is perpendicular to the edge of the area Q to be repaired at the point a is represented;
step 303, finding out pixel points corresponding to the MAX a (a) value from the edge points of the area Q to be repaired, repairing the block to be repaired with the pixel points corresponding to the MAX a (a) value as the center, and simultaneously repairing the repaired block M to be repairedaThe repair priority of (1) is set to 0;
and step 304, repeating the step 303 until the area Q to be repaired is completely repaired to obtain a second image.
According to the embodiment, the repairing priority is calculated, and the repairing blocks in the area to be repaired are sequentially repaired according to the priority, so that the repairing accuracy can be ensured to a certain extent.
As a preferred embodiment of this embodiment, the repairing process in step 303 includes the following steps:
step 401, calculate the block M to be repairedaThe gray value of the known pixel and the Euclidean distance d of the gray value in the corresponding block in the image which does not need to be repaired, and the block with the minimum Euclidean distance is taken as the block M to be repairedaMost matched image block Na;
Step 402, according to the best matching image block NaGray value of middle pixel point to block M to be repairedaThe gray value of the pixel point is filled, and after the filling is finished, the block M to be repaired isaMarking as repair-free areas;
and step 403, processing the image processed in the step 402 again according to the steps 401 to 402 until the area to be repaired is completely repaired.
In the present embodiment, the closest euclidean distance is calculated, and the point in the repair-unnecessary region of the euclidean distance is used as the most matched image block NaGray value of middle pixel point to block M to be repairedaImage ofThe gray values in the pixel points are filled, so that the continuity of the image can be ensured to a certain extent.
As a preferred embodiment of the present invention, the specific manner of superimposing the first image and the third image to obtain the fourth image includes the following:
the first image and the third image are subjected to an image superimposing operation by matlab, and the RGB values of the blank template image of the first image excluding the extracted contour line portion are set to 0.
In the present embodiment, when superimposing the first image and the third image, the RGB values of the blank template image excluding the extracted contour line portion of the first image are set to 0 at the same time, and it is possible to avoid a situation where the first image and the third image overlap each other at a position other than the contour line.
An image filtering apparatus for preserving image edges is also provided, to which the image filtering method for preserving image edges is applied, including:
the contour line extraction module is used for extracting contour lines in the target image and enabling the extracted contour lines and the blank template image with the same size as the original image to jointly form a first image;
the image restoration module is used for restoring the target image to obtain a second image;
the image enhancement module is used for carrying out image enhancement processing on the repaired second image to obtain a third image;
and the image superposition module is used for carrying out image superposition on the first image and the third image to obtain a fourth image, and outputting the fourth image as a target image.
As a preferred embodiment of this aspect, the image restoration module includes:
a to-be-repaired area determining subunit, configured to determine a to-be-repaired area of the target image;
the pixel point calculation subunit is used for calculating each pixel point a of the edge of the region Q to be repaired of the target image;
a repair priority calculation subunit for calculating a block M to be repaired centered on the pixel point aaRepair priority a.
A computer-readable storage medium is also proposed, in which a computer program is stored which, when being executed by a processor, carries out the steps of the image filtering method preserving image edges.
The modules described as separate parts may or may not be physically separate, and parts displayed as modules may or may not be physical modules, may be located in one place, or may be distributed on a plurality of network modules. Some or all of the modules may be selected according to actual needs to achieve the purpose of the solution of the present embodiment.
In addition, functional modules in the embodiments of the present invention may be integrated into one processing module, or each of the modules may exist alone physically, or two or more modules are integrated into one module. The integrated module can be realized in a hardware mode, and can also be realized in a software functional module mode.
The integrated module, if implemented in the form of a software functional module and sold or used as a stand-alone product, may be stored in a computer readable storage medium. Based on such understanding, all or part of the flow of the method according to the embodiments of the present invention may also be implemented by a computer program, which may be stored in a computer-readable storage medium and can implement the steps of the above-described method embodiments when the computer program is executed by a processor. Wherein the computer program comprises computer program code, which may be in the form of source code, object code, an executable file or some intermediate form, etc. The computer-readable medium may include: any entity or device capable of carrying the computer program code, recording medium, usb disk, removable hard disk, magnetic disk, optical disk, computer Memory, Read-Only Memory (ROM), Random Access Memory (RAM), electrical carrier wave signals, telecommunications signals, software distribution medium, etc. It should be noted that the computer readable medium may contain other components which may be suitably increased or decreased as required by legislation and patent practice in jurisdictions, for example, in some jurisdictions, computer readable media which may not include electrical carrier signals and telecommunications signals in accordance with legislation and patent practice.
While the present invention has been described in considerable detail and with particular reference to a few illustrative embodiments thereof, it is not intended to be limited to any such details or embodiments or any particular embodiments, but it is to be construed as effectively covering the intended scope of the invention by providing a broad, potential interpretation of such claims in view of the prior art with reference to the appended claims. Furthermore, the foregoing describes the invention in terms of embodiments foreseen by the inventor for which an enabling description was available, notwithstanding that insubstantial modifications of the invention, not presently foreseen, may nonetheless represent equivalent modifications thereto.
The above description is only a preferred embodiment of the present invention, and the present invention is not limited to the above embodiment, and the present invention shall fall within the protection scope of the present invention as long as the technical effects of the present invention are achieved by the same means. The invention is capable of other modifications and variations in its technical solution and/or its implementation, within the scope of protection of the invention.