14

I am looking for an efficient way for editing/reading pixels from Mat (or Mat3b) variable.

I have used :-

Image.at<Vec3b>(i,j)

but it seems to be very slow.

I also used this:-

A.data[A.channels()*A.cols*i + j + 0]

but the problem I am facing with this is when I run this loop

for(i=0; j<A.rows; i++){
   for(j=0; j<A.cols; j++){
           A.data[A.channels()*A.cols*i + j + 0] = 0;
           A.data[A.channels()*A.cols*i + j + 1] = 0;
           A.data[A.channels()*A.cols*i + j + 2] = 0;
    }
} 

only a portion of image is blackened.

1
  • There are faster way to access Mat elements, as the accepted answer correctly states, but running your program in Release mode will speed up a lot the mat.at<Vec>() accesses. They will be just a bit slower than pointer access
    – Sam
    Commented Apr 2, 2012 at 16:08

2 Answers 2

19

Here you can see some of the possibilities for fast element access.

But if you want to do it your way, you need to add a bracket. Otherwise you index computation is not correct:

for(int i=0; i<A.rows; i++){
   for(int j=0; j<A.cols; j++){
           A.data[A.channels()*(A.cols*i + j) + 0] = 0;
           A.data[A.channels()*(A.cols*i + j) + 1] = 0;
           A.data[A.channels()*(A.cols*i + j) + 2] = 0;
    }
} 

But the layout of the memory is not guaranteed to be contiguous due to padding. So according to this you should rather use a formula like this:

for(int i=0; i<A.rows; i++){
   for(int j=0; j<A.cols; j++){
           A.data[A.step[0]*i + A.step[1]* j + 0] = 0;
           A.data[A.step[0]*i + A.step[1]* j + 1] = 0;
           A.data[A.step[0]*i + A.step[1]* j + 2] = 0;
    }
} 
2
  • A.data[A.step[0]*i + A.step[1]* j + 0] = 0; This line is giving me error. Commented Apr 2, 2012 at 13:20
  • 2
    I used A.data[A.step*i + A.channels()*j + 0], this is working fine. Thanks it is taking much less time than the previous method. :) Commented Apr 2, 2012 at 13:23
14

This is one of the most efficient way for editing/reading pixels from cv::Mat. Create pointer to a row (of specific channel if needed)

for(int i=0; i<A.rows;i++){
  uchar* rowi = A.ptr/*<uchar>*/(i);
  for(int j=0; j<A.cols; j++){
     doProcessOnPixel(rowi[j]);
  }
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.