项目作者: petercorke

项目描述 :
Machine Vision Toolbox for MATLAB
高级语言: MATLAB
项目地址: git://github.com/petercorke/machinevision-toolbox-matlab.git
创建时间: 2015-04-06T04:44:35Z
项目社区:https://github.com/petercorke/machinevision-toolbox-matlab

开源协议:Other

下载


Build Status
Coverage
License: LGPL v3
Maintenance
GitHub stars

Synopsis

Machine Vision Toolbox for MATLAB® release 4.

The Machine Vision Toolbox (MVTB) provides many functions that are useful in machine vision and vision-based control. It is a somewhat eclectic collection reflecting my personal interest in areas of photometry, photogrammetry, colorimetry. It includes over 100 functions spanning operations such as image file reading and writing, acquisition, display, filtering, blob, point and line feature extraction, mathematical morphology, homographies, visual Jacobians, camera calibration and color space conversion. With input from a web camera and output to a robot (not provided) it would be possible to implement a visual servo system entirely in MATLAB.

An image is usually treated as a rectangular array of scalar values representing intensity or perhaps range. The matrix is the natural datatype for MATLAB and thus makes the manipulation of images easily expressible in terms of arithmetic statements in MATLAB language. Many image operations such as thresholding, filtering and statistics can be achieved with existing MATLAB functions.

Advantages of the Toolbox are that:

  • the code is mature and provides a point of comparison for other implementations of the same algorithms;
  • the routines are generally written in a straightforward manner which allows for easy understanding, perhaps at the expense of computational efficiency. If you feel strongly about computational efficiency then you can always rewrite the function to be more efficient, compile the M-file using the MATLAB compiler, or create a MEX version;
  • since source code is available there is a benefit for understanding and teaching.

Code Examples

Binary blobs

  1. >> im = iread('shark2.png'); % read a binary image of two sharks
  2. >> idisp(im); % display it with interactive viewing tool
  3. >> f = iblobs(im, 'class', 1) % find all the white blobs
  4. f =
  5. (1) area=7827, cent=(172.3,156.1), theta=-0.21, b/a=0.585, color=1, label=2, touch=0, parent=1
  6. (2) area=7827, cent=(372.3,356.1), theta=-0.21, b/a=0.585, color=1, label=3, touch=0, parent=1
  7. >> f.plot_box('g') % put a green bounding box on each blob
  8. >> f.plot_centroid('o'); % put a circle+cross on the centroid of each blob
  9. >> f.plot_centroid('x');

Binary image showing bounding boxes and centroids

Binary blob hierarchy

We can load a binary image with nested objects

  1. >> im = iread('multiblobs.png');
  2. >> idisp(im)

Binary image showing bounding boxes and centroids

and request the blob label image which we then display

  1. >> [label, m] = ilabel(im);
  2. >> idisp(label, 'colormap', jet, 'bar')

Binary image showing bounding boxes and centroids

Camera modelling

  1. >> cam = CentralCamera('focal', 0.015, 'pixel', 10e-6, ...
  2. 'resolution', [1280 1024], 'centre', [640 512], 'name', 'mycamera')
  3. cam =
  4. name: mycamera [central-perspective]
  5. focal length: 0.015
  6. pixel size: (1e-05, 1e-05)
  7. principal pt: (640, 512)
  8. number pixels: 1280 x 1024
  9. pose: t = (0, 0, 0), RPY/yxz = (0, 0, 0) deg

and its intrinsic parameters are

  1. >> cam.K
  2. ans =
  3. 1.0e+03 *
  4. 1.5000 0 0.6400
  5. 0 1.5000 0.5120
  6. 0 0 0.0010

We can define an arbitrary point in the world

  1. >> P = [0.3, 0.4, 3.0]';

and then project it into the camera

  1. >> cam.project(P)
  2. ans =
  3. 790
  4. 712

