background removal from image
25 views (last 30 days)
Show older comments
Hi , I am working on a project in color illusion. And I want to remove the background from the image, the image is a person's face with white bakground, I used matlab function color thresholding to remove the background, after that I convertd the image color space to DKL space, then I started with my project, but still in my analysis the whole image is getting calculated . I want to work with the histogram of the person's face not the background. I am attaching my whole code here.. kindly help me.
2. since I am new to matlab, I learnt my codes in bits and pieces using tutorial, I want to know how to connect all my pieces together. Like starting from background removal, then using the background removed image to convert in different color space. finally using this color space image in calculating my histograms and creating illusion. so, I have written my codes in parts ... that does all this function but not together in one script. I am stuck with how to combine all these.
Kindl guide me through this
6 Comments
Answers (1)
Subhadeep Koley
on 27 May 2020
Malini, here is a demo script for "...histogram of the person's face not the background..."
close all
clc
img = imread('dkloriginalimg.jpg');
% Create a binary image mask
grayImg = rgb2gray(img);
maskBW = grayImg(:, :) ~= 0;
maskBw = imfill(maskBW, 'holes');
% Display original image
subplot(1, 3, 1)
imshow(img, [])
title('Original Image')
% Display the mask
subplot(1, 3, 2)
imshow(maskBw)
title('Binary mask')
% Calculate the histogram
pixelsWithinMask = img(maskBw);
[pixCnt, lavels] = imhist(pixelsWithinMask);
subplot(1, 3, 3)
bar(lavels, pixCnt)
title('Histogram of the masked image')
axis square
grid on
Hope this helps!
4 Comments
Subhadeep Koley
on 28 May 2020
close all; clear; clc;
img = imread('dkloriginalimg.jpg');
% Create a binary image mask
grayImg = rgb2gray(img);
maskBW = grayImg(:, :) ~= 0;
maskBW = imfill(maskBW, 'holes');
K_median_cal = reshape(img, [], 1);
total_median = median(K_median_cal);
% I have commnted these lines as K and P values are being overwritten
% below in the for loop. Uncomment if you wish so
% K(G<total_median) = G(G<total_median);
% P(G>total_median) = G(G>total_median);
K = uint8(double(total_median) .* ones(size(img)));
P = uint8(double(total_median) .* ones(size(img)));
for i = 1:length(img(:, 1))
for j = 1:length(img(1, :))
if img(i, j) < total_median
K(i, j) = img(i, j);
else
P(i, j) = img(i, j);
end
end
end
% Visualization
figure
imshow(img)
title('original DKL img')
figure
imshow(K)
title('lower rectified')
figure
imshow(P)
title('upper rectified')
figure
imhist(img(maskBW))
title('original hist')
figure
imhist(K(maskBW))
title('lower rectified hist')
ylim([0 10000])
figure
imhist(P(maskBW))
title('upper rectified hist')
ylim([0 10000])
figure
subplot(2, 3, 1)
imshow(K)
title('lower rectified')
subplot(2, 3, 2)
imshow(img)
title('original DKL img')
subplot(2, 3, 3)
imshow(P)
title('upper rectified')
subplot(2, 3, 4)
imhist(K(maskBW))
title('lower rectified hist')
subplot(2, 3, 5)
imhist(img(maskBW))
title('original hist')
subplot(2, 3, 6)
imhist(P(maskBW))
title('upper rectified hist')
Hope this helps!
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!