2012-05-30 75 views
0

使用cvCalcOpticalFlowPyrLK時,生成的金字塔對於某些輸入圖像大小看起來不正確。我無法在文檔中找到任何有關良好輸入圖像大小的參考。儘管如此,它找到了點有誰知道這是怎麼發生的以及如何解決這個問題?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運行此)

截圖與640×480圖像 Screenshot with 640x480 image

截圖與631x480的圖像 Screenshot with 631x480

回答

0

似乎就像有一個對齊錯誤分配的內存。 IplImage對齊到4個字節。修復金字塔的初始化不夠充分,請參閱文檔((image_width + 8)* image_height/3字節的總大小就足夠了)。