Remove White border from Image

조회 수: 54 (최근 30일)
LinusL
LinusL 2021년 8월 13일
편집: DGM 2023년 5월 1일
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
LinusL
LinusL 2021년 8월 13일
i using gcf to save the image but when it display it show a white border
saveas(gcf, 'setCombine/Pokemon#' + count + '.png')

댓글을 달려면 로그인하십시오.

답변 (4개)

Simon Chan
Simon Chan 2021년 8월 13일
Try function imcrop
  댓글 수: 2
LinusL
LinusL 2021년 8월 13일
is there a way to automatic crop, since imcrop need specific axis
Simon Chan
Simon Chan 2021년 8월 14일
Now I understand, the accepted answer in this Link may help you.

댓글을 달려면 로그인하십시오.


Kristin Habersang
Kristin Habersang 2021년 8월 13일
  댓글 수: 1
LinusL
LinusL 2021년 8월 13일
it does not help me

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2021년 8월 14일
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 2023년 5월 1일
편집: DGM 2023년 5월 1일
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.

카테고리

Help CenterFile Exchange에서 Explore and Edit Images with Image Viewer App에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by