来自相机的OpenCV作物实时供稿 [英] OpenCV crop live feed from camera

查看:93
本文介绍了来自相机的OpenCV作物实时供稿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从OpenCV(640x320)的摄像头中获得正确的一种分辨率供稿,但将其切成一半并仅显示一半的帧(320x240).因此,请不要缩小规模,而要裁剪.我正在使用OpenCV 2.4.5,VS2010和C ++

How can I get properly one resolution feed from camera in OpenCV (640x320) but cut it into half and display only one half of the frame (320x240). So not to scale down, but to actually crop. I am using OpenCV 2.4.5, VS2010 and C++

这个相当标准的代码获得了640x480的输入分辨率,我对裁剪分辨率进行了一些更改,达到了320x240. 我应该使用Mat代替IplImage,如果是的话,最好的方法是什么?

This quite standard code gets 640x480 input resolution and I made some changes to crop resolution to 320x240. Should I use Mat instead of IplImage, and if so what would be the best way?

#include "stdafx.h"
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"

using namespace std;
char key;
int main()
{
    cvNamedWindow("Camera_Output", 1);    //Create window

    CvCapture* capture = cvCaptureFromCAM(1);  //Capture using camera 1 connected to system
    cvSetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH, 640 );
    cvSetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT, 480 );

    while(1){ //Create loop for live streaming

        IplImage* framein = cvQueryFrame(capture); //Create image frames from capture

        /* sets the Region of Interest  - rectangle area has to be __INSIDE__ the image */
        cvSetImageROI(framein, cvRect(0, 0, 320, 240));

        /* create destination image  - cvGetSize will return the width and the height of ROI */
        IplImage *frameout = cvCreateImage(cvGetSize(framein),  framein->depth, framein->nChannels);

        /* copy subimage */
        cvCopy(framein, frameout, NULL);

        /* always reset the Region of Interest */
        cvResetImageROI(framein);

        cvShowImage("Camera_Output", frameout);   //Show image frames on created window

        key = cvWaitKey(10);     //Capture Keyboard stroke
        if (char(key) == 27){
            break;      //ESC key loop will break.
        }
    }

    cvReleaseCapture(&capture); //Release capture.
    cvDestroyWindow("Camera_Output"); //Destroy Window
    return 0;
}

推荐答案

我认为您无需检查是否得到CvCapture.在只有一个摄像头的系统上,您的代码无法使用,因为您查询了摄像头1.但是第一个摄像头应为0,因此请更改此代码.

I think you don't check whether you are getting a CvCapture. On my system with only one camera your code doesn't work because you query camera 1. But the first camera should be 0 Thus change this code.

CvCapture* capture = cvCaptureFromCAM(1);  //Capture using camera 1 connected to system

至(请注意,我将1更改为0):

to (note I change 1 to 0):

CvCapture* capture = cvCaptureFromCAM(0);  //Capture using camera 1 connected to system
if (! capture ){
    /*your error handling*/
}

此外,您的代码似乎对我有用.您还可以检查其他指针值是否不为NULL.

Further than that your code seems to be working for me. You might also check the other pointer values whether you are not getting NULL.

这篇关于来自相机的OpenCV作物实时供稿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