协助CVLine / CVPoint

时间:2013-02-16 19:00:00

标签: opencv

在文本文件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);

    }

1 个答案:

答案 0 :(得分:0)

在我看来,这可能是一个错误的错误。当您调用malloc时,您正在为length元素数组分配足够的空间。数组是0索引的 - 也就是说,points[0]points[length-1]是有效的内存地址,而points[length]则不是。for(i=0; i < length; i++) { // scanf stuff here }

在第一个循环结束时,您设置的最后一个点是超过数组末尾的一个元素,并且在您尝试绘制最后一个线段时,该值可能会被某些内容覆盖。

尝试将第一个循环更改为:

for(i=0; i < length-1; i++)
    // cvLine stuff here

和你的第二个循环:

{{1}}