Erasing the boarders in an Image

2 views (last 30 days)
Hi
I would if it is possible in MATLAB to remove the black sides on the boarder of this image basically on right side and bottem side
Herein the code
close all;
clc;
%% Initalize the data
dataDir= fullfile('Data/');
exts = {'.jpg','.png','.tif','BMP'};
imds = imageDatastore(fullfile(dataDir),...
'IncludeSubfolders',true,'FileExtensions','.BMP','LabelSource','foldernames');
countEachLabel(imds);
[TrainData, TestData] = splitEachLabel(imds,0.5);
size(TrainData);
countEachLabel(TrainData);
numImages = numel(TrainData.Files);
for i = 1:numImages
img = readimage(TrainData, i);
img=rgb2gray(img);
img3= im2double(imresize(img, [100 100], 'bicubic'));
end

Accepted Answer

Image Analyst
Image Analyst on 12 Nov 2020
Do you want to
  1. crop it away, which will give a different size image?
  2. Or set it to white, which would leave the image the same size?
Also, what does "Enhacing" mean? No such word. Does it mean "enhancing" or "erasing"? If erasing, you need to answer my questions as to how you want to erase (get rid of) the black border. If enhancing, you need to define exactly what that means.
To crop you could use imcrop() but it's probably easiest to just use indexing:
img = img(row1 : row2, col1 : col2, :); % Works for either rgb or grayscale image.
I assume you can find the rows and columns and that it's known and fixed for all images. If it moves around and changes size (which is doubtful but possible), you'd need code to find them.
  2 Comments
Abdussalam Elhanashi
Abdussalam Elhanashi on 12 Nov 2020
Edited: Abdussalam Elhanashi on 12 Nov 2020
Hi Image Analyst
Thanks for your answer
I want to set it to white, which would leave the image the same size . How can i do that?
Yes I mean Erasing. I modified the question
Image Analyst
Image Analyst on 12 Nov 2020
Try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
%===============================================================================
% Read in gray scale image.
folder = pwd;
baseFileName = 'image.bmp';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows, columns, numberOfColorBands] = size(grayImage);
% If it's RGB instead of grayscale, convert it to gray scale.
if numberOfColorBands > 1
grayImage = rgb2gray(grayImage);
end
% Display the original image.
subplot(2, 1, 1);
imshow(grayImage);
axis on;
caption = sprintf('Original Image : %s', baseFileName);
title(caption, 'FontSize', fontSize);
impixelinfo;
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0.1, 1, 0.9]);
% Find out where all pixels in the rows are zero
allZeroRows = all(grayImage == 0, 2);
% Find out where all pixels in the rows are zero
allZeroColumns = all(grayImage == 0, 1);
% Set those rows to white.
grayImage(allZeroRows, :) = 255;
% Set those columns to white.
grayImage(:, allZeroColumns) = 255;
subplot(2, 1, 2);
imshow(grayImage);
title('With black border made white', 'FontSize', fontSize);

Sign in to comment.

More Answers (1)

Bjorn Gustavsson
Bjorn Gustavsson on 12 Nov 2020
Simply crop out the black frame. Something like this:
Img_frameless = Img_in(y_min:y_max,x_min:x_max); % for a grayscale image
Img_framelessRGB = Img_rgb(y_min:y_max,x_min:x_max,:); % for an RGB-image
HTH
  1 Comment
Bjorn Gustavsson
Bjorn Gustavsson on 12 Nov 2020
if you want to set the border to white just do this:
Imax = max(Img_in(:)); % just to get the peak intensity, which might depend on data-type
Img_WB = Img_in;
Img_WB(1:top_row_lb,:,:) = Imax;
Img_WB(bot_row_ub:end,:,:) = Imax;
Img_WB(:,1:left_col_rb,:) = Imax;
Img_WB(:,right_col_lb:end,:,:) = Imax;
HTH

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!