输入参数的大小在cvcalcopticalflowbm opencv 2.4.7中不匹配

时间:2014-02-15 07:08:45

标签: opencv opticalflow

我想在opencv 2.4.7中使用cvcalcopticalflowBM函数计算光流 当我遵守下面的代码。错误消息是“cvcalcopticalflowbm中输入参数的大小不是macth()

我不明白为什么会这样。请帮我。谢谢你提前。

#define BS 5

IplImage *imgA = NULL, *imgB = NULL;
IplImage *grayA = NULL, *grayB = NULL;
IplImage *velx = NULL, *vely = NULL;
IplImage *result = NULL;
imgA = cvLoadImage("00.jpg", 1);
imgB = cvLoadImage("01.jpg", 1);
grayA = cvCreateImage(cvGetSize(imgA), IPL_DEPTH_8U, 1);
grayB = cvCreateImage(cvGetSize(imgA), IPL_DEPTH_8U, 1);

cvCvtColor(imgA, grayA, CV_BGR2GRAY);
cvCvtColor(imgB, grayB, CV_BGR2GRAY);

CvSize size = cvGetSize(imgA);
size.width /= BS;
size.height /= BS;
result = cvCreateImage(size, IPL_DEPTH_8U, 1);
for (int i=0; i<size.height; i++) {
    for (int j=0; j<size.width; j++) {
        cvSet(result, CV_RGB(255,255,255), NULL);
    }
}
velx = cvCreateImage(size, IPL_DEPTH_32F, 1);
vely = cvCreateImage(size, IPL_DEPTH_32F, 1);
cvCalcOpticalFlowBM(grayB, grayA, cvSize(BS, BS), cvSize(1, 1), cvSize(1, 1), 0, velx, vely);
//
cvNamedWindow("HorFlowBM", CV_WINDOW_AUTOSIZE);
cvShowImage("HorFlowBM", velx);
cvNamedWindow("VerFlowBM", CV_WINDOW_AUTOSIZE);
cvShowImage("VerFlowBM", vely);
for (int i=0; i<size.height; i+=2) {
    for (int j=0; j<size.width; j+=2) {
        int dx = (int)cvGetReal2D(velx, i, j);
        int dy = (int)cvGetReal2D(vely, i, j);
        cvLine(result, cvPoint(j, i), cvPoint(j+dx, i+dy), CV_RGB(0,0,0), 1, 8, 0);
    }
}
cvNamedWindow("OpticalFlow", CV_WINDOW_AUTOSIZE);
cvShowImage("OpticalFlow", result);
cvWaitKey(0);

2 个答案:

答案 0 :(得分:0)

您确定输入图像正在加载吗?在加载它们之后尝试显示它们,即cvShowImage("input1", imgA);。另外,尝试打印两个图像的大小以检查两个图像的大小是否相同。

答案 1 :(得分:0)

我认出了这个错误。 velx和vely的大小应该是

CvSize velSize =
{
    (grayA->width - BLOCK_SIZE + SHIFT_SIZE)/SHIFT_SIZE,
    (grayA->height - BLOCK_SIZE + SHIFT_SIZE)/SHIFT_SIZE
};

编译程序时正确