0

使用 时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 图像的屏幕截图 带有 640x480 图像的屏幕截图

带有 631x480 图像的屏幕截图 631x480 截图

4

1 回答 1

0

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

于 2013-01-16T21:16:00.723 回答