MATLAB Answers

Erasing the boarders in an Image

조회 수: 1(최근 30일)
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;
%% Initalize the data
dataDir= fullfile('Data/');
exts = {'.jpg','.png','.tif','BMP'};
imds = imageDatastore(fullfile(dataDir),...
[TrainData, TestData] = splitEachLabel(imds,0.5);
numImages = numel(TrainData.Files);
for i = 1:numImages
img = readimage(TrainData, i);
img3= im2double(imresize(img, [100 100], 'bicubic'));

채택된 답변

Image Analyst
Image Analyst 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
Image Analyst
Image Analyst 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.
% 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);
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);
% Display the original image.
subplot(2, 1, 1);
axis on;
caption = sprintf('Original Image : %s', baseFileName);
title(caption, 'FontSize', fontSize);
% 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);
title('With black border made white', 'FontSize', fontSize);

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

추가 답변(1개)

Bjorn Gustavsson
Bjorn Gustavsson 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
  댓글 수: 1
Bjorn Gustavsson
Bjorn Gustavsson 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;

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

Community Treasure Hunt

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

Start Hunting!

Translated by