Remove White border from Image

86 Ansichten (letzte 30 Tage)
LinusL
LinusL am 13 Aug. 2021
Bearbeitet: DGM am 1 Mai 2023
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 Kommentar
LinusL
LinusL am 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')

Melden Sie sich an, um zu kommentieren.

Antworten (4)

Simon Chan
Simon Chan am 13 Aug. 2021
Try function imcrop
  2 Kommentare
LinusL
LinusL am 13 Aug. 2021
is there a way to automatic crop, since imcrop need specific axis
Simon Chan
Simon Chan am 14 Aug. 2021
Now I understand, the accepted answer in this Link may help you.

Melden Sie sich an, um zu kommentieren.


Kristin Habersang
Kristin Habersang am 13 Aug. 2021

Image Analyst
Image Analyst am 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.

DGM
DGM am 1 Mai 2023
Bearbeitet: DGM am 1 Mai 2023
I can't believe nobody noticed what's going on here.
Saving images using figure capture will generally result in an image which has been uncontrollably:
  • padded by some amount which is dependent on image geometry, version, and environment
  • resized by some amount that's dependent on the window geometry and axes properties
  • converted from indexed color to RGB (if it were indexed)
  • converted from grayscale to pseudocolor RGB (if it were grayscale)
It's the same as taking a screenshot.
Don't save images using figure capture.
It's as simple as that. There's no point in cropping an image that has also suffered other damage. You fix the problem by not creating the problem in the first place. If you generated and saved a thousand bad images, you go back and regenerate them again and save them correctly.
Read images with imread().
Write images with imwrite().
Additionally, if you're going to post a working image on the forum, post the image itself, not a screenshot of the thumbnail of the image as it appears in your file browser.

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by