How to realize the Image Restoration function of C++ OpenCV
This article is to share with you about C++ OpenCV how to achieve image restoration function, the editor feels very practical, so share with you to learn, I hope you can get something after reading this article, say no more, follow the editor to have a look.
Preface
Now we will use OpenCV C++ to repair the defective image. OpenCV provides inpaint API for image restoration.
1. OpenCV inpaint
The original picture is shown in the picture. The requirement of this case is to eliminate the red lines on the image. The inpaint API provided by OpenCV can achieve this effect.
Void inpaint (
InputArray src, original drawing
InputArray inpaintMask, binary mask, indicating the pixels to repair
OutputArray dst, target image
Double inpaintRadius, neighborhood painting around pixels. In general, if the area to be repaired is small, using smaller values produces less blur
Int flags INPAINT_NS or INPAINT_TELEA
)
The most important thing to use this API is to generate a binary mask image (the place to be repaired). We can use the mouse to respond to events.
/ draw a mask in inpaintMask using mouse response events to repair if (event = = EVENT_LBUTTONUP | |! (flags & EVENT_FLAG_LBUTTON)) {prevPt = Point (- 1,-1);} else if (event = = EVENT_LBUTTONDOWN) {prevPt = Point (x, y) } else if (event = = EVENT_MOUSEMOVE & & (flags & EVENT_FLAG_LBUTTON)) {prevPt = Point (x, y); / / draw mask image circle (inpaintMask, prevPt, 2, Scalar::all (255),-1); / / imshow ("inpaintMask", inpaintMask) / / Image restoration Mat result; inpaint (src, inpaintMask, result, 1, INPAINT_NS); / / imshow ("image", result);}
As shown in the picture, the mask image can then be repaired using inpaint.
Second, the source code # include#include # includeusing namespace cv;using namespace std;/*void inpaint (InputArray src, original image InputArray inpaintMask, binary mask, indicating the pixel OutputArray dst to be repaired, the target image double inpaintRadius, and the neighborhood painting around the pixel. In general, if the area to be repaired is small, using smaller values results in less blurred int flags INPAINT_NS or INPAINT_TELEA) * / Mat src, inpaintMask;Point prevPt (- 1,-1) Static void onMouse (int event, int x, int y, int flags, void*) {/ / use mouse to respond to events to draw a mask in inpaintMask for image repair if (event = = EVENT_LBUTTONUP | |! (flags & EVENT_FLAG_LBUTTON)) {prevPt = Point (- 1,-1) } else if (event = = EVENT_LBUTTONDOWN) {prevPt = Point (x, y);} else if (event = = EVENT_MOUSEMOVE & & (flags & EVENT_FLAG_LBUTTON)) {prevPt = Point (x, y) / draw mask images circle (inpaintMask, prevPt, 2, Scalar::all,-1); / imshow ("inpaintMask", inpaintMask); / / Image restoration Mat result; inpaint (src, inpaintMask, result, 1, INPAINT_NS); / / imshow ("image", result) }} int main () {src = imread ("girl.jpg"); if (src.empty ()) {cout