How can I remove white background from this Image?
19 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Riddhiben Joshi
le 17 Sep 2021
Commenté : Riddhiben Joshi
le 4 Oct 2021
I am working on a project where I need to remove the back from similar images in order to stack randomly them over a 100um x 100um matrix.
Thank you!
2 commentaires
Image Analyst
le 17 Sep 2021
What does "remove" mean to you? Set to black? Crop?
What does "stack" mean to you? Copy and paste?
Réponse acceptée
yanqi liu
le 26 Sep 2021
sir, may be remove the background, and make some add function, such as
clc;
clear all;
close all;
im = imread('https://ww2.mathworks.cn/matlabcentral/answers/uploaded_files/741089/20um.jpeg');
im2 = rgb2hsv(im);
bw = im2bw(im2(:,:,2), 0.5);
bw = imfill(bw, 'holes');
im = im .* uint8(cat(3,bw,bw,bw));
% add some images
im = imadd(im, im);
figure; imshow(im);
2 commentaires
Plus de réponses (1)
Image Analyst
le 29 Sep 2021
Try this:
% Demo by Image Analyst.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
fprintf('Beginning to run %s.m ...\n', mfilename);
%-----------------------------------------------------------------------------------------------------------------------------------
% Read in image. This is a horrible image. NEVER use JPG format for image analysis. Use PNG, TIFF, or BMP instead.
folder = [];
baseFileName = '20um.jpeg';
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(rgbImage)
% Display the image.
subplot(2, 2, 1);
imshow(rgbImage, []);
axis('on', 'image');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
caption = sprintf('Original RGB Image : "%s"\n%d rows by %d columns', baseFileName, rows, columns);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Set up figure properties:
% Enlarge figure to full screen.
hFig1 = gcf;
hFig1.Units = 'Normalized';
hFig1.WindowState = 'maximized';
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
hFig1.Name = 'Demo by Image Analyst';
% Get a binary image.
grayImage = rgb2gray(rgbImage);
binaryImage = ~imbinarize(grayImage, 'global');
% Remove frame around the outside.
binaryImage = imclearborder(binaryImage);
% Fill holes
binaryImage = imfill(binaryImage, 'holes');
subplot(2, 2, 2);
imshow(binaryImage);
% Get the bounding box.
props = regionprops(binaryImage, 'BoundingBox');
% Crop the images.
croppedImage = imcrop(rgbImage, props.BoundingBox);
binaryImage = imcrop(binaryImage, props.BoundingBox);
% Mask the image using bsxfun() function to multiply the mask by each channel individually. Works for gray scale as well as RGB Color images.
croppedImage = bsxfun(@times, croppedImage, cast(binaryImage, 'like', croppedImage));
% Display the image.
subplot(2, 2, 3);
imshow(croppedImage, []);
axis('on', 'image');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
title('Cropped Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
msgbox('Done!');
Voir également
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!