0

I'm having a problem regarding reading the pixel values (w=30, h=10). Suppose I'm using

  1. int readValue = cvGetReal2D(img,y,x); and
  2. int readValue = data[y*step+x];

Lets say I am trying to access pixel values at w=35, h=5 using the (1) and (2) method. The (1) will output an error of index out of range. But why (2) does not output an error of index out of range?

After that, I'm trying to use try...catch()...

Hammer
  • 10,109
  • 1
  • 36
  • 52
Mzk
  • 1,102
  • 3
  • 17
  • 40

1 Answers1

1

You have a continuous block of memory of

size  = w*h = 300

At w = 35 and h = 5 your equation gives

data[5*30+35] = data[190] < data[300]

so there is no error. If this is c++ then even if your index in data was larger than 299 it wouldn't throw an error. In that case you would be accessing the data beyond its bounds which results in undefined behavior.

I assume cvGetReal2D(img,y,x) is smart enough to tell you that one of your indices is larger than the defined size of that dimension even though it could be resolved to a valid address.

Hammer
  • 10,109
  • 1
  • 36
  • 52
  • Thank you for the responds Hammer. I'm using c++. How do you get the step value and do you know what can I do so that data[y*step +x] is smart enough to handle the exception? – Mzk Sep 22 '12 at 03:11
  • I realized that if width = 30 then the widthStep should be 31. – Mzk Sep 22 '12 at 03:47
  • I did it.Just need to do something at the try.catch.Thanks @Hammer. – Mzk Sep 22 '12 at 05:59
  • 1
    If width is 30, width step should be 30. Take for example a width of 4. Your first row is 0,1,2,3. second is 4,5,6,7 ect. That means your first column is 0,4,8,12, all are 4 apart – Hammer Sep 22 '12 at 06:08