MATLAB Answers

deleting leadind and trail zeros in an image

47 views (last 30 days)
Hi all I am working on enhancing a grayscal images, the first step should be cropping the image by removing lead and trail zeros (zeros rows an columns surrounding the image). i want t remove the surrounding zeros. please be advised that the cropping has be applied on zeros rows and columns that surrounding the image for different images, so i need a function or an algorithm does this cropping on different images. thanks in advance

  0 Comments

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 21 Nov 2017
Try this:
mask = grayImage ~= 0; % Find all zeros, even those inside the image.
mask = imfill(mask, 'holes'); % Get rid of zeros inside image.
% Invert mask and get bounding box.
props = regionprops(~mask, 'BoundingBox');
% Crop image.
croppedImage = imcrop(grayImage, props.BoundingBox);

  6 Comments

Show 3 older comments
Hassan  Qassim
Hassan Qassim on 22 Nov 2017
Dear My Friend thanks a lot for your help, your efforts are really appreciated. please have a look at attached image, i want to crop it as indicated in red rectangle. i am only interested in the part inside the red rectangle. looking forward for your answer. thanks in advance
Image Analyst
Image Analyst on 22 Nov 2017
Try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format short g;
format compact;
fontSize = 25;
%===============================================================================
% Get the name of the image the user wants to use.
baseFileName = 'test1-paint.jpg';
% Get the full filename, with path prepended.
folder = pwd
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% 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
end
%===============================================================================
% Read in demo image.
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage)
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
% grayImage = rgb2gray(grayImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the original image.
subplot(2, 2, 1);
imshow(grayImage, []);
axis on;
caption = sprintf('Original Color 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.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0.05 1 0.95]);
% 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.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
% Find the white surround.
whiteSurround = grayImage > 245;
% Take the largest blob only.
whiteSurround = bwareafilt(whiteSurround, 1);
% Display the image.
subplot(2, 2, 2);
imshow(whiteSurround, []);
axis on;
caption = sprintf('White Surround Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo();
drawnow;
% Erase image where there is the white surround.
grayImage(whiteSurround) = 0;
% Find the non-black pixels.
mask = grayImage > 7;
% Take the largest blob only.
mask = bwareafilt(mask, 1);
% Display the image.
subplot(2, 2, 3);
imshow(mask, []);
axis on;
caption = sprintf('Mask Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo();
drawnow;
% Find the bounding box
props = regionprops(mask, 'BoundingBox');
% Crop the image to the bounding box.
croppedImage = imcrop(grayImage, props.BoundingBox);
% Display the image.
subplot(2, 2, 4);
imshow(croppedImage, []);
axis on;
caption = sprintf('Cropped Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo();
drawnow;
Hassan  Qassim
Hassan Qassim on 24 Nov 2017
thanks a lot my dear friend. that worked very well. your information were very clear and very helpful. thanks again

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!