访问Ximea相机并使用OpenCV设置预定义的分辨率时,由于相机默认分辨率的大小为Mat,因此显示混乱的输出

Accessing Ximea camera and setting a predefined resolution with OpenCV shows puzzled output, due to Mat in size of camera’s default resolution

本文关键字:分辨率 相机 默认 小为 Mat 混乱 输出 显示 Ximea OpenCV 设置      更新时间:2023-10-16

>问题描述:

使用简单的代码,我可以访问我的内部摄像头网络摄像头并更改分辨率。因此,以默认分辨率和预定义分辨率(例如,从 640x480 到 320x240,使用 cap.set(CV_CAP_PROP_FRAME_WIDTH,320( 和 FRAME_HEIGHT, 240((显示帧 – 两者都可以正常工作。

使用相同的代码稍作调整,以便它适用于Ximea相机(VideoCapture cap(CV_CAP_XIAPI((,确实适用于默认分辨率。它是默认分辨率为 2592x1944 的MU9PC_MH,因此 648 x486 是所需的较低分辨率。

对于手动设置的分辨率,显示的窗口

/抓取帧的大小为默认分辨率(2592x1944(,并在此巨大的显示器中显示捕获的较低像素量,因此上五分之一填充了困惑的像素,窗口的其余部分是黑色的。

这种效果发生在两者上,C++ VideoCapture 和 Mat 以及 C CvCaptureFromCAM 和 IplImage* 中。

当我使用设置的标志CV_CAP_PROP_XI_DOWNSAMPLING时,我可以以正确的顺序看到带有像素的输出图像,但显示帧仍然具有默认的高分辨率,并且输出图像显示多次(该因子取决于我用于缩减采样的因子(。

我什至无法将 Mat 或 IplImage 强制为预定义的大小,因为这样就会发生断言错误或访问冲突(Mat image(XRES, YRES, CV_8UC3, Scalar(0,0,255(( (frame = cvCreateImage(cvSize( XRES, YRES(, IPL_DEPTH_8U,3(;.我已经通过frame.type((检查了输出是否是一个CV_8UC3

我想要的是 648x486 的 Ximea 相机输出,显示在相同尺寸的(理想(垫子中。有没有人遇到同样的问题?可能是由于我对工业相机配置/开发缺乏了解,但任何帮助都值得赞赏。

情况:视窗 7(x64(Visual Studio Professional 2012OpenCV 版本 2.4.10 为 x86 编译,并选中以下标志:WITH_XIMEA和WITH_OPENGLx86 中的简单 VS2012 项目(发布和调试(,用于摄像机流式传输和在窗口中显示摄像机帧。

简单的代码片段(不是我的,从opencv教程和ximea复制(:C++风格:

#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char **argv) {
	VideoCapture cap(0); // open the default camera
	//VideoCapture cap(CV_CAP_XIAPI);  
  if(!cap.isOpened())  // check if we succeeded
        return -1;
cout<<" "<<cap.get(CV_CAP_PROP_FRAME_WIDTH)<<" "<<cap.get(CV_CAP_PROP_FRAME_HEIGHT)<<endl; //output: 640, 480
	cap.set(CV_CAP_PROP_FRAME_WIDTH,XRES);
	cap.set(CV_CAP_PROP_FRAME_HEIGHT,YRES);	
cout<<" "<<cap.get(CV_CAP_PROP_FRAME_WIDTH)<<"  "<<cap.get(CV_CAP_PROP_FRAME_HEIGHT)<<endl;  //output: 320, 240
 for(;;)
    {
        Mat frame;
        cap >> frame; // get a new frame from camera
        imshow("camera frame", frame);
    if(waitKey(30) == 27) //wait for 'esc' key press
		{
            cout << "esc key is pressed by user" << endl;
			break; 
		}
    }
    return 0;}
C 型:

#include "cv.h" 
#include "highgui.h" 
#include <stdio.h>  
#include <iostream>
using namespace cv;
using namespace std;
// A Simple Camera Capture Framework 
int main() 
{
   CvCapture* capture = cvCaptureFromCAM( CV_CAP_XIAPI );
   if ( !capture ) {
     fprintf( stderr, "ERROR: capture is NULL n" );
     getchar();
     return -1;
   }
cvSetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH, 648 ); 
cvSetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT, 486 );
   // 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 );
cout<<cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH); //output: 648
cout<<cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT); //output: 486
cout<<frame->height<<frame->width<<endl; //output: 1944, 2592
     if ( !frame ) {
       fprintf( stderr, "ERROR: frame is null...n" );
       getchar();
       break;
     }
     cvShowImage( "mywindow", 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;
}

谢谢卡尔菲利普的回答。不幸的是,你是对的,这不是一个理想的方式。所以我拿着昨天的雪麦,自己找到了解决方案:cap_ximea.cppresetCvImage()方法有误.

第 207 行 if( (int(image.width != width ||(整数(图像高度 != 高度 ||image.frm != (XI_IMG_FORMAT(格式(

必须是:

if( (int)image.width != **frame->**width || (int)image.height != **frame->**height || image.frm != (XI_IMG_FORMAT)format)

VideoCapture::set()返回一个布尔值来指示方法的成功。不应让应用程序继续运行,而不检查此调用的成功/失败,并在必要时重新调整捕获的大小。

事实是,某些相机驱动程序不接受任意尺寸,对此您根本无能为力。但是,您可以使用默认大小检索框架,然后根据需要cv::resize()框架。

这并不理想,但它会完成工作。