I want to change the background to white, can someone help me pls
21 次查看(过去 30 天)
显示 更早的评论
I=imread('Fig2.jpg');
I2=imread('Fig3.jpg');
I3=imabsdiff(I,I2);
Ir=I3(:,:,1);
Ig=I3(:,:,2);
Ib=I3(:,:,3);
Ir((400:420),(1:290))=0;
Ir((400:480),(382:640))=0;
Ib((400:420),(1:290))=0;
Ib((400:480),(382:640))=0;
Ig((400:420),(1:290))=0;
Ig((400:480),(382:640))=0;
I4=cat(3,Ir,Ig,Ib);
subplot(2,2,1)
imshow(I), title('Figura.2 Original')
subplot(2,2,2)
imshow(I2), title('Figura.3 Original')
subplot(2,2,3)
imshow(I3), title('Subtracção')
subplot(2,2,4)
imshow(I4), title('Resultado Final')
回答(2 个)
Image Analyst
2021-5-22
Try this:
clc; % Clear command window.
fprintf('Running %s.m ...\n', mfilename);
clear; % Delete all variables.
close all; % Close all figure windows except those created by imtool.
workspace; % Make sure the workspace panel is showing.
I=imread('Fig2.jpg');
I2=imread('Fig3.jpg');
subplot(2, 3, 1)
imshow(I)
impixelinfo;
title('Figura.2 Original')
subplot(2, 3, 2)
imshow(I2)
impixelinfo;
title('Figura.3 Original')
I3=imabsdiff(I,I2);
subplot(2, 3, 3)
imshow(I3)
impixelinfo;
title('Subtracção')
% Get the histogram
subplot(2, 3, 4);
histogram(I3);
grid on;
title('Histogram of Subtraction')
% Create mask
threshold = 10;
xline(threshold, 'Color', 'r', 'LineWidth', 2);
mask = rgb2gray(I3) > threshold;
% Take largest blob only.
mask = bwareafilt(mask, 1);
% Fill holes
mask = imfill(mask, 'holes');
subplot(2, 3, 5)
imshow(mask)
title('Mask')
% Make a white image
whiteImage = 255 * ones(size(mask), 'uint8');
whiteImage(mask) = 0;
% Mask the image using bsxfun() function to multiply the mask by each channel individually. Works for gray scale as well as RGB Color images.
maskedRgbImage = I2 + whiteImage;
subplot(2, 3, 6)
imshow(maskedRgbImage)
title('Resultado Final')
% Maximize window
g = gcf;
g.WindowState = 'maximized';
fprintf('Done running %s.m\n', mfilename);
0 个评论
DGM
2021-5-22
编辑:DGM
2021-5-22
Well, I'm not sure where this is going, so I'll just throw out some things.
I=imread('Fig2.jpg');
I2=imread('Fig3.jpg');
I3=imabsdiff(I,I2);
% this doesn't need to be done per-channel
I4 = I3;
I4((400:420),(1:290),:)=0;
I4((400:480),(382:640),:)=0;
% get rid of the rest of the bg noise using a crude mask
m = bwareafilt(rgb2gray(I4)>10,1);
m = imfill(imclose(m,strel('disk',5)),'holes');
I4 = I4.*uint8(m); % apply the mask
Now that the bg is cleaned up somewhat, we have to ask what "making the bg white" actually means. There are probably other ways that can be interpreted, but this is what I came up with.
% you could change the surrounding BG to white using the mask
% but it probably won't be very good since the bottle is clear
% and the masking wasn't very tight
% if segmentation is the goal, more attention should be paid
% to making a good mask. i'm assuming compositing is the goal.
I4wb = I4 + 255*ones(size(I4),'uint8').*uint8(~m);
% or you could try to convert the bg to alpha by color
I5 = color2alpha(I4,[0 0 0]);
% maybe even adjust the alpha a bit to compensate for the bottle being clear
I5(:,:,4) = imadjust(I5(:,:,4));
The image is transparent. Right click to view the image without the webpage bg filling it in.
% then you could combine the image with a solid white bg or something else
gradpict = lingrad([480 640 3],[0 1; 1 0],[0 0 1; 1 0 0]*255,'cosine','uint8');
I6 = imblend(I5,gradpict,1,'normal');
color2alpha(), lingrad(), and imblend() are from MIMT, which is here:
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!