OpenCV代码示例错误

时间:2013-02-03 21:45:52

标签: c opencv runtime-error

我编写了以下代码示例,它应该创建一个特殊的渐变并将其保存到用户指定的文件中:

#include <stdio.h>
#include <cv.h>
#include <highgui.h>

static IplImage *image = 0;

void main() {
   char path[1024];
   int x, y;
   CvScalar scalar;

   scanf("%s", path);
   image = cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1);

   for (y = 0; y < 480; y++) {
      for (x = 0; x < 640; x++) {
         scalar = cvGet2D(image, x, y);
         scalar.val[0]=(unsigned char)(x + y);
         cvSet2D(image, x, y, scalar);
      }
   }

   cvSaveImage(path, image, 0);
}

我使用:gcc opencv.c -o opencv `pkg-config --libs --cflags opencv` -lm编译它,一切似乎都没问题。但是,在运行时(输入:“sample.png”),我收到以下错误:

OpenCV Error: One of arguments' values is out of range (index is out of range) in cvPtr2D, file /builddir/build/BUILD/OpenCV-2.3.1/modules/core/src/array.cpp, line 1797
terminate called after throwing an instance of 'cv::Exception'
  what():  /builddir/build/BUILD/OpenCV-2.3.1/modules/core/src/array.cpp:1797: error: (-211) index is out of range in function cvPtr2D

Aborted (core dumped)

请帮忙吗?提前致谢! :)

2 个答案:

答案 0 :(得分:4)

cvGet2D和cvSet2D使用[row,column]约定,就像opencv中的许多其他函数一样。

进一步阅读: http://www.cs.iit.edu/~agam/cs512/lect-notes/opencv-intro/

答案 1 :(得分:1)

cvGet2DcvSet2D函数将行作为第一个参数,将列作为第二个参数:x和y参数的轮次错误。因此,你走出图像。电话应该是:

cvGet2D(image, y, x);