/* webcam_escalada.c -- captura de imágenes y escalado de imagen
Copyright (C) 2010 Oscar García
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
Para compilar:
gcc webcam_escalada.c -o webcam_escalada `pkg-config --cflags --libs opencv`
*/
#include
#include
#include
#include
#include
int main(int argc, char *argv[])
{
CvCapture *dispositivo = NULL;
IplImage *fotograma = NULL, *escalada = NULL;
int tecla = 0, ndispositivo = CV_CAP_ANY;
CvSize tamanyo;
/* Analizamos los parámetros de entrada */
if (argc > 4 || argc < 3) {
printf("Uso: %s [#dispositivo]\n", (char*)basename(argv[0]));
return 1;
} else if (argc == 4) {
ndispositivo = atoi(argv[3]);
}
/* Calculamos el ancho y el alto de la imagen escalada */
tamanyo.width = atoi(argv[1]);
tamanyo.height = atoi(argv[2]);
if (tamanyo.width == 0 || tamanyo.height == 0) {
printf("ERROR: Especifique un ancho o alto > 0\n");
return 1;
}
/* Inicializamos el dispositivo de captura y comprobamos el estado */
printf("Inicializando dispositivo de captura #%d\n", ndispositivo);
dispositivo = cvCaptureFromCAM(ndispositivo);
if (dispositivo == NULL) {
fprintf(stderr, "Error inicializando dispositivo de captura #%d\n",
ndispositivo);
return 1;
}
/* Creamos la imagen con el tamaño deseado */
printf("Creando imagen escalada de %dx%d\n", tamanyo.width, tamanyo.height);
escalada = cvCreateImage(tamanyo, IPL_DEPTH_8U, 3);
if (escalada == NULL) {
fprintf(stderr, "Error creando imagen escalada\n");
return 1;
}
/* Creamos una ventana para mostrar el contenido de la webcam */
printf("Creando ventana\n");
cvNamedWindow("Fotograma", CV_WINDOW_AUTOSIZE);
/* Entramos en un bucle infinito del que saldremos al pulsar ESC */
printf("Entrando en el bucle principal (ESC para salir)\n");
while(tecla != 27) {
printf(".");
fflush(stdout);
/* Leemos un fotograma y comprobamos que la lectura haya sido correcta */
fotograma = cvQueryFrame(dispositivo);
if(fotograma == NULL) {
break;
}
/* Escalamos el fotograma capturado al tamaño de la imagen de salida */
cvResize(fotograma, escalada, CV_INTER_LINEAR);
/* Mostramos la imagen escalada y esperamos la pulsación de una tecla 100 ms */
cvShowImage("Fotograma", escalada);
tecla = cvWaitKey(100);
}
/* Antes de salir liberamos los recursos creados y/o usados */
printf("\nLiberando recursos\n");
cvDestroyWindow("Fotograma");
cvReleaseCapture(&dispositivo);
return 0;
}