OpenCV VideoCapture输出图像剪切到左上角的四分之一

时间:2019-05-03 02:07:50

标签: c++ opencv ueye

我正在尝试将IDS uEye摄像机与OpenCV集成在一起,并且目前可以使用。 我面临的问题是,当我使用IDS SDK查看摄像机图像时,会得到完整的图像。但是使用OpenCV的VideoCapture,我只能得到图像的左上角。 我只是将矩形图像分成四分之一来阐明完整图像应该是什么(整个矩形)以及我从视频捕获中得到的图像(仅左上四分之一) rect http://www.kheper.net/topics/civilization/four.gif

我已经尝试通过cap.set来调整图像的宽度和高度,并且由于VideoCapture行是在设置uEye相机参数之后进行的,因此,我确定这不是uEye相机的设置问题相机以及与VideoCapture本身有关的更多信息

char strCamFileName[256];
char* pcImageMemory;
int memId;
int nRet = 0;

SENSORINFO sInfo;
IplImage* img;
HIDS hCam = 0;                // index 0 means taking first camera available
RECT rc;
MSG msg;
Mat frame(MaxImageSizeY, MaxImageSizeX, CV_8UC1);

nRet = is_InitCamera(&hCam, hWndDisplay);
if (nRet != IS_SUCCESS)
{
    cout << endl << "Error Connecting to Camera" << endl;
    cout << "Closing program..." << endl;
    return 0;
}
else
{
    cout << endl << "Camera initialisation was successful!" << endl << endl;
}

// you can query information about the sensor type of the camera
nRet = is_GetSensorInfo(hCam, &sInfo);
if (nRet == IS_SUCCESS)
{
    cout << "Cameramodel: \t\t" << sInfo.strSensorName << endl;
    cout << "Maximum image width: \t" << sInfo.nMaxWidth << endl;
    cout << "Maximum image height: \t" << sInfo.nMaxHeight << endl << endl << endl;
}
MaxImageSizeX = sInfo.nMaxWidth;
MaxImageSizeY = sInfo.nMaxHeight;
DisplayWidth = MaxImageSizeX;
DisplayHeight = MaxImageSizeY;

int nColorMode = IS_COLORMODE_CBYCRY;
int nBitsPerPixel = 32;


// Get number of available formats and size of list
UINT count;
UINT bytesNeeded = sizeof(IMAGE_FORMAT_LIST);
nRet = is_ImageFormat(hCam, IMGFRMT_CMD_GET_NUM_ENTRIES, &count, sizeof(count));
bytesNeeded += (count - 1) * sizeof(IMAGE_FORMAT_INFO);
void* ptr = malloc(bytesNeeded);
// Create and fill list
IMAGE_FORMAT_LIST* pformatList = (IMAGE_FORMAT_LIST*)ptr;
pformatList->nSizeOfListEntry = sizeof(IMAGE_FORMAT_INFO);
pformatList->nNumListElements = count;
nRet = is_ImageFormat(hCam, IMGFRMT_CMD_GET_LIST, pformatList, bytesNeeded);
// Prepare for creating image buffers
char* pMem = NULL;
int memID = 0;
// Set each format and then capture an image
IMAGE_FORMAT_INFO formatInfo;

// Allocate image mem for current format, set format
nRet = is_AllocImageMem(hCam, MaxImageSizeX, MaxImageSizeY, nBitsPerPixel, &pMem, &memID);
nRet = is_SetImageMem(hCam, pMem, memID);
nRet = is_ImageFormat(hCam, IMGFRMT_CMD_SET_FORMAT, &formatInfo.nFormatID, sizeof(formatInfo.nFormatID));


// Sets the color mode to be used when image data are saved or displayed by the graphics card
is_SetColorMode(hCam, nColorMode);

// allocates an image memory for an image, activates it and sets the way in which the images will be displayed on the screen
int nMemoryId;
is_AllocImageMem(hCam, MaxImageSizeX, MaxImageSizeY, nBitsPerPixel, &pcImageMemory, &nMemoryId);
is_SetImageMem(hCam, pcImageMemory, nMemoryId);
is_SetDisplayMode(hCam, IS_SET_DM_DIB);
is_HotPixel(hCam, IS_HOTPIXEL_DISABLE_CORRECTION, NULL, NULL);

IS_RECT AAOI;       // IS_RECT type variable for Auto AOI parameters
AAOI.s32X = MaxImageSizeX / 3 | IS_AOI_IMAGE_POS_ABSOLUTE;
AAOI.s32Width = MaxImageSizeX / 3;
AAOI.s32Y = MaxImageSizeY / 3 | IS_AOI_IMAGE_POS_ABSOLUTE;
AAOI.s32Height = MaxImageSizeY / 3;

