Mzk
Mzk

Reputation: 1120

Reading Pixel Value Method?

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()...

Upvotes: 0

Views: 695

Answers (1)

Hammer
Hammer

Reputation: 10329

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.

Upvotes: 1

Related Questions