Question: Create a classifier in Matlab that accepts as input an image and outputs a string indicating whether the person in the image is: (1) wearing

Create a classifier in Matlab that accepts as input an image and outputs a string indicating whether the person in the image is: (1) wearing PPE or (2) not wearing PPE. The color-based system we developed during the in-class tutorial might be a good starting point. The four test images, along with other images you may find useful, have been provided along with the assignment document.

I have mentioned my error as a comment below towards the end. Have a look at it and solve the error for me to get an output.

worker_image = imread("~/Downloads/Assets/PPE_1.jpg");

imshow(worker_image)

Red = worker_image(:,:,1);

Green = worker_image(:,:,2);

Blue = worker_image(:,:,3);

[yRed, ~] = imhist(Red);

[yGreen, ~] = imhist(Green);

[yBlue, x] = imhist(Blue);

yRed(x>250)=0;

yGreen(x>250)=0;

yBlue(x>250)=0;

figure()

plot(x, yRed, 'Red', x, yGreen, 'Green', x, yBlue, 'Blue');

legend('Red Channel', 'Green Channel', 'Blue Channel')

title('Color Distribution of Isolated PPE.jpg')

xlabel('intensity')

ylabel('frequency')

xlim([0,255])

Ex_red_min = 180;

Ex_red_max = 250;

Ex_green_min = 200;

Ex_green_max = 240;

Ex_blue_min = 130;

Ex_blue_max = 170;

sample_worker_image = imread("~/Downloads/Assets/Worker_wearing_PPE.jpg");

PPE_region = zeros(size(sample_worker_image,1,2));

PPE_region(sample_worker_image(:,:,1) >= Ex_red_min & ...

sample_worker_image(:,:,1) <= Ex_red_max & ...

sample_worker_image(:,:,2) >= Ex_green_min & ...

sample_worker_image(:,:,2) <= Ex_green_max & ...

sample_worker_image(:,:,3) >= Ex_blue_min & ...

sample_worker_image(:,:,3) <= Ex_blue_max)=1;

imshow(PPE_region)

[height,width,numColorChannels] = size(worker_image);

fprintf("height of the image is %d pixels, width of the image is %d pixels, and there are %d color channels (red, green, blue)",height,width,numColorChannels);

imshow(worker_image)

axis on

worker_lab = rgb2lab(worker_image);

lab_a = worker_lab(:,:,2);

lab_a = lab_a(:);

lab_b = worker_lab(:,:,3);

lab_b = lab_b(:);

red = worker_image(:,:,1);

green = worker_image(:,:,2);

blue = worker_image(:,:,3);

color = [red(:), green(:), blue(:)];

color = double(color)/255;

figure()

set(gcf,'position',[0,0,1000,500]);

pos1 = [0.05,0.25,0.4,0.6];

pos2 = [0.5,0.05,0.25,0.8];

subplot('Position',pos1)

image(worker_image)

title('PPE Region.jpg')

axis equal

axis off

subplot('Position',pos2)

s = scatter(lab_a,lab_b,8,color, 'filled');

s.MarkerFaceAlpha = 0.05;

title('Abstract Color Representation')

axis equal

axis off

Ex_red_min = 180;

Ex_red_max = 250;

Ex_green_min = 200;

Ex_green_max = 240;

Ex_blue_min = 130;

Ex_blue_max = 170;

sample_worker_image2 = imread("~/Downloads/Assets/Worker_not_wearing_PPE.jpg");

PPE_region = zeros(size(sample_worker_image2,1,2));

PPE_region(sample_worker_image(:,:,1) >= Ex_red_min & ...

sample_worker_image(:,:,1) <= Ex_red_max & ...

sample_worker_image(:,:,2) >= Ex_green_min & ...

sample_worker_image(:,:,2) <= Ex_green_max & ...

sample_worker_image(:,:,3) >= Ex_blue_min & ...

sample_worker_image(:,:,3) <= Ex_blue_max)=1;

imshow(PPE_region)

[B_BB_x_min,B_BB_y_min,B_BB_x_range,b_BB_y_range] = calculateBoundingBoxes(PPE_region, sample_worker_image);

figure()

imshow(sample_worker_image)

hold on

rectangle('Position', [B_BB_x_min,B_BB_y_min,B_BB_x_range,b_BB_y_range + 35],'EdgeColor','r','LineWidth',3) ;

text(B_BB_x_min+5,B_BB_y_min-20,"PPE Detected.",'Color','red','FontSize',14);

%The below function is where I'm getting an error, can you please help me out to detect BB_y_min since I'm getting "Nan" as it's value.

function [BB_x_min,BB_y_min,BB_x_range,BB_y_range] = calculateBoundingBoxes(PPE_region, sample_worker_image)

x_sums = sum(PPE_region,1);

BB_x_min = nan;

BB_x_range = 0;

BB_x_range_subtract = 0;

x_range_start = 0;

for x = 2:size(sample_worker_image,2)

if x_sums(x-1) == 0 && x_sums(x)>0 && isnan(BB_x_min)

BB_x_min = x;

x_range_start = 1;

elseif x_sums(x-1) == 0 && x_sums(x)>0

BB_x_range_subtract = 0;

end

if x_range_start == 1

BB_x_range = BB_x_range + 1;

end

if x_range_start == 1 && x_sums(x-1) == 0

BB_x_range_subtract = BB_x_range_subtract + 1;

end

end

BB_x_range = BB_x_range - BB_x_range_subtract;

y_sums = sum(PPE_region,2);

BB_y_min = nan;

BB_y_range = 0;

BB_y_range_subtract = 0;

y_range_start = 0;

for y = 2:size(sample_worker_image,1)

if y_sums(y-1) == 0 && y_sums(y)>0 && isnan(BB_y_min)

BB_y_min = y;

y_range_start = 1;

elseif y_sums(y-1) == 0 && y_sums(y)>0

BB_y_range_subtract = 0;

end

if y_range_start == 1

BB_y_range = BB_y_range + 1;

end

if y_range_start == 1 && y_sums(y-1) == 0

BB_y_range_subtract = BB_y_range_subtract + 1;

end

disp(BB_y_range)

disp(BB_y_min)

disp(BB_x_min)

disp(BB_x_range)

disp(BB_y_range)

end

BB_y_range = BB_y_range - BB_y_range_subtract;

end

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!