How to handle 100 binary images with a single matrix?

2 views (last 30 days)
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
Guillaume
Guillaume on 9 Apr 2018
Your question is really lacking in details making it hard to answer. What bit of your process is giving you trouble?
Zara Khan
Zara Khan on 9 Apr 2018
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

Image Analyst
Image Analyst on 13 Apr 2018
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
Zara Khan
Zara Khan on 16 Apr 2018
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)

Image Analyst
Image Analyst on 9 Apr 2018
Use blockproc() in a loop over all images.
  4 Comments
Zara Khan
Zara Khan on 11 Apr 2018
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.
Zara Khan
Zara Khan on 12 Apr 2018
I have written my code here. Please anyone help me in doing the part

Sign in to comment.

Categories

Find more on Image Processing and Computer Vision in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!