2

I'm profiling the code that I have developed, and I see a bottleneck in my code when I use cvSet2D. Is there some alternative to cvSet2D more efficient?

How can I write that pixels?

2 Answers 2

1

I recommend you to use the C++ functions and not the old C style functions.

The most efficient way to write to pixels is the following.

   cv::Mat frame;

   // load your img to your Mat

   uchar* p = frame.ptr(row); // returns a row pointer
   p[column] = x; // accesses value in the given column

One thing to note is that you might have more columns than you have pixel columns, e.g. on a 3 channel image you have 3 times the number of actual pixel columns you can access.

For more information on different ways to iterate over pixels, check this tutorial.

1
  • Thank you very much. Nice tutorial.
    – edsonlp1
    Apr 26, 2012 at 11:42
0

You need to get a pointer to the data field of the structure.

(C API) The IplImage structure has a char* field called data. Access (your_type*)image->data for the first element, and then use it like a regular C 1D array, but be careful to use the field stepWidth to jump from a line to the next (because lines of data may be aligned on multiples of 16 bits for memory access optimization).

(C++ API) Use T* cv::Mat::ptr<T>(int i) to get a pointer to the first element of the line you want to access. Then use as a regular C++ 1D array.

This should be the faster access pattern (see the book OpenCV2 Cookbook for a comparison of the different access patterns).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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