2

这似乎应该非常简单,但由于某种原因,我的线没有居中 - 它比应该的更靠左。

这是我的代码:

static const int width = 240;
static const int height = 320;

VideoCapture cap(0);
cap.set(CV_CAP_PROP_FRAME_WIDTH, width);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, height);

//Set points for center line
CvPoint mid_bottom, mid_top;
mid_bottom.x = width/2;
mid_bottom.y = 0;
mid_top.x = width/2;
mid_top.y = height;

//for purposes not related to this issue, I have to use IplImages instead of Mats
Mat frame; //(edit - forgot this declaration before, sorry!)
cap >> frame;
IplImage frame_ipl = frame;

//drawing the line
cvLine(&frame_ipl, mid_bottom, mid_top, RED, 2);

结果:

关于为什么会出错的任何想法?

4

1 回答 1

3

Niko 敏锐地观察到,您的相机可能无法提供指定尺寸的镜框。您可以检查返回的值set()以查看是否创建成功。如果没有,它将返回false

    bool wset = cap.set(CV_CAP_PROP_FRAME_WIDTH, width);
    bool hset = cap.set(CV_CAP_PROP_FRAME_HEIGHT, height);
    if (!wset || !hset)
    {
        std::cout << "Image dimension could not be set" << std::endl;
        // Handle error...
    }

一种更通用的方法是根据实际图像尺寸定义线条,这允许它在任意图像上工作:

    CvPoint mid_bottom, mid_top;
    mid_bottom.x = frame_ipl.width/2;
    mid_bottom.y = 0;
    mid_top.x = frame_ipl.width/2;
    mid_top.y = frame_ipl.height;
于 2013-07-11T21:34:13.193 回答