OpenCV cvSetCaptureProperty - eiichiromomma/CVMLAB GitHub Wiki

(OpenCV) cvSetCaptureProperty

未実装だったcvSetCapturePropertyでのサイズ変更について

元ネタ

Yahoo.comのOpenCV Groupsに投稿されたネタ。

変更点

highgui.h

280行目付近に

#define CV_CAP_PROP_FRAME_320x240 16 //Add this lines or equivalents
#define CV_CAP_PROP_FRAME_640x480 17 //according to the resolution

を挿入。

もっと大きい画像に対応したければ自分でdefineを追加してindexの数字を増やすと良い(らしい)

cvcap_vfw.cpp

上記で大きい画像のdefineを追加した場合はcaseを増やす。

480行付近に

static int icvSetPropertyCAM_VFW (CvCaptureCAM_VFW* capture, int property_id, double value )
{
  switch( property_id )
  {
  case CV_CAP_PROP_FRAME_320x240:
    {
      BITMAPINFO szBitmapInfo;
      capGetVideoFormat(capture->capWnd, &szBitmapInfo, sizeof(szBitmapInfo));

      int val = szBitmapInfo.bmiHeader.biSizeImage /
      (szBitmapInfo.bmiHeader.biWidth * szBitmapInfo.bmiHeader.biHeight);

      // Here you specify the width and height
      szBitmapInfo.bmiHeader.biWidth = 320;
      szBitmapInfo.bmiHeader.biHeight = 240;
      szBitmapInfo.bmiHeader.biSizeImage = 320 * 240 * val;
      if(capSetVideoFormat(capture->capWnd, &szBitmapInfo, sizeof(szBitmapInfo)))
      {
        capture->frame.width = 320;
        capture->frame.height = 240;
      }
      return 1;
    }
  case CV_CAP_PROP_FRAME_640x480:
    {
      BITMAPINFO szBitmapInfo;
      capGetVideoFormat(capture->capWnd, &szBitmapInfo, sizeof(szBitmapInfo)); // Get current configuration

      int val = szBitmapInfo.bmiHeader.biSizeImage /
      (szBitmapInfo.bmiHeader.biWidth * szBitmapInfo.bmiHeader.biHeight);

      // Here you specify the width and height
      szBitmapInfo.bmiHeader.biWidth = 640;
      szBitmapInfo.bmiHeader.biHeight = 480;
      szBitmapInfo.bmiHeader.biSizeImage = 640 * 480 * val;
      if(capSetVideoFormat(capture->capWnd, &szBitmapInfo, sizeof(szBitmapInfo))) // If successful Setting interesting fields
      {
        capture->frame.width = 640;
        capture->frame.height = 480;
      }
      return 1;
    }
  }
  return 0;
}

を挿入。

その下あたりの

static CvCaptureVTable captureCAM_VFW_vtable = 
{
    6,
    (CvCaptureCloseFunc)icvCloseCAM_VFW,
    (CvCaptureGrabFrameFunc)icvGrabFrameCAM_VFW,
    (CvCaptureRetrieveFrameFunc)icvRetrieveFrameCAM_VFW,
    (CvCaptureGetPropertyFunc)icvGetPropertyCAM_VFW,
    (CvCaptureSetPropertyFunc)0,//←ここ
    (CvCaptureGetDescriptionFunc)0
};

static CvCaptureVTable captureCAM_VFW_vtable = 
{
    6,
    (CvCaptureCloseFunc)icvCloseCAM_VFW,
    (CvCaptureGrabFrameFunc)icvGrabFrameCAM_VFW,
    (CvCaptureRetrieveFrameFunc)icvRetrieveFrameCAM_VFW,
    (CvCaptureGetPropertyFunc)icvGetPropertyCAM_VFW,
    (CvCaptureSetPropertyFunc)icvSetPropertyCAM_VFW, //←ここ
    (CvCaptureGetDescriptionFunc)0
};

のように変更。

ライブラリのリビルド

C:\Program Files\OpenCV_makeのopencv.slnを開いてビルド。

確認用サンプル

1,2キーでサイズ変更。 当然だがcv.lib、cxcore.lib、highgui.libをリンクする。

    #include "cv.h"
    #include "highgui.h"

    void Capture();

    int main(void)
    {
      Capture();
      return 0;
    }

    void Capture()
    {
      CvCapture* capture = 0;
      capture = cvCaptureFromCAM(-1);
      if (capture)
      {
        cvNamedWindow("video", 1);
        for(;;)
        {
          IplImage* frame = 0;
          frame = cvQueryFrame(capture);

          cvShowImage("video", frame);

          int c = cvWaitKey(10);

          if (c == 27)
            break;
          if (c == '1')
            c = cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_640x480, 0);
          if (c == '2')
            c = cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_320x240, 0);
        }
      }
    }