Use OpenCV2.1.0 with Matlab 2010a

Side Note: First draft on Mar 27 2011.

OpenCV and Matlab are often “competitors”, as a lot of image/video processing techniques are implemented in both of them. However, there’re times you want to combine the strength/functions of the two. And luckily Matlab provides C/C++ interface.

Below are the step-by-step configurations for setting up Matlab environment for calling OpenCV functions.

1. Download and Install OpenCV.

2. Set up C/C++ compiler for Matlab.

  • Start up matlab 2010a, enter “mex –setup”. The screen capture below shows this step in detail,

image

3. Edit the mexopts.bat file.

  • The file location can be found at the second last line of the screen capture.
  • Add “set OPENCVDIR=C:OpenCV2.1” at the beginning of the file just below the line “set VCINSTALLDIR=%VSINSTALLDIR%VC”.
  • Add “%OPENCVDIR%includeopencv;” at the begnning of the the “set INCLUDE=” line
  • Add “%OPENCVDIR%lib;%OPENCVDIR%bin;” at the beginning of the “set LIB=” line
  • Add “cv210.lib highgui210.lib cvaux210.lib cxcore210.lib” at the line “set LINKFLAGS=” at the position before “/nologo”.

4. Edit PATH environment variable

Append “C:OpenCV2.1lib” at the end of the PATH.

image

5. Restart Matlab and you can start using OpenCV from Matlab by using the Matlab C++ interface.

Image Histogram Equalization

Side Note: First draft on Mar 26 2011.

1. Concepts

Image Histogram refers to a graphical representation of tonal distribution of a digital image. It plots the number of pixels at each tonal level. (according to wikipedia)

Image histogram equalization is an image processing method for image contrast adjustment. The nice property of image histogram equalization is it makes use of the input image property. It can be very effective for image that has low contrast. Below is an example,

Mandrillimage

2. Matlab Implementation

Matlab has histogram equalization function built-in, histeq. But one can easily implement it himself/herself.

A simple histogram equalization makes use of the Cumulative Distribution Function (CDF),

image

where Sk is the transformed tonal level for an input tonal level rk. k = 0, 1, …2^numOfBitsPerChannelOfAPixel-1 is the tonal level range. nj is the total number of pixels with jth tonal level. The equation says the total number of pixels have gray level no more than kth level divide by the total number of pixels will be the CDF of input tonal level k.

The CDF is able to map the input tonal level for every pixel to a range of [0, 1], once we scale this range to [0, 255], we can produce the output tonal level for every pixel.

The matlab implementation for Gray image and explanation is as below,

function[O]=feipeng_histeq(fpath)
    %1. read the image file into a matrix
    I=imread(fpath);
    %2. get the total number of pixel
    N=numel(I);
    %3. get a one-dimensional matrix indicating number of 
    %pixels at each
    %grey-level
    A=zeros(1,256);
    for i=1:1:256
        A(1,i)=numel(I(I==i-1));
    end
    figure, imhist(I);  %for test
    %4. get the cumulative number of pixels at each grey-level
    %[note]can be improved by using the previous calculated
    % B for calculation of
    %current B
    B=zeros(1,256);
    for i=1:1:256
        for j=1:1:i
            B(1,i)=B(1,i)+A(1,j);
        end
    end
    %5. get the normalized cumulative distribution at each 
    % grey-level
    for i=1:1:256
        B(1,i)=B(1,i)/N;
    end
    figure, plot(0:255, B);
    %6. for each pixel, compute it's output intensity
    [nrows,ncols]=size(I);
    O=zeros(nrows,ncols, 'uint8');
    for i=1:1:nrows
        for j=1:1:ncols
            O(i,j)=B(1,I(i,j)+1)*255;
        end
    end
    AO = zeros(1, 256);
    for i=1:1:256
        AO(1, i) = numel(O(O==i-1));
    end
    figure, imhist(O);
    figure,imshow(O);
    %for comprison with built-in histogram equalization
    O1 = histeq(I);
    figure, imhist(O1);
    figure, imshow(O1);
end

The plots for the histogram are shown below,

imageimageimage

The 3 plots are input image, the output image histogram plot for the implementation above and the matlab built-in implementation histogram.

3. Photoshop

Photoshop (not sure starts with which version, but CS5 definitely) has histogram equalization built-in at Image=>Adjustments=>Curves.

How to Gaussian in Matlab

Side note: First draft on Mar 18 2011. Everything popular tends to become a verb. “Let’s Google it.”, “Did you tweet today?”… Well, Gaussian deserves to be a verb also.

1. 1-D Gaussian Filter

1-D Gaussian filter can be created according to the normal distribution function below,

image

The sample matlab code is given below,

image

A plot of the GUASS will give you the graph below,

image

2. 2-D Gaussian Filter

2-D Gaussian Filter can be created based on the following formula,

image

The sample matlab code is given below,

image

The surfc function gives the plot below,

image

2. fspeical and imfilter: 2-D Gaussian Filter

fspecial is used to create several kinds of predefined 2-D filter in matlab, including “gaussian”. imfilter can be used to apply the created filter to multidimensional images.

fspecial takes three input parameters, with the syntax h = fspecial(‘gaussian’, hsize, sigma). hsize can be a vector specifying the number of rows and cols in the generated filter h, or a scalar indicating equal size for height and width. Sigma is the standard deviation marking the thiness of the gaussian.

imfilter takes at least two input parameters, with the syntax B = imfilter(A, H, …), where A is the input matrix, H is the filter. The common options include following,

  • Output Size Options:
    1. ‘same’: output is the same size as input. Default behavior if not specified
    2. ‘full’: output is full filtered result, which is larger than input.
  • Correlation and Convolution Options:
    1. ‘corr’: use correlation operations for filtering. Default behavior if not specified.
    2. ‘conv’: use convolution operations for filtering.

This predefined Gaussian filter use the formula below to create the filter,

image

Sample matlab code is given as below,

image

The surfc(h) gives the following plot,

image

References:

1. http://www.mathworks.com/help/toolbox/images/ref/fspecial.html

2. http://www.mathworks.com/help/toolbox/images/ref/imfilter.html