MATLAB Answers

Remove white space from around image

74 views (last 30 days)
Martin
Martin on 14 Dec 2011
Edited: Walter Roberson on 22 May 2018
Hello, I'm pretty new to MatLab, so I'm having a hard time doing some things with it. I have a grayscale jpg-image, with a very large white space around the actual image, which is dark. Is there an easy command, to remove the colors <250 from the image?
Regards, Martin

  0 Comments

Sign in to comment.

Answers (4)

Sean de Wolski
Sean de Wolski on 14 Dec 2011
I(I>250) = 0; %sets them to black.
To to crop them you'll have to use a cropping tool such as imcrop or know the indices you wish to crop to.

  0 Comments

Sign in to comment.


Reza Farrahi Moghaddam
Reza Farrahi Moghaddam on 24 Feb 2012
You can try this tiny tool to remove white space around images:

  0 Comments

Sign in to comment.


hf fh
hf fh on 22 May 2018
Edited: Walter Roberson on 22 May 2018
But the problem is I have 20 images and I want to remove white space around images: How to apply 20 images to you the way I try but I can not..
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%dir folder
input_filename = '/Users/mac/Desktop/RemoveWhiteSpace/RemoveWhiteSpace/';
% % Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isdir(input_filename)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', input_filename);
uiwait(warndlg(errorMessage));
return;
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(input_filename, '*.png'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 :20
baseFileName = theFiles(k).name;
fullFileName = fullfile(input_filename, baseFileName);
u_in = mat2gray(imread(fullFileName));
imshow(u_in);
figure(k),
imshow(RemoveWhiteSpace(u_in));
RemoveWhiteSpace([], 'file', '*.png', 'output', 'test_out*.png');
saveas(figure(k),sprintf('%d.tif',k))
end

  0 Comments

Sign in to comment.


hf fh
hf fh on 22 May 2018
Edited: Walter Roberson on 22 May 2018
this good for 100 images
folder = '/Users/mac/Desktop/fiel2/RemoveWhiteSpace/RemoveWhiteSpace/semple';
%image3d = zeros(300, 300, 99, 'uint8');
for k = 1 : 99
% Read one image.
u_in = mat2gray(imread( fullfile(folder, sprintf('%d.tif', k) )));
%figure, imshow(u_in);
figure(k), imshow(RemoveWhiteSpace(u_in));
RemoveWhiteSpace([], 'file', sprintf('%d.tif', k) , 'output', sprintf('%d.png', k));
end

  0 Comments

Sign in to comment.

Sign in to answer this question.