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 Center および File ExchangeImages についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by