Vision via Matlab


The Pic_Grabber is a very basic code to take one picture using an Image Acquisition Device in Matlab. It requires the Matlab Image Acquisition Toolbox to run.

Variable_Name =Videoinput(‘winvideo’)


The video input function makes a connection between Matlab and a particular image acquisition device. It saves that connection to a variable (Variable_Name). Any time the user wants to call on the image from the image acquisition device they should call on this “Variable_Name”


Winvideo is an adaptor in the Image Acquisition Toolbox. This adaptor searches for all accessible Image Acquisition Devices connected to your computer. Winvideo will default to the default mode of the first camera detected.

triggerconfig(Variable_Name, ‘manual’)


The Triggerconfig function specifies the triggering parameters for each camera. The triggerconfig options are manual or immediate. This function turns the camera on for acquiring images.


The manual option allows the user to turn on the camera at specified times during the code instead of having the camera running the entire time.


The immediate option turns the camera on right after the triggerconfig function is executed.



The Start function is used when the Triggerconfig is in the Manual mode. It turns the camera on.



The Getsnapshot function returns one single frame from the camera being used.



The Imshow function displays the image saved to the “Variable_Name2”.



The stop function is used to turn off the camera.



The delete function deletes the image saved under “Variable_Name”. Both the stop(V) and the delete(V) commands need to be used to completely close and turn off the camera being used.

Colored Object Detection

%%% Colored Object Detection Program    %%%%%%%%
%%% By: Andrew Bryant and Billy Bates   %%%%%%%%
%%% Advanced Robotics - April 2010      %%%%%%%%

clear all

%% INPUTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Color of object (RGB value)

% Minimum Object Size (pixels)
obj.minsize = 100;
%% Read in Image Data %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

image = imread('RedDot.jpg');


The imread function reads in an image and saves it a variable for image processing.

%% Filter Image %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%Subtract object color from image
 I(:,:,1) = I(:,:,1) - obj.color(1);
 I(:,:,2) = I(:,:,2) - obj.color(2);
 I(:,:,3) = I(:,:,3) - obj.color(3);
%Convert to black and white image


The im2bw function turns the Image into a Binary black and white image based off of the level specified “.1”. This level is based on Black being “0” and white being “1”.

%Filter out any objects that are smaller than minimum size
I = bwareaopen(~I, obj.minsize);


The Bwareaopen function removes all connected components in a Binary image that have less than a specified number of connected components “100”.

%% Analyze Image %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Labels each object in image with a different number in the matrix
I = bwlabel(I);
%Analyze objects
s  = regionprops(I, 'centroid','minoraxislength','majoraxislength','orientation')


The regionprops function measures a set of properties for a given region “I”. These regions have to be labeled, the bwlabel function labeled the regions of interest in the previous step.


The centroid command in the regionprops function finds the centroid of each of the regions labeled by the bwlabel function.


The area command in the regionprops function finds the area of each of the regions labeled by the bwlabel function.


The MaxAxisLength command in the regionprops function finds the longest cross sectional length of each of the regions labeled by the bwlabel function.

%Read data from structure to matrix form
centroids = cat(1, s.Centroid);
axis=[cat(1, s.MajorAxisLength),cat(1, s.MinorAxisLength),cat(1, s.Orientation)];
%% Plot Centroids %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
imshow(image), hold on
plot(centroids(:,1), centroids(:,2), 'b*')

hold off

Line Detection Program

%%% Line Detection Program              %%%%%%%%
%%% By: Andrew Bryant and Billy Bates   %%%%%%%%
%%% Advanced Robotics - April 2010      %%%%%%%%

%% Clear the Workspace %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all
close all
%% Read in Image %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

image = imread('lines.tif');

%Display Image
%% Filter Image %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Convert Image to Grayscale
**I = rgb2gray(image);**

%Median Filter
**I = medfilt2(I);**


The medfilt2 function filters out what is sometimes called “salt & pepper” noise in an image. By default the filter takes the mean of a 3x3 matrix around a pixel and replaces that pixel with the mean of the surrounding area.

%Normalize Contrast
**I = imadjust(I,stretchlim(I),[]);**


The imadjust function maps a grayscale image so that 1% of the data is saturated at low and high intensities of the original image.

%Display Filtered Grayscale
**figure(2), imshow(I);**

%Detect edges in image
**BW = edge(I,'sobel',.10);**


The edge function takes a binary image or gray scale image and returns a binary image with 1’s where it finds edges in the image and 0’s everwhere else.


The canny method of edge detection is based off of finding the local maxima of the gradient of the image. It uses two thresholds, to detect strong and weak edges. It only includes the weak edges if they are connected to strong edges. This method is less likely to be fooled by noise. ++++ sobel
The sobel method of edge detection uses the Sobel approximation to the derivative. It returns edges where the gradient is maximum.

**figure(3), imshow(BW);**

The Hough Transform is explained best in the Matlab Help Section

%% Hough Transform %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
**[H,theta,rho] = hough(BW);
P = houghpeaks(H,10,'threshold',ceil(0.1*max(H(:))));
x = theta(P(:,2));
y = rho(P(:,1));**

%%Select lines from peaks in hough transform
%Adjust FillGap and MinLength to change line detection behavior
**lines = houghlines(BW,theta,rho,P,'FillGap',10,'MinLength',60);**


The Fillgap parameter in the houghlines function specifies a distance between two line segments in the same hough transform bin that if less or equal to that number the houghlines function will connect them to become on line.


The Minlength parameter in the houghlines function specifies the Minimum merged line length that will be kept as a line.

%% Plot Detected lines on image, highlighting the longest %%%%%%%%%%%%%%%%
**figure(1),imshow(image), hold on
max_len = 0;**

% If no lines are detected, skip plotting
**if size(lines, 2)>1**

**for k = 1:length(lines)
   xy = [lines(k).point1; lines(k).point2];

   % Plot beginnings and ends of lines

   % Determine the endpoints of the longest line segment
   **len = norm(lines(k).point1 - lines(k).point2);
   if ( len > max_len)
      max_len = len;
      xy_long = xy;

% Highlight the longest line segment
hold off**

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License