Vision via Matlab

Pic_Grabber:

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’)

Videoinput()

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

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’)

triggerconfig()

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.

manual

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.

immediate

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

start(Variable_Name)

start()

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

Variable_Name2=getsnapshot(Variable_Name)

getsnapshot()

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

imshow(Variable_Name2)

imshow()

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

stop(Variable_Name)

stop()

The stop function is used to turn off the camera.

delete(Variable_Name)

delete()

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
clc

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

% Color of object (RGB value)
obj.color=[255,0,0];

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

image = imread('RedDot.jpg');
RedDot.JPG

imread()

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

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

I=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);
object_color_filt.jpg
%Convert to black and white image
I=im2bw(I,.1);
object_bw.jpg

im2bw()

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);
object_bwareaopen.jpg

bwareaopen()

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')

regionprops()

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.

centroid

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

Area

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

MaxAxisLength

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
object_output.jpg

Line Detection Program

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

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

image = imread('lines.tif');

%Display Image
figure(1)
%% Filter Image %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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

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

medfilt2

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),[]);**

imadjust

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);**

edge

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.

canny

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);**

Fillgap

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.

Minlength

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(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');**

   % Plot beginnings and ends of lines
   **plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
   plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');**

   % 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;
   end
end**

% Highlight the longest line segment
**plot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','cyan');
hold off**

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