Life is a game, take it seriously

Posts Tagged ‘matlab’

Object matching method made in the 20th century

In Computer Vision, Matlab on January 15, 2012 at 8:33 pm

written by gooly

object recognition using SIFTI just submitted some Matlab code for object matching, using an old but simple method mentioned in the paper:

Lowe, D.G. 1999. Object recognition from local scale-invariant features.
In International Conference on Computer Vision, Corfu,
Greece, pp. 1150–1157.

This is the original famous SIFT paper. Most people know SIFT points for its robustness and scale, rotation invariant, but many might not notice that an object matching method is also mentioned in the paper.

This Matlab code is based on that method but uses SURF points instead of SIFT. To run the Matlab code you have to download the SURFmex library first.
Remember to include the SURFmex library by right clicking the folder in Matlab and add subfolders to path.

You can then run Demo.m to see the matching result.

Demo.m first calls createTargetModel with a target image and an image with the contour of the target in the same image as input. createTargetModel then gathers the information needed for object matching and output it as targetModel.

matchTarget is then called with the targetModel and the test image as input. The contour of the target in the test image will then be shown.

The algorithm works as follows. First the SURF points of the target image is extracted and stored.  In matchTarget.m the SURF points of the test image is also calculated and each of them is matched to the most similar SURF point in the model. By using the scale and orientation of the SURF point descriptor, each matched SURF point pair has a translation from the target image to the test image.

Therefore 1 pair of correctly matched SURF points can decide the position, scale and orientation of the target in the test image. However most of the matched pairs aren’t correct, therefore we use all of the pairs to cast votes on what are the correct position, scale and orientation of the target in the test image.

The result that has the highest votes are then refined. A rotation matrix and  a transition vector is then calculated based on the SURF point pairs in the result.



SURF On Images: continue

In Computer Vision, Matlab on May 7, 2011 at 11:43 pm

written by Gooly

Continuing the last post, I will show how to actually work on SURF points using MATLAB. First download the SURFmex library; this is a second version released in 2008, which is simply a wrapper around the OpenCV dll. The good thing about OpenCV is that it’s open source and actively used, so you won’t get stuck in a dll which couldn’t move to another OS. Note that you have to fix the surfplot function bug in the common folder, see this explanation. Next, put the library in the project folder and remember to right click on it and add to path. Now, put 2 images, in my case img1.png and img2.png, in the same folder. Next step, download a small function I wrote to show the matching result like the image above from here (or search match_plot on Matlab Central); put the function in the same folder. Finally, add the following code and run.

function match_test()

close all;

% surfnum = 1:30;

left_img  = imread('./img1.png');
right_img = imread('./img2.png');

[spixels_l,descr_l,sign_l,info_l] = SurfPoints(rgb2gray(left_img));

hold on;
plot(spixels_l(1,:),spixels_l(2,:),'r*'); hold on

[spixels_r,descr_r,sign_r,info_r] = SurfPoints(rgb2gray(right_img));

hold on;
plot(spixels_r(1,:),spixels_r(2,:),'r*'); hold on

matches = surfmatch(descr_l,descr_r);

surfnum = randi(size(matches,2),1,30);


hold on;

hold on;


You will get 2 image which have all of the SURF points marked, and an image like the one above which matches 30 random SURF points, and 2 images showing the orientation and size of the SURF points like the image below.

On the next post, I’ll show how to do simple object recognition using SURF points. By the way, if you work on stereo vision, these images must look familiar; for those who don’t know, search for middlebury 

MATLAB: Read all images in a folder; everything starts here

In Computer Vision, Matlab on April 13, 2011 at 3:26 pm

written by Gooly

In ordinary people’s life, it’s often the case that you need to deal with a large but not extremely huge image database with non sequential naming. Instead of reading each image when you need it, you probably would prefer to read it all into a matrix for one time, or even store it into a mat file so that you can access it much faster next time. The MATLAB function dir would be a good way to do this, see the example below.

function X = ReadImgs(Folder,ImgType)
    Imgs = dir([Folder '/' ImgType]);
    NumImgs = size(Imgs,1);
    image = double(imread([Folder '/' Imgs(1).name]));
    X = zeros([NumImgs size(image)]);
    for i=1:NumImgs,
      image = double(imread([Folder '/' Imgs(i).name]));
      if (size(image,3) == 1)
        X(i,:,:) = image;
        X(i,:,:,:) = image;

To read in all the jpeg files in image_folder simply do the following,

ImageData = ReadImgs('image_folder','*.jpg');

Notice that this function only deals with image of the same size, which is usually the case; to be able to deal with different size images, you can use imresize in the function or other methods depending on what the task you are actually working on.

When dealing with huge image database (2GB up), you would probably prefer to store the images into separate mat files, only open the one that you want to use, and close it afterwards.

Note that as Thiruvikraman Kandhadai mentioned in the comment, this code won’t run if you have both RGB and Gray scale images in the folder. Save them into two separate matrices if you really need to.