如何使用OpenCV在一个窗口中显示一个图像和一个视频
How to display one image and one video in one window using OpenCV
我在这个页面上看到了一段代码。这是一个简洁的函数,它以一个char*
和多个IplImage*
参数作为输入,然后在一个窗口中显示所有图像。
我想对一个图像和一个视频做同样的事情。那么,除了更改主函数以初始化网络摄像头输入,并修改对上述函数的调用外,我还需要更改什么?
我的主要功能是:
int main() {
IplImage *img1 = cvLoadImage("image1.png");
CvCapture* capture = cvCaptureFromCAM(0);
if ( !capture ) {
fprintf( stderr, "ERROR: capture is NULL n" );
getchar();
return -1;
}
// Create a window in which the captured images will be presented
cvNamedWindow( "mywindow", CV_WINDOW_AUTOSIZE );
// Show the image captured from the camera in the window and repeat
while ( 1 ) {
// Get one frame
IplImage* frame = cvQueryFrame( capture );
if ( !frame ) {
fprintf( stderr, "ERROR: frame is null...n" );
getchar();
break;
}
//cvShowImage( "mywindow", frame );
cvShowManyImages("Image", 2, img1, frame);
// Do not release the frame!
//If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version),
//remove higher bits using AND operator
if ( (cvWaitKey(10) & 255) == 27 ) break;
}
// Release the capture device housekeeping
cvReleaseCapture( &capture );
cvDestroyWindow( "mywindow" );
return 0;
}
问题是,它只显示视频的第一帧。我需要继续按esc
来获得下一帧。
这可以根据您的需要工作:
void cvShowManyImages(char* title, int nArgs, ...) {
// img - Used for getting the arguments
IplImage *img;
// DispImage - the image in which input images are to be copied
IplImage *DispImage;
int size;
int i;
int m, n;
int x, y;
// w - Maximum number of images in a row
// h - Maximum number of images in a column
int w, h;
// scale - How much we have to resize the image
float scale;
int max;
// If the number of arguments is lesser than 0 or greater than 12
// return without displaying
if(nArgs <= 0) {
printf("Number of arguments too small....n");
return;
}
else if(nArgs > 12) {
printf("Number of arguments too large....n");
return;
}
// Determine the size of the image,
// and the number of rows/cols
// from number of arguments
else if (nArgs == 1) {
w = h = 1;
size = 300;
}
else if (nArgs == 2) {
w = 2; h = 1;
size = 300;
}
else if (nArgs == 3 || nArgs == 4) {
w = 2; h = 2;
size = 300;
}
else if (nArgs == 5 || nArgs == 6) {
w = 3; h = 2;
size = 200;
}
else if (nArgs == 7 || nArgs == 8) {
w = 4; h = 2;
size = 200;
}
else {
w = 4; h = 3;
size = 150;
}
// Create a new 3 channel image
DispImage = cvCreateImage( cvSize(100 + size*w, 60 + size*h), 8, 3 );
cvZero(DispImage);
// Used to get the arguments passed
va_list args;
va_start(args, nArgs);
// Loop for nArgs number of arguments
for (i = 0, m = 20, n = 20; i < nArgs; i++, m += (20 + size)) {
// Get the Pointer to the IplImage
img = va_arg(args, IplImage*);
// Check whether it is NULL or not
// If it is NULL, release the image, and return
if(img == 0) {
printf("Invalid arguments");
cvReleaseImage(&DispImage);
return;
}
// Find the width and height of the image
x = img->width;
y = img->height;
// Find whether height or width is greater in order to resize the image
max = (x > y)? x: y;
// Find the scaling factor to resize the image
scale = (float) ( (float) max / size );
// Used to Align the images
if( i % w == 0 && m!= 20) {
m = 20;
n+= 20 + size;
}
// Set the image ROI to display the current image
cvSetImageROI(DispImage, cvRect(m, n, (int)( x/scale ), (int)( y/scale )));
// Resize the input image and copy the it to the Single Big Image
cvResize(img, DispImage);
// Reset the ROI in order to display the next image
cvResetImageROI(DispImage);
}
// Create a new window, and show the Single Big Image
// cvNamedWindow( title, 1 );
cvShowImage( title, DispImage);
// cvWaitKey(20);
// End the number of arguments
va_end(args);
// Release the Image Memory
cvReleaseImage(&DispImage);
}
int main() {
IplImage *img1 = cvLoadImage("d:\ImagesForTest\cat.bmp");
CvCapture* capture = cvCaptureFromCAM(0);
if ( !capture ) {
fprintf( stderr, "ERROR: capture is NULL n" );
getchar();
return -1;
}
// Create a window in which the captured images will be presented
cvNamedWindow( "mywindow", CV_WINDOW_AUTOSIZE );
// Show the image captured from the camera in the window and repeat
while ( 1 ) {
// Get one frame
IplImage* frame = cvQueryFrame( capture );
if ( !frame ) {
fprintf( stderr, "ERROR: frame is null...n" );
getchar();
break;
}
//cvShowImage( "mywindow", frame );
cvShowManyImages("Image", 2, img1, frame);
// Do not release the frame!
//If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version),
//remove higher bits using AND operator
if ( (cvWaitKey(10) & 255) == 27 ) break;
}
// Release the capture device housekeeping
cvReleaseCapture( &capture );
cvDestroyAllWindows();
return 0;
}
相关文章:
- 输入值后,如何在一个括号下显示值,例如"{6,7,8,9}
- 仅显示链表的最后一个元素
- 在 QListView 中显示最后一个元素
- 我在C++中编写了一个方法来打印树类的预序,但它显示了分割错误
- 类中的一个变量显示,但另一个不显示
- 我编写了一个程序来显示数组的内容,但它显示的是它的地址
- 为什么断点显示数组的第二个值是一个大数字?额外学分工作
- 我已经做了一个程序来使用队列链C++表显示学生姓名和身高,现在我想以我不知道如何显示的顺序显示它
- 螺旋矩阵在最后一个"for"循环中中断,不显示任何内容
- 我有一个嵌套数组,它由另一个数组中的元素组成,这些元素分组为2.之后,我想显示每个元素的属性
- 当被一个输入分开时,有两个控制台输出同时显示
- 如何使QLineSeries/QXYSeries仅显示一个点标签
- 如何在 for 循环中显示一个结果
- 在QT C++编辑Qtable模型数据时,我需要得到一个小盒子,我需要显示编辑文本,这怎么可能?
- 如何在QListWidget中显示QStringList的每个项目QLabel和另一个QString?
- 我在视觉工作室中制作了一个类似胭脂的游戏,我想显示玩家的生命值
- C++显示两个区间之间的数字的程序检查一个数字是否可以表示为两个素数的总和
- Gtkmm - 关闭窗口然后显示另一个窗口的正确方法
- 将终端设置为原始模式:仅在按下下一个键后显示字符
- 你如何制作一个显示 24、60 和 1 的向量