double enable = 1;
double disable = 0;
is_SetAutoParameter(hCam, IS_SET_AUTO_SPEED, &enable, 0);
is_SetAutoParameter(hCam, IS_SET_ENABLE_AUTO_GAIN, &enable, 0);
is_SetAutoParameter(hCam, IS_SET_ENABLE_AUTO_WHITEBALANCE, &enable, 0);
is_SetAutoParameter(hCam, IS_SET_ENABLE_AUTO_FRAMERATE, &disable, 0);
is_SetAutoParameter(hCam, IS_SET_ENABLE_AUTO_SHUTTER, &disable, 0);
is_SetAutoParameter(hCam, IS_SET_ENABLE_AUTO_SENSOR_GAIN, &disable, 0);
is_SetAutoParameter(hCam, IS_SET_ENABLE_AUTO_SENSOR_WHITEBALANCE, &enable, 0);
is_SetAutoParameter(hCam, IS_SET_ENABLE_AUTO_SENSOR_SHUTTER, &disable, 0);
is_AOI(hCam, IS_AOI_AUTO_BRIGHTNESS_SET_AOI, &AAOI, sizeof(AAOI));
is_AOI(hCam, IS_AOI_AUTO_WHITEBALANCE_SET_AOI, &AAOI, sizeof(AAOI));

VideoCapture cap;             //--- INITIALIZE VIDEOCAPTURE
int deviceID = 0;             // 0 = open default camera
int apiID = cv::CAP_ANY;      // 0 = autodetect default API
if (cap.open(deviceID, apiID))
{
    cout << "cap opened" << endl;
}
else
{
    cout << "cap not opened" << endl;
}

cout << "Press 1 to capture image" << endl
     << "Press 2 to use (last) captured image" << endl;

cap.read(frame);

据我所知VideoCapture应该能够从相机获取整个图像,对吗? 老实说,我真的很困惑,为什么VideoCapture剪切了图像的3/4,我将不胜感激

1 个答案:

答案 0 :(得分:0)

好吧,我发现了问题所在... 同样,我在原始帖子中遗漏了太多代码(因为有很多与USB相关的不相关代码),所以我将在这里包括我遗漏的最重要的部分

double enable = 1;
double disable = 0;
is_SetAutoParameter(hCam, IS_SET_AUTO_SPEED, &enable, 0);
is_SetAutoParameter(hCam, IS_SET_ENABLE_AUTO_GAIN, &enable, 0);
is_SetAutoParameter(hCam, IS_SET_ENABLE_AUTO_WHITEBALANCE, &enable, 0);
is_SetAutoParameter(hCam, IS_SET_ENABLE_AUTO_FRAMERATE, &disable, 0);
is_SetAutoParameter(hCam, IS_SET_ENABLE_AUTO_SHUTTER, &disable, 0);
is_SetAutoParameter(hCam, IS_SET_ENABLE_AUTO_SENSOR_GAIN, &disable, 0);
is_SetAutoParameter(hCam, IS_SET_ENABLE_AUTO_SENSOR_WHITEBALANCE, &enable, 0);
is_SetAutoParameter(hCam, IS_SET_ENABLE_AUTO_SENSOR_SHUTTER, &disable, 0);
is_AOI(hCam, IS_AOI_AUTO_BRIGHTNESS_SET_AOI, &AAOI, sizeof(AAOI));
is_AOI(hCam, IS_AOI_AUTO_WHITEBALANCE_SET_AOI, &AAOI, sizeof(AAOI));

//// Acquires a single image from the camera
//is_FreezeVideo(hCam, IS_WAIT);
//// Output an image from an image memory in the specified window
//int nRenderMode = IS_RENDER_FIT_TO_WINDOW;
//is_RenderBitmap(hCam, nMemoryId, hWndDisplay, nRenderMode);

is_ExitCamera(hCam);        // exit camera so that OpenCV can access as camera parameters have been set

CalibSet CS;        // declaring variable 'CS' under the class 'CalibSet'
Mat livemap1, livemap2;
FileStorage tfs(inputCalibFile, FileStorage::READ);     // Read the settings
if (!tfs.isOpened())
{
    cout << "Could not open the calibration file: \"" << inputCalibFile << "\"" << endl;
    return -1;
}
tfs["camera_matrix"] >> CS.CamMat;
tfs["distortion_coefficients"] >> CS.DistCoeff;
tfs["image_width"] >> CS.image.width;
tfs["image_height"] >> CS.image.height;
tfs.release();                                         // close Settings file

所以。基本上,类CalibSet的作用是保存.xml文件的值,该文件用于在失真校正后提取值。 有关更多信息,请点击此处Camera calibration data retrieval 但是阻碍cap.set工作的问题可能是最后几行。 tfs["image_width"] >> CS.image.width;tfs["image_height"] >> CS.image.height;分别将“ image_width”和“ image_height”中的值存储在类CalibSet中的各个变量中。

猜猜是什么... .xml文件中的宽度和高度是640x480 ... 我将.xml中的该部分修改为假定的1280x1024,并且摄像机的实时供稿已固定,最终得到了完整的图像,而不是以前的1/4。