Remove White border from Image

78 views (last 30 days)
LinusL
LinusL on 13 Aug 2021
Answered: Image Analyst on 14 Aug 2021
How do i remove white border from image as my output image when merging images display an output with a white border.
or is there a way to remove white border from images?
Any expert can offer me guidance how to remove white border from images
Thanks.
  1 Comment
LinusL
LinusL on 13 Aug 2021
i using gcf to save the image but when it display it show a white border
saveas(gcf, 'setCombine/Pokemon#' + count + '.png')

Sign in to comment.

Answers (3)

Simon Chan
Simon Chan on 13 Aug 2021
Try function imcrop
  2 Comments
Simon Chan
Simon Chan on 14 Aug 2021
Now I understand, the accepted answer in this Link may help you.

Sign in to comment.


Kristin Habersang
Kristin Habersang on 13 Aug 2021

Image Analyst
Image Analyst on 14 Aug 2021
Try this:
% Demo by Image Analyst, August, 2021.
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.
folder = [];
baseFileName = 'image.PNG';
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~isfile(fullFileName)
% 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
fullFileName = fullFileNameOnSearchPath;
end
rgbImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(rgbImage)
% Display the image.
subplot(2, 2, 1);
imshow(rgbImage, []);
axis('on', 'image');
caption = sprintf('Original Image : "%s"', baseFileName);
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';
%--------------------------------------------------------------------------------------------------------
% Threshold the image.
grayImage = rgb2gray(rgbImage);
% Display the image.
subplot(2, 2, 2);
% imhist(grayImage);
imshow(grayImage, []);
axis('on', 'image');
caption = sprintf('Gray Scale Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
mask = grayImage < 230; % Determined from impixelinfo or histogram.
% Take largest blob only.
mask = bwareafilt(mask, 1);
% Display the image.
subplot(2, 2, 3);
imshow(mask, []);
axis('on', 'image');
caption = sprintf('Mask Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Get bounding box
props = regionprops(mask, 'BoundingBox');
% Crop the image.
croppedImage = imcrop(rgbImage, props.BoundingBox);
% Display the image.
subplot(2, 2, 4);
imshow(croppedImage, []);
axis('on', 'image');
caption = sprintf('Cropped Image ');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.

Community Treasure Hunt

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

Start Hunting!