Kindly explain the code (each step in detail) thanks.

%% Different ways to convert RGB image into grey scale
clear all;
close all;
clc;
r=21;
se1_para = 10;
se2_para = 2;
img_old = imread("C:\Users\aayus\OneDrive\Desktop\Screenshots\1.jpg");
figure(1),imshow(img_old);
% crop
[x,y,z] = size(img_old);
img = rgb2gray(img_old);
se1 = strel('disk',se1_para);
img_c = imclose(img,se1)
figure(2), imshow(img_c,[]);
AND
clear all;
close all;
a=imread("C:\Users\aayus\OneDrive\Desktop\Screenshots\1.jpg");
figure(1);
e=imshow(a);
b=rgb2gray(a);
figure(2);
imshow(b);
c=imfreehand(gca);
d=createMask(c,e);
figure(3);
imshow(d);
f=edge(d,'canny');
figure(4);
imshow(f);
h=bwarea(d);
[m,n]=size(f);
for k=0;
for o=1:m;
for p=1:n;
if f(o,p)==1;
k=k+1;
end
end
end
end

Answers (1)

DGM
DGM on 11 Dec 2022
Edited: DGM on 11 Dec 2022
First part
% Different ways to convert RGB image into grey scale <-- misleading comment
clear all; % removes all variables, globals, functions and MEX links (bad practice)
close all; % closes all open figure windows
clc; % clear console
% set up parameters
r=21; % this is never used
se1_para = 10;
se2_para = 2; % this is never used
% read a file, show it
img_old = imread("C:\Users\aayus\OneDrive\Desktop\Screenshots\1.jpg");
figure(1),imshow(img_old);
% crop <-- another misleading comment that has nothing to do with anything
[x,y,z] = size(img_old); % get the size of the image
% note that array indexing is [y x z], not [x y z]
% since we're dealing with color images, it would make more sense to use [rows cols channels]
% safer practice would be [rows,cols,chans,~] = size(img_old);
% but none of this is ever used, so the whole line can be discarded
img = rgb2gray(img_old); % presume image is RGB and convert to BT601 luma
% this will result in error if input is not RGB
se1 = strel('disk',se1_para); % create 10px disk strel
img_c = imclose(img,se1); % perform morphological closing (remove small dark features)
figure(2), imshow(img_c,[]); % show it
Second part
clear all; % same thing
close all; % same thing
% read the image and show it
a=imread("C:\Users\aayus\OneDrive\Desktop\Screenshots\1.jpg");
figure(1);
e=imshow(a);
b=rgb2gray(a); % same thing; show it
figure(2);
imshow(b);
c=imfreehand(gca); % create freehand ROI object
d=createMask(c,e); % create mask from ROI object
% note that variable e does not exist
figure(3); % show the mask by itself
imshow(d);
f=edge(d,'canny'); % find edges of mask
figure(4); % show the edge map by itself
imshow(f);
h=bwarea(d); % find the area of the edgemap
% the area returned by bwarea() is not exactly the number of white pixels
% see documentation for how corner pixels are weighted
[m,n]=size(f); % get size of edge map (see prior comment)
for k=0; % this loop does nothing
for o=1:m; % loop through every row
for p=1:n; % and every column in edge map
if f(o,p)==1; % and count every 1
k=k+1;
end
end
end
end
% this whole pile of loops can be replaced by
% k = nnz(f);
Instead of using edge() on a binary image, it would make more sense to use bwperim().
k = nnz(bwperim(d));
would actually give you the perimeter of the selection, whereas finding the area of an edgemap from edge() will give a gross overestimate. You could also use regionprops() instead.

Products

Release

R2022a

Asked:

on 11 Dec 2022

Edited:

DGM
on 11 Dec 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!