MATLAB Answers

Defining human figure in an image as exactly shown in output with defimed codes in description -Homework-

1 view (last 30 days)
image processor processor
image processor processor on 12 Jun 2021
Answered: Joaquin Quintana on 21 Jun 2021
Hello dear masters;
I had tried to do as seen in below yet I could not get even close.
I have a homework with image processing class, which I should use two of defined codes with input image to take an output image as shown in attachments.
Code list;
1- imerode
2-imdilate
3-imopen
4-imclose
5-bwhitmiss
6-bwmorph
7-imfill
8-imreconstruct
9-imclearborder
Thanks advance.
close all
clear all
clc
A = imread('insan.jpg');
B = rgb2gray(A);
C=double(B);
%Sobel filter
for i=1:size(C,1)-2
for j=1:size(C,2)-2
%Sobel mask for x-direction:
Gx=((2*C(i+2,j+1)+C(i+2,j)+C(i+2,j+2))-(2*C(i,j+1)+C(i,j)+C(i,j+2)));
%Sobel mask for y-direction:
Gy=((2*C(i+1,j+2)+C(i,j+2)+C(i+2,j+2))-(2*C(i+1,j)+C(i,j)+C(i+2,j)));
%The gradient of the image
B(i,j)=sqrt(Gx.^2+Gy.^2);
end
end
figure(1); subplot(1,3,1), imshow(B); title('Sobel gradient');
se = strel('disk',10)
background = imopen(B,se);
I2 = B - background
% BW2=~BW2
subplot(1,3,2); imshow(I2);
B=im2bw(I2);
g1= bwmorph(B, 'thin',0.05);
subplot(1,3,3);imshow(g1);

Answers (1)

Joaquin Quintana
Joaquin Quintana on 21 Jun 2021
Hello - here is a quick way of doing this. It uses imadjust and imreconstruct to acquire the desired output. The image shown is the your provided desired output image and the image created from the code provided here overlaid. Imshowpair is used to overlay the images and the green and purple show where the images differ.
Hope it helps.
Code used
close all;
clear;
clc;
%read in users images
input_img = im2double(rgb2gray(imread('input_image.jpg')));
desired_output = imbinarize(rgb2gray(imread('desired_output_image.jpg')));
%check the shape of the images
shapeA = size(input_img)
shapeB = size(desired_output)
%resize image to be the same dimensions
if ~(size(input_img) == size(desired_output))
desired_output = imresize(desired_output,shapeA);
end
%adjust image intensity to something useful for identifying figure
adjusted = imadjust(input_img, [0.0 0.25]);
%create binary of adjusted image
BW = ~imbinarize(adjusted);
% place markers on the person head, body and buildings roof (objects user wants to keep)
% pass markers to imreconstruct to create final image
marker = false(size(BW));
marker(170,250) = true;
marker(220,270) = true;
marker(120,30) = true;
reconstructed = imreconstruct(marker,BW);
%this is the desired output made from the input image
figure
imshow(reconstructed)
title('output')
%show the differences between the users requested output and what was created
figure
imshowpair(reconstructed,desired_output)
title('overlaid output with desired output from user')
%read in users images
input_img = im2double(rgb2gray(imread('input_image.jpg')));
desired_output = imbinarize(rgb2gray(imread('desired_output_image.jpg')));
%check the shape of the images
shapeA = size(input_img)
shapeB = size(desired_output)
%resize image to be the same dimensions
if ~(size(input_img) == size(desired_output))
desired_output = imresize(desired_output,shapeA);
end
%adjust image intensity to something useful for identifying figure
adjusted = imadjust(input_img, [0.0 0.25]);
%create binary of adjusted image
BW = ~imbinarize(adjusted);
% place markers on the person head, body and buildings roof (objects user wants to keep)
% pass markers to imreconstruct to create final image
marker = false(size(BW));
marker(170,250) = true;
marker(220,270) = true;
marker(120,30) = true;
reconstructed = imreconstruct(marker,BW);
%this is the desired output made from the input image
figure
imshow(reconstructed)
title('output')
%show the differences between the users requested output and what was created
figure
imshowpair(reconstructed,desired_output)
title('overlaid output with desired output from user')

Community Treasure Hunt

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

Start Hunting!