How can I remove white background from this Image?

21 views (last 30 days)
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 Comments
Image Analyst
Image Analyst on 17 Sep 2021
What does "remove" mean to you? Set to black? Crop?
What does "stack" mean to you? Copy and paste?
Riddhiben Joshi
Riddhiben Joshi on 29 Sep 2021
Hi Thank you for responding, by remove i mean to crop the white background portion and only have blue part.
And by stack i meanto say place next to each other in a 2D such that i can control distance between them. I have 7 different images which i wish to stack randomly.
I hope this helps!

Sign in to comment.

Accepted Answer

yanqi liu
yanqi liu on 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 Comments
Riddhiben Joshi
Riddhiben Joshi on 29 Sep 2021
Hi thank you for responding, I tried to use the above code code but I keep getting this error.
MAP must be a Mx3 array.
Error in rgb2hsv (line 36)
[r, g, b, isColorMap, isEmptyInput, isThreeChannel] = parseInputs(varargin{:});

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 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!');

Categories

Find more on Images in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!