Interfacing Matlab with Microcontroller using RS 232 Cable (Part 2)

Posted on at


MATLAB Code:

function [camera_name, camera_id, resolution] = getCameraInfo(a)
camera_name = char(a.InstalledAdaptors(end));
camera_info = imaqhwinfo(camera_name);
camera_id = camera_info.DeviceInfo.DeviceID(end);
resolution = char(camera_info.DeviceInfo.SupportedFormats(end));
%% Matlab code
clc
a = imaqhwinfo;
[camera_name, camera_id, format] = getCameraInfo(a);
% Capture the video frames using the videoinput function
% You have to replace the resolution & your installed adaptor name.
vid = videoinput(camera_name, camera_id, format);
% Set the properties of the video object
set(vid, 'FramesPerTrigger', Inf);
set(vid, 'ReturnedColorspace', 'rgb')
vid.FrameGrabInterval = 5;
%start the video aquisition here
start(vid)
% Set a loop that stop after 100 frames of aquisition
while(vid.FramesAcquired<=100)
data = getsnapshot(vid);
% Now to track red objects in real time
% we have to subtract the red component
% from the grayscale image to extract the red components in the
image.
diff_im1 = imsubtract(data(:,:,1), rgb2gray(data));
5 | P a g e
diff_im2 = imsubtract(data(:,:,3), rgb2gray(data));
if sum( diff_im1(:)) > sum(diff_im2(:))
%clear all;
s = serial ('COM7');
set (s,'BaudRate',4800);
fopen(s);
fprintf(s,'%s','c,b');
fclose(s)
delete(s)
else
%clear all;
s = serial ('COM7');
set (s,'BaudRate',4800);
fopen(s);
fprintf(s,'%s','a,d');
fclose(s)
delete(s)
end
% Get the snapshot of the current frame
%Use a median filter to filter out noise
diff_im1 = medfilt2(diff_im1, [3 3]);
diff_im2 = medfilt2(diff_im2, [3 3]);
% Convert the resulting grayscale image into a binary image.
diff_im1 = im2bw(diff_im1,0.18);
diff_im2 = im2bw(diff_im2,0.18);
% Remove all those pixels less than 300px
diff_im1 = bwareaopen(diff_im1,300);
diff_im2 = bwareaopen(diff_im2,300);
% Label all the connected components in the image.
bw1 = bwlabel(diff_im1, 8);
bw2 = bwlabel(diff_im2, 8);
% Here we do the image blob analysis.
% We get a set of properties for each labeled region.
stats1 = regionprops(bw1, 'BoundingBox', 'Centroid');
stats2 = regionprops(bw2, 'BoundingBox', 'Centroid');
% Display the image
imshow(data)
hold on
%This is a loop to bound the red objects in a rectangular box.
for object = 1:length(stats1)
bb = stats1(object).BoundingBox;
bc = stats1(object).Centroid;
rectangle('Position',bb,'EdgeColor','r','LineWidth',2)
plot(bc(1),bc(2), '-m+')
6 | P a g e
a=text(bc(1)+15,bc(2), strcat('X: ', num2str(round(bc(1))), '
Y: ', num2str(round(bc(2)))));
set(a, 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize',
12, 'Color', 'yellow');
end
for object = 1:length(stats2)
bb = stats2(object).BoundingBox;
bc = stats2(object).Centroid;
rectangle('Position',bb,'EdgeColor','r','LineWidth',2)
plot(bc(1),bc(2), '-m+')
a=text(bc(1)+15,bc(2), strcat('X: ', num2str(round(bc(1))), '
Y: ', num2str(round(bc(2)))));
set(a, 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize',
12, 'Color', 'yellow');
end
hold off
end
if sum( diff_im1(:)) > sum(diff_im2(:))
%clear all;
s = serial ('COM7');
set (s,'BaudRate',4800);
fopen(s);
fprintf(s,'%s','c,b');
fclose(s)
delete(s)
else
%clear all;
s = serial ('COM7');
set (s,'BaudRate',4800);
fopen(s);
fprintf(s,'%s','a,d');
fclose(s)
delete(s)
end
% Both the loops end here.
% Stop the video aquisition.
stop(vid);
% Flush all the image data stored in the memory buffer.
flushdata(vid);



About the author

160