removing background of the color picture
조회 수: 6 (최근 30일)
이전 댓글 표시
Hi, i am working on a project, were I have to calculate the pixels inside the object only, my image contains a human face and a white background, all I want to do is to remove the background and save only the face of the person and calculate histograms from the face pixels only for my project. I have tried a thresholding method, so that kind of removes background and gives a black color as background. And i have used imwrite function to save the image and use it for further analysis. but the problem, I am not sure whether I am calculating only from the face pixels or the entire image including black as background. I am attaching my code here. kindly help me clear my doubt
%%threshold the image
I = imread('facergbimg.jpg');
imshow(I)
[BW rgb]= createMaskbcgremoval(I); this is a bckground removal code done with the use of image proccessing color threshold from the math works.
figure,imshow(rgb)
imwrite(rgb,'file.jpg')
% % save the image in a folder
% % work with the background removal image working only with face pixels
% % separate image into 3 channels
img = imread('file.jpg'); % Read image
red = img(:,:,1); % Red channel
green = img(:,:,2); % Green channel
blue = img(:,:,3); % Blue channel
a = zeros(size(img, 1), size(img, 2));
just_red = cat(3, red, a, a);
just_green = cat(3, a, green, a);
just_blue = cat(3, a, a, blue);
back_to_original_img = cat(3, red, green, blue);
figure, imshow(img), title('Original image')
figure, imshow(just_red), title('Red channel'),imwrite(just_red,'just_red.jpg')
figure, imshow(just_green), title('Green channel'),imwrite(just_green,'just_green.jpg')
figure, imshow(just_blue), title('Blue channel'),imwrite(just_blue,'just_blue.jpg')
%% now split the histogram
G =imread('just_green.jpg');
K_median_cal= reshape(G,[],1);
total_median = mean(K_median_cal);
K(G<total_median) = G(G<total_median);
P(G>total_median) = G(G>total_median);
K = uint8(double(total_median) .* ones(size(G)));
P = uint8(double(total_median) .* ones(size(G)));
for i=1:length(G(:,1))
for j=1:length(G(1,:))
if G(i,j)<total_median
K(i,j)=G(i,j);
else
P(i,j)=G(i,j);
end
end
end
figure(1)
imshow(G)
figure(2)
imshow(K)
title('lowerrectified');
figure(3)
imshow(P)
title('upper rectified');
figure(4)
imhist(G)
figure(5)
imhist(K)
ylim([0 10000])
figure(6)
imhist(P)
ylim([0 10000])
댓글 수: 0
채택된 답변
Image Analyst
2020년 5월 3일
"whether I am calculating only from the face pixels or the entire image including black as background." <== you are calculating total_median from G, which is the green channel of the background-removed image that you wrote to disk. I must say though that it looks like a very roundabout way of coding, like why write the rgb matrix out to disk only to read it back in as img? Etc.
댓글 수: 3
Image Analyst
2020년 5월 3일
Not sure what you want but to compute the median of each color channel, you'd do this:
medianR = median(red);
medianG = median(green);
medianB = median(blue);
I can't figure out how you're computing total_median or what it even means.
And I don't know exactly what "see how the transmittion is happening from black to each color channel" means. Do you want code to zoom in to see the black-to-colored region in more detail? If so, see the attached zooming demo.
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!