MATLAB: How can i conver this code to do the same thing fo white background

background detectioncropimage processingImage Processing Toolboxmatlab script

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);
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);

Best Answer

Try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 15;
% Read in a standard MATLAB color demo image.
folder = 'D:\Temporary stuff';
baseFileName = 'test.jpg';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% 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.', 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 image.
subplot(2, 2, 1);
imshow(grayImage);
axis on;
title('Original Gray Scale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Find the mask
mask = ~(grayImage == 255);
% Get rid of small junk.
mask = bwareaopen(mask, 2500);
% Display the image.


subplot(2, 2, 2);
imshow(mask);
axis on;
title('Mask Image', 'FontSize', fontSize);
% Call regionprops to get the Bounding Box.
measurements = regionprops(mask, 'BoundingBox');
% Crop out the masked part
thisBoundingBox = measurements(1).BoundingBox
croppedImage = imcrop(grayImage, thisBoundingBox);
% Display the image.
subplot(2, 2, 3);
imshow(croppedImage);
axis on;
title('Cropped Image', 'FontSize', fontSize);
% Make another version with the background black
smallMask = croppedImage >= 251;
croppedImage2 = croppedImage; % Initialize.
croppedImage2(smallMask) = 0;
% Display the image.
subplot(2, 2, 4);
imshow(croppedImage2);
axis on;
title('Cropped Image', 'FontSize', fontSize);
msgbox('Results would be better if you use a PNG image instead of a JPG image.');