필터 지우기
필터 지우기

HOW TO CROP AN IMAGE

조회 수: 6 (최근 30일)
federica pasquali
federica pasquali 2018년 8월 11일
답변: Image Analyst 2018년 8월 11일
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.

채택된 답변

KALYAN ACHARJYA
KALYAN ACHARJYA 2018년 8월 11일
편집: KALYAN ACHARJYA 2018년 8월 11일
%Try this one for Gray Image
image(image(:, :)=0, :)=[]
  댓글 수: 7
federica pasquali
federica pasquali 2018년 8월 11일
preferably I would like to remove the black parts and keep the clear image as much as possible
Image Analyst
Image Analyst 2018년 8월 11일
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.

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

추가 답변 (1개)

Image Analyst
Image Analyst 2018년 8월 11일
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.

카테고리

Help CenterFile Exchange에서 Read, Write, and Modify Image에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by