Hiển thị các bài đăng có nhãn MATLAB. Hiển thị tất cả bài đăng
Hiển thị các bài đăng có nhãn MATLAB. 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

Thứ Năm, 2 tháng 1, 2014

MATLAB options file in Linux 64

Happy new year 2014,
I am writing this blog to notice you a trick to change option files in Matlab in Linux, which is very useful. When you want to use MEX to compile a C/Fortran source in MEX interface, you need to modify option files mexopts.sh in Linux because the default file contains errors in Linux. Why is it important so? The answer is that options files control which compiler to use, the compiler and link command options, and the runtime libraries to link against.
Then, where the file locate in Linux? The default file mexopts.sh locates in /home/tranlaman/.matlab/R2012a. But there is another default root file lie in /home/tranlaman/MATLAB/R2012a/bin which overrides the file in /home/tranlaman/.matlab/R2012a. So, modifying the mexopts.sh in /home/tranlaman/MATLAB/R2012a/bin would change everything.
Finally, how do we modify this file? The answer is to change some compiling and linking flag such as CC, CFLAGS, CLIBS, LD, LDFLAGS. Please refer to [1] as a complete flag for custom building MEX files.
Wish everyone has a good year.