MATLAB: Divide an image in half

black/white image sliceImage Processing Toolbox

Hey guys, I want to divide a black and white image 320px by 240px vertically and then count up the black pixels where the output is the % of black pixels in each of the 2 image segments. So far I have this
[filename pathname] = uigetfile('*.jpg', 'Choose Image');
imageLocation = strcat(pathname, filename);
blackWhiteImage = im2bw(imread(imageLocation), 0.5);
splitImage = imdivide(blackWhiteImage,2);
totalNumberOfPixels = 120*160;
numberOfBlackPixelsLeft = sum(sum(splitImage == 0));
set(handles.console, 'string', percentBlackPixelsLeft);
I am new to matlab and I need some help with that, any help would be greatley appriciated :)))

Best Answer

See this demo. Just copy, paste, and run:
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.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 18;
% Change the current folder to the folder of this m-file.
if(~isdeployed)
cd(fileparts(which(mfilename)));
end
% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
% Read in a standard MATLAB gray scale demo image.

folder = fullfile(matlabroot, '\toolbox\images\imdemos');
button = menu('Use which demo image?', 'CameraMan', 'Moon', 'Eight', 'Coins', 'Pout');
if button == 1
baseFileName = 'cameraman.tif';
elseif button == 2
baseFileName = 'moon.tif';
elseif button == 3
baseFileName = 'eight.tif';
elseif button == 4
baseFileName = 'coins.png';
else
baseFileName = 'pout.tif';
end
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
% 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')
% Let's compute and display the histogram.
[pixelCount grayLevels] = imhist(grayImage);
subplot(2, 2, 2);
bar(pixelCount);
grid on;
title('Histogram of original image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
% Binarize the image
binaryImage = im2bw(grayImage, 0.5);
% Display the original gray scale image.
subplot(2, 2, 2);
imshow(binaryImage, []);
numBlackPixelsTotal = sum(~binaryImage(:));
percentTotal = 100 * numBlackPixelsTotal / (rows * columns);
caption = sprintf('Complete Binary Image has %d Black pixels = %.3f %%', ...
numBlackPixelsTotal, percentTotal);
title(caption, 'FontSize', fontSize);
% Get the top half.
topHalf = binaryImage(1:floor(rows/2),:);
subplot(2, 2, 3);
imshow(topHalf, []);
% Count the Black pixels in the top half.
numBlackPixelsTop = sum(~topHalf(:));
percentTop = 100 * numBlackPixelsTop / numel(topHalf);
caption = sprintf('Top Half of Binary Image has %d Black pixels = %.3f %%', ...
numBlackPixelsTop, percentTop);
title(caption, 'FontSize', fontSize);
% Get the bottom half.
bottomHalf = binaryImage(floor(rows/2)+1:end,:);
subplot(2, 2, 4);
imshow(bottomHalf, []);
% Count the Black pixels in the bottom half.
numBlackPixelsBottom = sum(~bottomHalf(:));
percentBottom = 100 * numBlackPixelsBottom / numel(bottomHalf);
caption = sprintf('Bottom Half of Binary Image has %d Black pixels = %.3f %%', ...
numBlackPixelsBottom, percentBottom);
title(caption, 'FontSize', fontSize);