which is the corresponding coordinate in pixels. If we shift the camera slightly the image plane coordiante will also change

  1. >> cam.project(P, 'pose', SE3(0.1, 0, 0) )
  2. ans =
  3. 740
  4. 712

We can define an edge-based cube model and project it into the camera’s image plane

  1. >> [X,Y,Z] = mkcube(0.2, 'pose', SE3(0, 0, 1), 'edge');
  2. >> cam.mesh(X, Y, Z);

Perspective camera view

or with a fisheye camera

  1. >> cam = FishEyeCamera('name', 'fisheye', ...
  2. 'projection', 'equiangular', ...
  3. 'pixel', 10e-6, ...
  4. 'resolution', [1280 1024]);
  5. >> [X,Y,Z] = mkcube(0.2, 'centre', [0.2, 0, 0.3], 'edge');
  6. >> cam.mesh(X, Y, Z);

Fisheye lens camera view

Bundle adjustment

Color space

Plot the CIE chromaticity space

  1. showcolorspace('xy')
  2. lambda = [460:10:540 560:20:600];
  3. [x,y]=lambda2xy(lambda*1e-9);
  4. hold on
  5. plot_point([x y]', 'printf', {' %d', lambda}, 'ko', 'MarkerFaceColor', 'k', 'MarkerSize', 6)

CIE chromaticity space

Load the spectrum of sunlight at the Earth’s surface and compute the CIE xy chromaticity coordinates

  1. lambda = [400:5:700] * 1e-9; % visible light
  2. sun_at_ground = loadspectrum(lambda, 'solar');
  3. >> lambda2xy(lambda, sun_at_ground)
  4. ans =
  5. 0.3327 0.3454
  6. >> colorname(ans, 'xy')
  7. loading rgb.txt
  8. ans =
  9. 'antiquewhite4'

Hough transform

  1. im = iread('church.png', 'grey', 'double');
  2. edges = icanny(im);
  3. h = Hough(edges, 'suppress', 10);
  4. lines = h.lines();
  5. idisp(im, 'dark');
  6. lines(1:10).plot('g');
  7. lines = lines.seglength(edges);
  8. lines(1)
  9. k = find( lines.length > 80);
  10. lines(k).plot('b--')

Hough transform

SURF features

We load two images and compute a set of SURF features for each

  1. >> im1 = iread('eiffel2-1.jpg', 'mono', 'double');
  2. >> im2 = iread('eiffel2-2.jpg', 'mono', 'double');
  3. >> sf1 = isurf(im1);
  4. >> sf2 = isurf(im2);

We can match features between images based purely on the similarity of the features, and display the correspondences found

  1. >> m = sf1.match(sf2)
  2. m =
  3. 644 corresponding points (listing suppressed)
  4. >> m(1:5)
  5. ans =
  6. (819.56, 358.557) <-> (708.008, 563.342), dist=0.002137
  7. (1028.3, 231.748) <-> (880.14, 461.094), dist=0.004057
  8. (1027.6, 571.118) <-> (885.147, 742.088), dist=0.004297
  9. (927.724, 509.93) <-> (800.833, 692.564), dist=0.004371
  10. (854.35, 401.633) <-> (737.504, 602.187), dist=0.004417
  11. >> idisp({im1, im2})
  12. >> m.subset(100).plot('w')

Feature matching

Clearly there are some bad matches here, but we we can use RANSAC and the epipolar constraint implied by the fundamental matrix to estimate the fundamental matrix and classify correspondences as inliers or outliers

  1. >> F = m.ransac(@fmatrix, 1e-4, 'verbose')
  2. 617 trials
  3. 295 outliers
  4. 0.000145171 final residual
  5. F =
  6. 0.0000 -0.0000 0.0087
  7. 0.0000 0.0000 -0.0135
  8. -0.0106 0.0116 3.3601
  9. >> m.inlier.subset(100).plot('g')
  10. >> hold on
  11. >> m.outlier.subset(100).plot('r')
  12. >> hold off

where green lines show correct correspondences (inliers) and red lines show bad correspondences (outliers)
Feature matching after RANSAC

Fundamental matrix

What’s new

  • Travis CI is now running on the code base
  • All code related to pose representation has been split out into the Spatial Math Toolbox. This repo is now a dependency.

Installation

Install from shared MATLAB Drive folder

This will work for MATLAB Online or MATLAB Desktop provided you have MATLAB drive setup.

  1. Click on the appropriate link below and an invitation to share will be emailed to the address associated with your MATLAB account:

  2. Accept the invitation.

  3. A folder named RVC1 or RVC2 will appear in your MATLAB drive folder.
  4. Use the MATLAB file browser and navigate to the folder RVCx/rvctools and double-click the script named startup_rvc.m

Note that this is a combo-installation that includes the Robotics Toolbox (RTB) as well.

Install from github

You need to have a recent version of MATLAB, R2016b or later.

The Machine Vision Toolbox for MATLAB has dependency on two other GitHub repositories: spatial-math and toolbox-common-matlab.

To install the Toolbox on your computer from github follow these simple instructions.

From the shell:

  1. mkdir rvctools
  2. cd rvctools
  3. git clone https://github.com/petercorke/machinevision-toolbox-matlab.git vision
  4. git clone https://github.com/petercorke/spatial-math.git smtb
  5. git clone https://github.com/petercorke/toolbox-common-matlab.git common
  6. make -C vision

The last command builds the MEX files. Then, from within MATLAB

  1. >> addpath rvctools/common % rvctools is the same folder as above
  2. >> startup_rvc

The second line sets up the MATLAB path appropriately but it’s only for the current session. You can either:

  1. Repeat this everytime you start MATLAB
  2. Add the MATLAB commands above to your startup.m file
  3. Once you have run startup_rvc, run pathtool and push the Save button, this will save the path settings for subsequent sessions.

Downloading the example images

The Robotics, Vision & Control book (2nd edition) uses a number of example images and image sequences. These are bulky and not really appropriate to keep on Github but you can download them.
There are two zip archives:

Archive Size Contents

images-RVC2a.zip | 74M | All images, seq/*, mosaic/*, campus/* |
images-RVC2a.zip | 255M | Chapter 14: bridge-l/*, bridge-r/* |

Each will expand into the ./images folder which is the default location that MVTB searches for images and sequences.

To download the main (and smaller) archive

  1. cd rvctools/vision
  2. wget petercorke.com/files/MVTB/images-RVC2a.zip
  3. unzip images-RVC2a

To download the second (and larger) archive

  1. cd rvctools/vision
  2. wget petercorke.com/files/MVTB/images-RVC2b.zip
  3. unzip images-RVC2b

Download the contributed code

Some MVTB functions are wrappers of third-party open-source software. Working versions, some patched, can be downloaded below. If you are not using MacOS you will need to rebuild the code.

Archive Size Contents

contrib.zip | 16M | vl_feat, graphseg, EPnP, camera calibration |
contrib2.zip | 5M | SIFT, SURF |

The packages, and their home pages are

Tool Author MVTB function

OpenSurf | | isurf |
SIFT | | isift |
vl_feat | A. Vedaldi and B. Fulkerson| imser, isift, BagOfWords|
graphseg | P. Felzenszwalb, D. Huttenlocher | igraphseg |
Efficient perspective-n-point camera pose estimation (EPnP) | V. Lepetit, F. Moreno-Noguer, P. Fua | CentralCamera.estpose |
Camera Calibration Toolbox for MATLAB | Jean-Yves Bouget | calib_gui |

Online resources:

Please email bug reports, comments or code contribtions to me at rvc@petercorke.com

Contributors

Contributions welcome. There’s a user forum at http://tiny.cc/rvcforum

License

This toolbox is released under GNU LGPL.