Skip to content

Commit cde9e62

Browse files
committedJul 3, 2016
OpenCV Image Viewer
Permite mostrar una o varias imágenes en la misma ventana, las imágenes son ubicadas en celdas cuadradas.
1 parent 1aa8343 commit cde9e62

File tree

5 files changed

+123
-0
lines changed

5 files changed

+123
-0
lines changed
 

‎data/baboon.jpg

32.9 KB
Loading

‎data/butterfly.jpg

31.5 KB
Loading

‎multiple-image/CMakeLists.txt

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
cmake_minimum_required(VERSION 3.4)
2+
3+
project( MultipleImageViewer )
4+
5+
find_package( OpenCV 3.0.0 REQUIRED )
6+
7+
file(COPY ../data/lena.jpg ../data/butterfly.jpg ../data/baboon.jpg DESTINATION image)
8+
9+
add_executable( ${PROJECT_NAME} source.cpp )
10+
target_link_libraries( ${PROJECT_NAME} ${OpenCV_LIBS} )

‎multiple-image/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Mostrar Varias Imagenes en una Misma Ventana
2+
Veremos como mesclar varios cv::Mat para combinar un grupo de imágenes que mostraremos en una sola ventana, esto nos permitirá desplegar varias imágenes utilizando solo una ventana.
3+
4+
La documentación está disponible en [Tutoriales OpenCV](http://acodigo.blogspot.com/p/tutorial-opencv.html)

‎multiple-image/source.cpp

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#include <iostream>
2+
#include <cstdarg>
3+
#include <opencv2/opencv.hpp>
4+
5+
using namespace cv;
6+
using namespace std;
7+
8+
void getSquareImage(cv::InputArray img, cv::OutputArray dst, int size)
9+
{
10+
if (size < 2) size = 2;
11+
int width = img.cols(), height = img.rows();
12+
13+
cv::Mat square = dst.getMat();
14+
15+
// si la imagen es cuadrada solo redimensionar
16+
if (width == height) {
17+
cv::resize(img, square, Size(size, size));
18+
return;
19+
}
20+
21+
// establecer color de fondo del cuadrante
22+
square.setTo(Scalar::all(0));
23+
24+
int max_dim = (width >= height) ? width : height;
25+
float scale = ((float)size) / max_dim;
26+
27+
// calcular la region centrada
28+
cv::Rect roi;
29+
30+
if (width >= height)
31+
{
32+
roi.width = size;
33+
roi.x = 0;
34+
roi.height = (int)(height * scale);
35+
roi.y = (size - roi.height) / 2;
36+
}
37+
else
38+
{
39+
roi.y = 0;
40+
roi.height = size;
41+
roi.width = (int)(width * scale);
42+
roi.x = (size - roi.width) / 2;
43+
}
44+
45+
// redimensionar imagen en la region calculada
46+
cv::resize(img, square(roi), roi.size());
47+
}
48+
49+
void showImages(const String& window_name, int rows, int cols, int size, std::initializer_list<const Mat*> images, int pad = 1)
50+
{
51+
if (pad <= 0) pad = 0;
52+
53+
int width = size * cols + ((cols + 1) * pad);
54+
int height = size * rows + ((rows + 1) * pad);
55+
56+
// crear la imagen de salida con un color de fondo blanco
57+
Mat dst = Mat(height, width, CV_8UC3, Scalar::all(255));
58+
59+
int x = 0, y = 0, cols_counter = 0, img_counter = 0;
60+
61+
// recorrer la lista de imagenes
62+
for (auto& img : images) {
63+
Mat roi = dst(Rect(x + pad, y + pad, size, size));
64+
65+
// dibujar la imagen en el cuadrante indicado
66+
getSquareImage(*img, roi, size);
67+
68+
// avanzar al siguiente cuadrante
69+
x += roi.cols + pad;
70+
71+
// avanza a la siguiente fila
72+
if (++cols_counter == cols) {
73+
cols_counter = x = 0;
74+
y += roi.rows + pad;
75+
}
76+
77+
// detener si no hay mas cuadrantes disponibles
78+
if (++img_counter >= rows * cols) break;
79+
}
80+
81+
imshow(window_name, dst);
82+
}
83+
84+
int main(int argc, char** argv)
85+
{
86+
Mat image0 = imread("image/lena.jpg", CV_LOAD_IMAGE_COLOR);
87+
Mat image1 = imread("image/baboon.jpg", CV_LOAD_IMAGE_COLOR);
88+
Mat image2 = imread("image/butterfly.jpg", CV_LOAD_IMAGE_COLOR);
89+
90+
Mat image4;
91+
cvtColor(image0, image4, CV_BGR2GRAY);
92+
cvtColor(image4, image4, CV_GRAY2BGR);
93+
94+
Mat image5;
95+
filter2D(image1, image5, image5.depth(), Mat::eye(2, 2, CV_8UC1));
96+
97+
Mat image6;
98+
Sobel(image2, image6, -1, 1, 1);
99+
100+
if (!image0.empty() || !image1.empty() || !image2.empty())
101+
{
102+
namedWindow("Display Multiple Image", WINDOW_AUTOSIZE);
103+
showImages("Display Multiple Image", 2, 3, 240, { &image0, &image1, &image2, &image4, &image5, &image6 }, 5);
104+
}
105+
else cout << "No image data." << endl;
106+
107+
waitKey(0);
108+
return 0;
109+
}

0 commit comments

Comments
 (0)
Please sign in to comment.