HOW TO CROP AN IMAGE

7 views (last 30 days)
federica pasquali
federica pasquali on 11 Aug 2018
Answered: Image Analyst on 11 Aug 2018
Dear all,
I've an image saved in a uint8 variable and I want to remove the zeros from it (the zeros are the black spaces of the image) to obtain only the image without black space.
I've tried this but it doesn't work because instead of removing the black spaces, it takes away the colors of the image :
TOT=sum(IMG);
tolgo=find(TOT==0);
IMG(:,tolgo)=[];
TOT=sum(IMG');
tolgo=find(TOT'==0);
IMG(tolgo,:)=[];
Thanks.

Accepted Answer

KALYAN ACHARJYA
KALYAN ACHARJYA on 11 Aug 2018
Edited: KALYAN ACHARJYA on 11 Aug 2018
%Try this one for Gray Image
image(image(:, :)=0, :)=[]
  7 Comments
federica pasquali
federica pasquali on 11 Aug 2018
preferably I would like to remove the black parts and keep the clear image as much as possible
Image Analyst
Image Analyst on 11 Aug 2018
A hand doing what? Evidently this code already does what you want because you accepted it. Do you have a second question? We're willing to help with that. If it's not a followup question to this one, then just post the new question in a new post.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 11 Aug 2018
Here is code to crop away the rectangular black padding on the edges of the image:
% Code to crop off any black padding on the edges of the image.
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 = 20;
%=======================================================================================
% Read in image.
fullFileName = fullfile(pwd, 'prova.jpg');
[folder, baseFileName, ext] = fileparts(fullFileName);
rgbImage = imread(fullFileName);
% Shrink it to speed it up
% rgbImage = imresize(rgbImage, 0.75);
% Get the dimensions of the image.
[rows, columns, numberOfColorChannels] = size(rgbImage);
% Display the original image.
subplot(2, 2, 1);
imshow(rgbImage, []);
axis on;
caption = sprintf('Original Color Image, %s: %d rows by %d columns', baseFileName, rows, columns);
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')
% Get a mask by finding all pixels that are pure black (0,0,0).
blackPixels = max(rgbImage, [], 3) > 0;
% Display the original image.
subplot(2, 2, 2);
imshow(blackPixels, []);
axis on;
caption = sprintf('Black Pixels');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Find coordinates of all non-black pixels.
[blackRows, blackColumns] = find(blackPixels);
% Crop the image.
croppedImage = rgbImage(blackRows(1):blackRows(end), blackColumns(1):blackColumns(end), :); % Crop
% Get the dimensions of the image.
[rows2, columns2, numberOfColorChannels] = size(croppedImage);
% Display the cropped image.
subplot(2, 2, 3);
imshow(croppedImage, []);
axis on;
caption = sprintf('Cropped Image, %s: %d rows by %d columns', baseFileName, rows2, columns2);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
Note: no colors interior to the cropping rectangle are altered.

Community Treasure Hunt

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

Start Hunting!