How to handle 100 binary images with a single matrix?
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Guillaume
il 9 Apr 2018
Your question is really lacking in details making it hard to answer. What bit of your process is giving you trouble?
Risposta accettata
Image Analyst
il 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
Più risposte (1)
Vedere anche
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!