How to crop an image automatically?

조회 수: 12 (최근 30일)
Sabarinathan Vadivelu
Sabarinathan Vadivelu 2012년 11월 30일
답변: Nehal 2014년 3월 3일
The following is my input Image. Here is there any possibility crop automatically that chromosome part? Because other parts are fully zero.

채택된 답변

Image Analyst
Image Analyst 2012년 11월 30일
Try this:
clc;
close all;
workspace;
format longg;
format compact;
fontSize = 20;
% Read in a standard MATLAB gray scale demo image.
folder = 'C:\Users\Saba\Documents';
baseFileName = '24495081.jpg';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- 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 in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage)
% Display the original gray scale image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% Get all rows and columns where the image is nonzero
[nonZeroRows nonZeroColumns] = find(grayImage);
% Get the cropping parameters
topRow = min(nonZeroRows(:));
bottomRow = max(nonZeroRows(:));
leftColumn = min(nonZeroColumns(:));
rightColumn = max(nonZeroColumns(:));
% Extract a cropped image from the original.
croppedImage = grayImage(topRow:bottomRow, leftColumn:rightColumn);
% Display the original gray scale image.
subplot(2, 2, 2);
imshow(croppedImage, []);
title('Cropped Grayscale Image', 'FontSize', fontSize);
  댓글 수: 2
Joakim
Joakim 2013년 12월 18일
This works great, if I want to crop away a white Border can i just change the nonZeroRows/nonZeroColumns to some other value(if yes which?)
Image Analyst
Image Analyst 2013년 12월 18일
Yes. It depends on what rows and columns the border occurs in. If you have a binary (true/false) image, you can use imclearborder() to do it automatically.

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

추가 답변 (1개)

Nehal
Nehal 2014년 3월 3일
please send me this image which u have used. my email id is nehal7789@yahoo.com

카테고리

Help CenterFile Exchange에서 Images에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by