这看起来应该非常简单,但由于某种原因,我的线路没有居中 - 它的左侧比它应该更远。
这是我的代码:
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);
有关为何出错的任何想法?
答案 0 :(得分: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;