Skip to content

Commit 8c88b4a

Browse files
committed
Restauración de Imagen
Uso de las funciones OpenCV floodfill() + inpaint() para restaurar una imagen dañada.
1 parent ff33dc4 commit 8c88b4a

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

restaurar-imagen/CMakeLists.txt

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
cmake_minimum_required(VERSION 2.8)
2+
3+
project( Restauracion_Inpaint )
4+
5+
find_package( OpenCV REQUIRED )
6+
7+
file(COPY baboon.jpg DESTINATION data)
8+
9+
add_executable( Restauracion_Inpaint restauracion.cpp )
10+
target_link_libraries( Restauracion_Inpaint ${OpenCV_LIBS} )

restaurar-imagen/baboon.jpg

189 KB
Loading

restaurar-imagen/restauracion.cpp

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#include <opencv2\opencv.hpp>
2+
#include <iostream>
3+
4+
using namespace cv;
5+
using namespace std;
6+
7+
Mat image, original;
8+
9+
int connectivity = 4;
10+
int newMaskVal = 255;
11+
12+
static void onMouse(int event, int x, int y, int, void*)
13+
{
14+
if (event != EVENT_LBUTTONDOWN) return;
15+
16+
// obtener el punto donde se hizo clic
17+
Point seed = Point(x, y);
18+
19+
int ffillMode = 1;
20+
int flags = connectivity + (newMaskVal << 8) + (ffillMode == 1 ? FLOODFILL_FIXED_RANGE : 0);
21+
int b = (unsigned)theRNG() & 255;
22+
int g = (unsigned)theRNG() & 255;
23+
int r = (unsigned)theRNG() & 255;
24+
int lo = 20, up = 20;
25+
26+
Rect ccomp;
27+
28+
// generar un color de manera aleatoria
29+
Scalar newVal = Scalar(r * 0.299 + g * 0.587 + b * 0.114);
30+
31+
Mat dst, mask, inpaint_mask;
32+
33+
// inicializar el Mat mask
34+
mask.create(image.rows + 2, image.cols + 2, CV_8UC1);
35+
mask = Scalar::all(0);
36+
37+
// obtener el mask para reparar la imagen
38+
floodFill(image, mask, seed, newVal, &ccomp,
39+
Scalar(lo, lo, lo),
40+
Scalar(up, up, up), flags);
41+
42+
// obtener inpaintMask y restaurar la imagen
43+
inpaint_mask = Mat(mask, Rect(1, 1, mask.cols - 2, mask.rows - 2));
44+
inpaint(image, inpaint_mask, dst, 3, INPAINT_NS);
45+
46+
imshow("image", image);
47+
imshow("mask", mask);
48+
imshow("inpaint", dst);
49+
50+
original.copyTo(image);
51+
}
52+
53+
54+
int main(int argc, char** argv)
55+
{
56+
char* filename = argc >= 2 ? argv[1] : (char*)"data/baboon.jpg";
57+
image = imread(filename, 1);
58+
59+
if (image.empty())
60+
{
61+
cout << "No se puede abrir: data/baboon.jpg" << endl;
62+
system("pause");
63+
return 0;
64+
}
65+
66+
// guardar una copia de la imagen original
67+
image.copyTo(original);
68+
69+
namedWindow("image", 0);
70+
namedWindow("mask", 0);
71+
namedWindow("inpaint", 0);
72+
73+
// responder a eventos del raton
74+
setMouseCallback("image", onMouse, 0);
75+
76+
for (;;)
77+
{
78+
imshow("image", image);
79+
80+
int c = waitKey(0);
81+
if ((c & 255) == 27)
82+
{
83+
cout << "Exiting ...\n";
84+
break;
85+
}
86+
}
87+
88+
return 0;
89+
}

0 commit comments

Comments
 (0)