在文本文件 npoints3 中,我的坐标格式为 1 242 2 242 3 242 ... 900 242,它是我图像上的一条直线;但是,下面的代码会打印一条直线加上一条额外的线段。我不确定我做错了什么。有人可以帮我解释为什么我会得到额外的线段吗?提前致谢!
IplImage* InputImg = cvLoadImage("image.jpg", CV_LOAD_IMAGE_GRAYSCALE);
CVPoint * points = (CvPoint*)malloc(length*sizeof(CvPoint*));
FILE *fp;
fp = fopen("npoints3", "r");
for(i=1; i <= length; i++)
{
fscanf(fp, "%d", &(points[i].x));
fscanf(fp, "%d", &(points[i].y));
printf("%d %d\n",points[i].x, points[i].y);
}
for(i=1; i < length; i++)
cvLine(InputImg, points[i], points[i+1], red, 3, 1);
感谢您的回复。为了确保我清楚地理解你,我已经发布了你的建议编辑。以下循环是您建议的吗?提前致谢。
IplImage* InputImg = cvLoadImage("image.jpg", CV_LOAD_IMAGE_GRAYSCALE);
if (!InputImg) {
printf("Could not load Input Image");
exit(0);
}
points1 = (CvPoint*)malloc(length*sizeof(CvPoint*));
points2 = (CvPoint*)malloc(length*sizeof(CvPoint*));
points = (CvPoint*)malloc(length*sizeof(CvPoint*));
fp = fopen("points", "r");
for(i=0; i < length; i++)
{
fscanf(fp, "%d", &(points[i].x));
fscanf(fp, "%d", &(points[i].y));
}
for(i=0; i < length; i++)
{
points1[i].x = points[i].x;
points1[i].y = points[i].y;
points2[i].x = points[i+1].x; points2[i].y = points[i+1].y;
// cvLine(InputImg, points1[i], points2[i], red, 3, 1);
}
cvSnakeImage(InputImg, points, length, alpha, beta, gamma, CV_VALUE, winni, criteria, calcGradient);
for (i = 0; i <length-1; i++)
{
points1[i].x = points[i].x;
points1[i].y = points[i].y;
points2[i].x = points[i+1].x; points2[i].y = points[i+1].y;
cvLine(InputImg, points1[i], points2[i], red, 3, 1);
}