cvCalcOpticalFlowPyrLK输入图像大小

时间:2012-05-30 10:37:28

标签: c++ opencv opticalflow

使用cvCalcOpticalFlowPyrLK时,生成的金字塔对于某些输入图像大小似乎有误。我在文档中找不到任何对良好输入图像大小的引用。然而,它找到了积分。有谁知道这是怎么发生的以及如何解决这个问题?

#include <opencv/highgui.h>
#include <opencv/cv.h>
#include <iostream>

#define SHOW_IMAGE(image) { \
  cvNamedWindow(#image, CV_WINDOW_AUTOSIZE); \
  cvShowImage(#image, image); \
}

const CvScalar COLOR_GREEN = { { 0.0, 255.0, 0.0, 255.0 } };
const CvScalar COLOR_RED = { { 255.0, 0.0, 0.0, 255.0 } };

void drawPoint(IplImage* image, const float x, const float y, const CvScalar& color) {
  cvCircle(image, cvPoint(x, y), 3, color, -1, 8);
}

void drawPoints(IplImage* image, const CvPoint2D32f* points, const char* status, int count) {
  for (int i = 0; i != count; ++i) {
    if (status[i] == 1) {
      drawPoint(image, points[i].x, points[i].y, COLOR_GREEN);
    } else {
      drawPoint(image, points[i].x, points[i].y, COLOR_RED);
    }
  }
}

using namespace std;

int main( int argc, char** argv ) {
  /* load samples */
  IplImage* src0 = cvLoadImage( "test0.png" );
  IplImage* src1 = cvLoadImage( "test1.png" );
  CvSize size = cvSize(src0->width, src0->height);
  /* allocate grey images and convert to grey */
  IplImage* prev = cvCreateImage(size, 8, 1);
  cvCvtColor(src0, prev, CV_RGB2GRAY);
  IplImage* curr = cvCreateImage(size, 8, 1);
  cvCvtColor(src1, curr, CV_RGB2GRAY);
  /* allocate pyramids */
  IplImage* prevPyr = cvCreateImage(size, 8, 1);
  IplImage* currPyr = cvCreateImage(size, 8, 1);
  /* set previous features */
  const CvPoint2D32f prevFeatures[] = {{133, 133}, {364, 133}, {364, 322}, {133, 322}};
  /* allocate current features */
  CvPoint2D32f currFeatures[] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}};
  int count = 4;
  CvSize winSize = cvSize(20, 20);
  int level = 3;
  char status[4];
  CvTermCriteria criteria = cvTermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 20, 0.03);
  int flags = 0;
  cvCalcOpticalFlowPyrLK(
    /* previous image    */prev
    /* current image     */, curr
    /* previous pyramid  */, prevPyr
    /* current pyramid   */, currPyr
    /* previous features */, prevFeatures
    /* current features  */, currFeatures
    /* features count    */, count
    /* win_size          */, winSize
    /* level             */, level
    /* status            */, status
    /* track error       */, 0
    /* criteria          */, criteria
    /* flags             */, flags);
  drawPoints(src0, prevFeatures, status, count);
  drawPoints(src1, currFeatures, status, count);
  SHOW_IMAGE(src0);
  SHOW_IMAGE(src1);
  SHOW_IMAGE(currPyr);
  SHOW_IMAGE(prevPyr);
  /* wait any key */
  while (cvWaitKey(1) == -1);
  return 0;
}

(如果使用gcc,您可以使用g++ pyr.cpp -o pyr -lcv -lcvaux && ./pyr编译并运行它)

640x480图片的屏幕截图 Screenshot with 640x480 image

631x480图片的屏幕截图 Screenshot with 631x480

1 个答案:

答案 0 :(得分:0)

似乎分配的内存存在对齐错误。 IplImage对齐到4个字节。 Perhapse金字塔没有初始化足够,请参阅文档((image_width + 8)* image_height / 3字节的总大小就足够了。)