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 に答える