How to handle 100 binary images with a single matrix?

My folder consists of 100 binary images. I want to divide each of these images into some parts and want to calculate area of each block and want to store it in a matrix. Later that matrix I want to map to excel sheet I mean to say that I want to write these values to excel sheet. How to implement this repeated process so that 100 images each blocks area value will be stored in a matrix.

2 Comments

Your question is really lacking in details making it hard to answer. What bit of your process is giving you trouble?
I am not able to store each blocks areas to a matrix then it in excel sheet. As I have 100 images. And I am diving each of theSe images into 4 or 8 parts. So there will be either 400 or 800 blocks.I want to store these areas in a matrix then it in a excel file.

Sign in to comment.

 Accepted Answer

Since your blocks are just simply quadrants, as I can see from your code attempt, do it like this:
folder = 'D:\data_set1';
% folder = pwd;
filepattern = fullfile(folder, '*.png');
srcFiles = dir(filepattern);
numImages = length(srcFiles);
allAreas = zeros(numImages, 4);
areaFractions = zeros(numImages, 4);
for k = 1 : numImages
fullFileName = fullfile(folder, srcFiles(k).name);
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage);
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
grayImage = rgb2gray(grayImage);
end
% Display original image.
subplot(1, 2, 1);
imshow(grayImage);
title(srcFiles(k).name, 'FontSize', 15, 'Interpreter', 'none');
% Make sure the image is uint8 or uint16
if ~isinteger(grayImage);
continue;
end
% Threshold the image.
binaryImage = imbinarize(grayImage);
% Make sure there is only one blob in the image.
binaryImage = bwareafilt(binaryImage, 1);
% Display binary image.
subplot(1, 2, 2);
imshow(binaryImage);
title('Binary Image', 'FontSize', 15, 'Interpreter', 'none');
drawnow;
% Get the centroid and the bonding box.
% Not sure why since we don't use them so I'll comment it out.
% props = regionprops(binaryImage, 'Centroid', 'BoundingBox');
% Determine half way point. Need to round in case image has odd number of rows or columns.
rowMid = round(rows / 2);
colMid = round(columns / 2);
% Divide the binary image up into quadrants.
q1 = binaryImage(1:rowMid, 1:colMid);
q2 = binaryImage(1:rowMid, (colMid+1):end);
q3 = binaryImage((rowMid+1):end, colMid);
q4 = binaryImage((rowMid+1):end, (colMid+1):end);
% Get the foreground area in each quadrant.
area1 = sum(q1(:));
area2 = sum(q2(:));
area3 = sum(q3(:));
area4 = sum(q4(:));
allAreas(k, :) = [area1, area2, area3, area4];
areaFractions(k, :) = [area1, area2, area3, area4] ./ [numel(q1), numel(q2), numel(q3), numel(q4)];
end
% Report to the command window.
allAreas
areaFractions

1 Comment

Image Analyst: Thank you so much. It has helped me a lot. This is the exact solution of my problem.

Sign in to comment.

More Answers (1)

Use blockproc() in a loop over all images.

4 Comments

But not able to store all blocks areas in a single matrix
For 4 areas, the area of each quadrant is numel(binaryImage)/4.
If you divide it into 8, the area is numel(binaryImage)/8 pixels.
For one image, you could of course store all the quadrant areas into one matrix.
areas4 = numel(binaryImage)/4 * ones(1, 4);
areas8 = numel(binaryImage)/8 * ones(1, 8);
If you want the foreground area of the binary image rather than the area of the image, you'll have to sum each quadrant
q1 = binaryImage(1:rows/2, 1:columns/2);
q2 = binaryImage(1:rows/2, columns/2:end);
q3 = binaryImage(rows/2:end, 1:columns/2);
q4 = binaryImage(rows/2:end, columns/2:end);
area1 = sum(q1(:));
area2 = sum(q2(:));
area3 = sum(q3(:));
area4 = sum(q4(:));
i
srcFiles = dir('D:\data_set1\*.png');
for i = 1 : length(srcFiles)
filename = strcat('D:\data_set1\',srcFiles(i).name);
a = imread(filename);
bwimg =bwareafilt(~a, 1);
props = regionprops(bwimg,'Centroid','BoundingBox');
assert(numel(props) == 1, 'more than one object found in image ');
[rows columns]=size(bwimg);
areas4 = numel(bwimg)/4 * ones(1, 4);
q1 = bwimg(1:rows/2, 1:columns/2);
q2 = bwimg(1:rows/2, columns/2:end);
q3 = bwimg(rows/2:end, 1:columns/2);
q4 = bwimg(rows/2:end, columns/2:end);
area1 = sum(q1(:));
area2 = sum(q2(:));
area3 = sum(q3(:));
area4 = sum(q4(:));
end
Image Analyst: When I am doing this for a set of 100 images from a folder then all these areas are not storing in a single matrix. How to do that ? As I am new many things are not still clear to me. After storing these values in a single matrix I want to write them in a excel file.
I have written my code here. Please anyone help me in doing the part

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!