someone
someone

Reputation: 361

OpenCV Code Sample Error

I have written the following Code Sample that is supposed to create a peculiar-looking gradient and save it to a file specified by the user:

#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);
}

I compile it using: gcc opencv.c -o opencv `pkg-config --libs --cflags opencv` -lm and everything seems to be OK. However, during runtime (input: "sample.png"), I get the following error:

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)

Any help, please? Thanks in Advance! :)

Upvotes: 0

Views: 1863

Answers (2)

ajshort
ajshort

Reputation: 3754

The cvGet2D and cvSet2D function take the row as the first argument, and the column as the second argument: you have the x and y arguments the wrong way round. Hence you are going outside the image. The call should be:

cvGet2D(image, y, x);

Upvotes: 1

Nicola Pezzotti
Nicola Pezzotti

Reputation: 2357

cvGet2D and cvSet2D uses [row,column] convention like many other functions in opencv.

For further readings: http://www.cs.iit.edu/~agam/cs512/lect-notes/opencv-intro/

Upvotes: 4

Related Questions