Effacer les filtres
Effacer les filtres

HOW TO CROP AN IMAGE

6 vues (au cours des 30 derniers jours)
federica pasquali
federica pasquali le 11 Août 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.

Réponse acceptée

KALYAN ACHARJYA
KALYAN ACHARJYA le 11 Août 2018
Modifié(e) : KALYAN ACHARJYA le 11 Août 2018
%Try this one for Gray Image
image(image(:, :)=0, :)=[]
  7 commentaires
federica pasquali
federica pasquali le 11 Août 2018
preferably I would like to remove the black parts and keep the clear image as much as possible
Image Analyst
Image Analyst le 11 Août 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.

Connectez-vous pour commenter.

Plus de réponses (1)

Image Analyst
Image Analyst le 11 Août 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.

Catégories

En savoir plus sur Read, Write, and Modify Image dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by