background removal from image

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 个评论

Hi , is there any comments or correction for my code ? please
Not until you attach 'dkloriginalimg.jpg'.
I'm not sure what you mean. Your code runs and it's all in one script file. It does create several figures, but I assume you want that because you know how to use subplot() and you did it for the final figure.
Actually my question is I have used color thresholding method to remove background from the image, i want to calculate the histogram only from the facepixels and not from background... but when you look at the final image the calculation is including background. I dont want the analysis with background
@Image Analyst : May be i can put my question in a this way like, how do i remove the foreground(person's face) from the background, then do my further analysis only with the foreground image ?.

请先登录,再进行评论。

回答(1 个)

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 个评论

hey Subhadeep, thanks for the help. but when I use the code there is a intrusion of green color near the ears. also, when I use it and paste it before the split histogram code, the final window still shows the bacground involvement in the histogram...
How do I carry this object masked image until my last stage of analysis in splithistogram split: This is the issue. Now I am confused how to use only face pixels to do my entire project.
Is there any hope for my issue?
@ Malini Bakthavatchalam Refer the code below,
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!

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Image Segmentation and Analysis 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by