Hiển thị các bài đăng có nhãn math expressions. Hiển thị tất cả bài đăng
Hiển thị các bài đăng có nhãn math expressions. Hiển thị tất cả bài đăng

Chủ Nhật, 19 tháng 7, 2015

OpenCV vs Matlab in Image Processing

OpenCv and Matlab are frequently used in image processing and computer vision because of their well-supported from OpenCv developers and Mathworks. In this post, I would like to highlight some differences in some basic concepts in memory layout, model, math expressions and etc. These concepts are very vital for programmers who use these tools.

1. Memory layout of an image
  • OpenCV supports only interleaved images (packing multiple channels one after the other for each pixel). This is more ridiculous and take notices when reading pixel values of opencv image.
  • Always to like this in scanning an image in Opencv 
    int channels = I.channels();

    int nRows = I.rows;
    int nCols = I.cols * channels;

    if (I.isContinuous())
    {
        nCols *= nRows;
        nRows = 1;
    }

    int i,j;
    uchar* p;
    for( i = 0; i < nRows; ++i)
    {
        p = I.ptr<uchar>(i);
        for ( j = 0; j < nCols; ++j)
        {
            p[j] = table[p[j]];
        }
    }
  • Matlab all of the channels clustered into image planes with the planes placed one after another. There is one advantage that matlab is homogenous, and single invariant to deal with.
2. Memory model
  • OpenCV is row-major
  • Matlab is column-major.

3. Matrix allocation and copy
  • Opencv: Do not expect a deep copy of an assignment operation in OpenCv. It just copies the header of Mat object.

  • Matlab: Somehow, Matlab will delay the deep copy until necessarily.
Ex: a = rand(5000);
      b = a;                // still not copy from a
      a(1) = 1;            // a will make a deep copy of itself and modify first element to 1.

This is the only way to implement Mat in opencv, because Mat will be shared between many variables rather than deep copying around, which is heavily prohibited for large object in memory.

4. Math experssions:
Although cv::Mat supports some mathematical expressions, it is very limited. Matlab is very intuitive and expressive language of math.

5. Matrix dimension in Opencv:
Semantic meaning x ~ width, y ~ height. But the first dim is y, second dim is x. 
For example, to access elements in position (x, y) in Mat, we should do
mat.at<int>(y, x).
Similarly, Matlab also uses the same notations.

6. Continuity of memory layout:
Memory layout for a matrix in Matlab is continuous, column after column. However, in Opencv, it can be separate or continuous. We can check its continuity by calling function Mat::isContinuous() in Mat objects. 
http://docs.opencv.org/modules/core/doc/basic_structures.html#mat-iscontinuous

7. Size vs. Size
OpenCV returns size() method as Size(cols, rows). This is inconsistent with Mat constructor Mat(rows, cols, type). While Matlab is always consistent, a matrix will return size as index dimensions of matrix (0, 1, 2, ...).
The problem with OpenCV that it has notations of width, height with width ~ x ~ cols, height ~ y ~ rows. Remembering this correspondence is problematic.
REMEMBER: Size(x, y).

Selamat Hari Raya
@An