Getting the Wrong Output While Using Image Batch Processor (Edited Version)

3 visualizaciones (últimos 30 días)
Greetings, everyone.
I am a beginner in MATLAB and I just started to learn it for my bachelor thesis. So, I created a function that counts porosity from an image and feed that function into batch processor to process hundreds of image. I used the simpe formula (black pixel/total pixel) * 100 to generate porosity value and for 1 image, I expected to only get 1 porosity value. Therefore, I wanted the output table to be (the number of images)x1. But after running a trial with 12 images processed by a porosity calculator function in image batch processor, I got 12x2 table where each desired output is a vector(12,1).
Here's my code:
function porosity = porosity_function(image)
% Calculates the porosity of an image.
% Args:
% image: A MATLAB array representing the image.
% Returns:
% The porosity of the image
black_pixels = sum(image == 0);
total_pixels = size(image, 1) * size(image, 2);
porosity = (black_pixels * 100) / total_pixels;
end
And here's a few sample images (all are binary image, originally a SEM -scanning electron microscope- image of a limestone with 2000-8000x magnification). In my field of study (chemistry), pore is one of the key characteristics that represents a material/substance and there are many methods to measure it. For my thesis, I compare porosity calculated digitally (from an image and using MATLAB) and experimentally (using gas sorption analyzer instrument with nitrogen adsorption method). I have 100+ SEM images to process, so I need to put it in batch processor to save time.
Perhaps anyone could tell me what's wrong with my function? Any help would be appreciated.

Respuesta aceptada

Walter Roberson
Walter Roberson el 6 de Ag. de 2023
The problem is not the porosity function: the problem is the way you are storing the results into the output table.
You are probably doing something like
for i = 1 : number_of_files
T(i).output = Porosity_results;
T(i).fileName = filenames{i};
end
when you should instead be doing
for i = 1 : number_of_files
T(i).output = Porosity_results(i);
T(i).fileName = filenames{i};
end
or better yet make the table in one call, such as
T = table(Porosity_results, filenames(:), 'VariableNames', {'output', 'fileName'});
  2 comentarios
Shafa Salsabila
Shafa Salsabila el 28 de Ag. de 2023
Editada: Shafa Salsabila el 28 de Ag. de 2023
Hi, sorry for the late reply, your answer is very helpful. Now my code works, eventhough I run it without MATLAB's batch processor :)
function batch_process_images(image_folder)
% Batch process images in a folder and its subfolders, calculating porosity for each image.
% Args:
% image_folder: Path to the main folder containing images and subfolders.
% List all image files in the main folder and its subfolders
image_files = dir(fullfile(image_folder, '**', '*.png')); % Change the extension as needed
num_files = numel(image_files);
porosity_results = zeros(num_files, 1);
filenames = cell(num_files, 1);
for i = 1:num_files
image_path = fullfile(image_files(i).folder, image_files(i).name);
image = imread(image_path);
% Calculate porosity for the current image
black_pixels = sum(image(:) == 0); % Flatten the image to a vector
total_pixels = numel(image);
porosity_results(i) = (black_pixels * 100) / total_pixels;
filenames{i} = image_files(i).name;
end
% Create a table with porosity results and filenames
T = table(porosity_results, filenames, 'VariableNames', {'output', 'fileName'});
% Display the table
disp(T);
% If you want, you can also save the table as a CSV file
writetable(T, 'porosity_results.csv');
end
image_folder = 'C:\image\path'; % Change this to the actual path of your main folder
batch_process_images(image_folder);
Walter Roberson
Walter Roberson el 28 de Ag. de 2023
We recommend that you do not use image as the name of a variable, as doing so interfers with calling the MATLAB function named image

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Import, Export, and Conversion en Help Center y File Exchange.

Productos


Versión

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by