Remember to change directory of the picture.
clc;close all;workspace;format longg;format compact;fontSize = 20;% Read in a standard MATLAB gray scale demo image.
folder = 'C:\Users\Monsterman\Documents';baseFileName = 'test.jpg';% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName); endgrayImage = 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);
Best Answer