Robotics and Image Processing: Object Detection using MATLAB Code

Verified

Added on  2023/05/26

|9
|1024
|289
Homework Assignment
AI Summary
This assignment provides a detailed solution for image processing and object detection in robotics using MATLAB. The solution includes MATLAB functions for thresholding an image, segmenting an image to display objects in different colors, and extracting information about individual segments such as their label, area, and centroid. Morphological filtering and binary transformations are applied to enhance image segmentation. The code identifies and labels objects (penguins in this case) within the image, calculates their dimensions, and determines their centroids, demonstrating practical applications of image processing in robotics. Desklib offers a wide range of solved assignments and study resources for students.
Document Page
Running head: INTRODUCTION TO ROBOTICS
INTRODUCTION TO ROBOTICS
Name of the Student
Name of the University
Author Note
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
1) The function to threshold an image is given below.
MATLAB code:
function thresholdimage(I,thres)
x = I;
figure;
subplot(2,1,1)
imshow(x)
title('Grayscale Image')
x=double(x);
total=0;
[row,col]=size(x);
bin=zeros(row,col); % initializing binary matrix with all 0s
for i=1:row
for j=1:col
total=total+x(i,j);
end
end
%%% applying binary transformation
for i=1:row
for j=1:col
if x(i,j) > thres
bin(i,j)=1;
else
bin(i,j)=0;
end
Document Page
end
end
subplot(2,1,2)
imshow(bin);
title('binary image')
end
MATLAB command and Output:
I = rgb2gray(imread('penguins.jpg'));
thresholdimage(I,100)
Grayscale Image
binary image
Document Page
2) The MATLAB function which segments an image and displays the image in different
colour in which background is given a different colour with particular intensity and
subjects are given a different colour with a different intensity is shown below.
MATLAB function:
%%% converting the image to grayscale
I = rgb2gray(imread('penguins.jpg'));
%%% removing all of the foreground using the morphological filtering. The
%%% radius of the disk = 300 taken for complete dark background.
se = strel('disk',300);
background = imopen(I,se);
I2 = I - background; % subtracting the background from the grayscale image
%%% using the Thresholding function to convert the image into binary.
%%% thres= 100 is used.
x=double(I2);
total=0;
[row,col]=size(x);
bin=zeros(row,col); % initializing binary matrix with all 0s
for i=1:row
for j=1:col
total=total+x(i,j);
end
end
%%% applying binary transformation
for i=1:row
for j=1:col
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
if x(i,j) > 100
bin(i,j)=1;
else
bin(i,j)=0;
end
end
end
%%% clearing background noise from image
bw = bwareaopen(bin,50); % removing the background noise from the image
%%% finding all of the connected components in the binary image. Hence,
%%% detects all the objects i.e. the number of penguins in the image
prop = bwconncomp(bw,6);
labeled = labelmatrix(prop); % the dimensions of the objects are stored in labeled by
labelmatrix function
RGB_label = label2rgb(labeled,'jet','y','shuffle'); % displaying the background and
objects in different color by colormap
imshow(RGB_label)
Output:
Document Page
3) The information of the individuals segments from part 2 and their label, area and
centroid is displayed by the following MATLAB function.
MATLAB function:
function [numobjects,dimensionlist,Area,centroid] = segmentinfo()
%%% converting the image to grayscale
I = rgb2gray(imread('penguins.jpg'));
%%% removing all of the foreground using the morphological filtering. The
%%% radius of the disk = 300 taken for complete dark background.
se = strel('disk',300);
background = imopen(I,se);
I2 = I - background; % subtracting the background from the grayscale image
%%% using the Thresholding function to convert the image into binary.
Document Page
%%% thres= 100 is used.
x=double(I2);
total=0;
[row,col]=size(x);
bin=zeros(row,col); % initializing binary matrix with all 0s
for i=1:row
for j=1:col
total=total+x(i,j);
end
end
%%% applying binary transformation
for i=1:row
for j=1:col
if x(i,j) > 100
bin(i,j)=1;
else
bin(i,j)=0;
end
end
end
%%% clearing background noise from image
bw = bwareaopen(bin,50); % removing the background noise from the image
%%% finding all of the connected components in the binary image. Hence,
%%% detects all the objects i.e. the number of penguins in the image
prop = bwconncomp(bw,6);
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
numobjects = prop.NumObjects;
dimensionlist = prop.PixelIdxList;
penguindata = regionprops(prop,'basic');
Area = [penguindata.Area];
centroid = [penguindata.Centroid];
Output:
numobjects =
6
dimensionlist =
1×6 cell array
{158084×1 double} {49×1 double} {[72698]} {[102024]} {[158266]}
{[158648]}
Area =
158084 49 1 1 1 1
centroid =
Document Page
254.8575 184.0620 189.2653 303.5918 191.0000 308.0000 268.0000 297.0000
416.0000 151.0000 417.0000 152.0000
Hence, the number of detected objects was found to be 6 using the above
segmentation method as the size of the objects is large and some of the objects are
connected. The number of penguins are 5 and the background is also taken into
account and hence the number of different objects are 6. The dimensions of each label
is displayed and their area. The centroids of each object or label is calculated along
with their intersection and hence centroid vector length is larger than the number of
objects.
chevron_up_icon
1 out of 9
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]