C++ 如何使用OpenCV在一个窗口中显示一个图像和一个视频
我在第页发现了一段代码。这是一个简洁的函数,它接受一个C++ 如何使用OpenCV在一个窗口中显示一个图像和一个视频,c++,opencv,C++,Opencv,我在第页发现了一段代码。这是一个简洁的函数,它接受一个char*和多个IplImage*参数作为输入,然后在一个窗口中显示所有图像 我想对一张图片和一个视频做同样的事情。因此,除了更改主函数以初始化web cam输入,并修改对上述函数的调用之外,我还需要更改什么 我的主要职能是: int main() { IplImage *img1 = cvLoadImage("image1.png"); CvCapture* capture = cvCaptureFromCAM(0);
char*
和多个IplImage*
参数作为输入,然后在一个窗口中显示所有图像
我想对一张图片和一个视频做同样的事情。因此,除了更改主函数以初始化web cam输入,并修改对上述函数的调用之外,我还需要更改什么
我的主要职能是:
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;
}
void cvShowManyImages(字符*标题,整数字符,…){
//img-用于获取参数
IplImage*img;
//DispImage—要在其中复制输入图像的图像
IplImage*显示图像;
整数大小;
int i;
int m,n;
int x,y;
//w-一行中的最大图像数
//h-一列中的最大图像数
int w,h;
//缩放-我们必须调整图像的大小
浮标;
int max;
//如果参数数小于0或大于12
//返回而不显示
如果(nArgs 12){
printf(“参数数量太大……\n”);
返回;
}
//确定图像的大小,
//以及行/列的数量
//从参数的数量
else if(nArgs==1){
w=h=1;
尺寸=300;
}
else if(nArgs==2){
w=2;h=1;
尺寸=300;
}
else if(nArgs==3 | | nArgs==4){
w=2;h=2;
尺寸=300;
}
else if(nArgs==5 | | nArgs==6){
w=3;h=2;
尺寸=200;
}
else if(nArgs==7 | | nArgs==8){
w=4;h=2;
尺寸=200;
}
否则{
w=4;h=3;
尺寸=150;
}
//创建新的3通道图像
DispImage=cvCreateImage(cvSize(100+尺寸*w,60+尺寸*h),8,3);
cvZero(disprime);
//用于获取传递的参数
va_列表参数;
va_启动(args、nArgs);
//nArgs参数数的循环
对于(i=0,m=20,n=20;iwidth;
y=img->高度;
//查找高度或宽度是否更大,以便调整图像大小
最大值=(x>y)?x:y;
//查找缩放因子以调整图像大小
比例=(浮动)((浮动)最大/尺寸);
//用于对齐图像
如果(i%w==0&&m!=20){
m=20;
n+=20+尺寸;
}
//设置图像ROI以显示当前图像
cvSetImageROI(DispImage,cvRect(m,n,(int)(x/scale),(int)(y/scale));
//调整输入图像的大小并将其复制到单个大图像
cvResize(img、DispImage);
//重置ROI以显示下一幅图像
cvResetImageROI(DispImage);
}
//创建一个新窗口,并显示单个大图像
//cvNamedWindow(标题1);
cvShowImage(标题、显示图像);
//cvWaitKey(20);
//结束参数的数量
va_端(args);
//释放图像内存
cvReleaseImage(&DispImage);
}
int main(){
IplImage*img1=cvLoadImage(“d:\\ImagesForTest\\cat.bmp”);
CvCapture*capture=cvCaptureFromCAM(0);
如果(!捕获){
fprintf(stderr,“错误:捕获为空\n”);
getchar();
返回-1;
}
//创建一个显示捕获图像的窗口
cvNamedWindow(“mywindow”,CV_WINDOW_AUTOSIZE);
//在窗口中显示从相机捕获的图像,然后重复
而(1){
//得到一帧
IplImage*frame=cvQueryFrame(捕获);
如果(!帧){
fprintf(stderr,“错误:帧为空…\n”);
getchar();
打破
}
//cvShowImage(“我的窗口”,框架);
cvShowManyImages(“图像”,2,img1,帧);
//不要松开框架!
//如果按下ESC键,则在OpenCV 0.9.7(linux版本)下的键=0x10001B,
//使用AND运算符删除高位
如果((cvWaitKey(10)&255)==27)中断;
}
//释放捕获设备
cvReleaseCapture(&capture);
cvallwindows();
返回0;
}
是否尝试将帧复制到新图像并绘制两幅图像?我想看到视频与图像一起播放。