Trying to create a code to process images and create an excel sheet
Show older comments
I have been trying to create a code to process a folder's worth of images and create a workbook in excel with certain datapoints. I am currently getting a blank workbook. I have tried to fix the mistake for a while and I still don't know what it is. Please give me any suggestions for improvement. Here's my code:
clear
clc
close all
% Call Images from each folder ============================================
% path = cd;
path ='C:\Users\myname\Downloads\nozz4-2000_S0001';
info_folder = dir(path); [size_folder, s] = size(info_folder);
Data2 = [];
scale = 0.041524197; %mm/pix
D_top = 10-512*scale;
% scale = 0.063619997;
% D_top = 155;
framerate = 1/2000;
for i_folder = 3:1:size_folder
if info_folder(i_folder,1).isdir == 1
folder_name = info_folder(i_folder,1).name;
find_jpg = [path '\' folder_name '\*.tif'];
condition = dir(find_jpg);
[total_frame,s1] = size(condition);
%==================================================================
Data1 = [];
Data_number = cell(total_frame, 1);
for i = 1:1:total_frame
% =============================================================
% Image processing ============================================
I_file_name = condition(i,1).name; % I_file_name is the name of image.
I_name_path = [path '\' folder_name '\' I_file_name];
I = imread(I_name_path,'tif'); % I is the image for image processing.
I_double = double(I); % I_double is the numbers to calculate.
I_gauss = imgaussfilt(I,0.8);
I_double2 = double(I_gauss);
%==============================================================
% Image processing for each image =============================
I_adjust1 = I(:,:,1);
I_gauss = imgaussfilt(I_adjust1,0.8);
I_double2 = double(I_adjust1);
[xx,yy] = size(I_adjust1);
Num_cell = 0;
I_adjust = I_adjust1(Num_cell+1:xx-Num_cell,:);
I_norm = I_double2/(max(max(I_double2)));
I_norm = mat2gray(I_adjust/(max(max(I_adjust))));
dI_x = I_double2(:,2*Num_cell+1:yy) - I_double2(:,1:yy-2*Num_cell);
dI_y = I_double2(1:xx-2*Num_cell,:) - I_double2(1+2*Num_cell:xx,:);
dI_xy = sqrt(dI_x(1+Num_cell:xx-Num_cell,:).^2+dI_y(:,1+Num_cell:yy-Num_cell).^2);
I_grad = dI_xy/(max(max(dI_xy)));
I_Binary = imbinarize(imcomplement(I_norm),0.3);
I_Binary4 = imbinarize(I_grad,'adaptive','ForegroundPolarity','bright','Sensitivity',0.2);
I_Binary2=bwareafilt(I_Binary,1);
I_Binary3 = imfill(I_Binary2,'holes');
I_Binary_8 = uint8(I_Binary3);
I_Intensity_Region = I.*I_Binary_8;
[B,L] = bwboundaries(I_Binary3,'holes');
stats = regionprops(I_Binary3, 'Orientation', 'MajorAxisLength', ...
'MinorAxisLength', 'Eccentricity', 'Centroid', 'EquivDiameter', 'Circularity', 'BoundingBox');
Eccentricity1 = stats.Eccentricity;
D_position = stats.Centroid(2)*scale+D_top;
Majaxis = stats.MajorAxisLength*scale;
Minaxis = stats.MinorAxisLength*scale;
Orientation1 = stats.Orientation;
Centroid_x = stats.Centroid(1);
Centroid_y = stats.Centroid(2);
Equiv_Diam = stats.EquivDiameter*scale;
Circularity1 = stats.Circularity;
Drop_Area = Majaxis*Minaxis*pi;
Dsquared = Minaxis^2;
Width1 = stats.BoundingBox(3)*scale;
phi = linspace(0,2*pi,50);
cosphi = cos(phi);
sinphi = sin(phi);
xbar = stats.Centroid(1);
ybar = stats.Centroid(2);
a = stats.MajorAxisLength/2;
b = stats.MinorAxisLength/2;
theta = pi*stats.Orientation/180;
R = [ cos(theta) sin(theta)
-sin(theta) cos(theta)];
xy = [a*cosphi; b*sinphi];
xy = R*xy;
x = xy(1,:) + xbar;
y = xy(2,:) + ybar;
temp_data = [Eccentricity1, D_position,Majaxis, Minaxis, Orientation1,Centroid_x,Centroid_y, Equiv_Diam, Drop_Area, Dsquared, Circularity1, Width1];
Data_number{i} = temp_data;
imshow(I_Binary3)
%==============================================================
end
% Data process for each folder ====================================
Data1 = vertcat(Data_number{:});
%Append Data1 to Data2
Data2 = [Data2; Data1];
%==================================================================
% Export as Excel for each folder =================================
save_name = [folder_name '.xlsx'];
A1 = {'Eccentricity', 'D_position (mm)' ,...
'Majaxis (mm)', 'Minaxis (mm)', 'Orientation (Degrees)', 'Centroid_x (Pixels)', 'Centroid_y (Pixels)', ...
'Equiv_Diam (mm)', 'Droplet Area (mm^2)','Dsquared','circularity','width(mm)'};
writematrix(A1, save_name, 'Sheet1', 'A1');
writematrix(Data1, save_name, 'Sheet1', 'A2');
%==================================================================
subplot(2,2,1),imshow(I_adjust1); hold on; plot(x,y,'r','LineWidth',2);
subplot(2,2,2),imshow(I_Binary);hold on; plot(x,y,'r','LineWidth',2);
subplot(2,2,3),imshow(I_Binary2);hold on; plot(x,y,'r','LineWidth',2);
subplot(2,2,4),imshow(I_Binary3);hold on; plot(x,y,'r','LineWidth',2);
end
% Export as Excel for each folder =====================================
writematrix(Data2, 'ImageDropData4.xlsx');
%==================================================================
end
%
fprintf('ALL done.\n');
%==========================================================================
4 Comments
Ashutosh
on 11 Jul 2023
I will assume that the issue is only with writing the data to an Excel file, and the rest of the code is doing its job. You can try the following, to fix the writing part:
- The name of the file to be written should be a single string, not an array. Try strcat.
- You have defined A1 as a 1x12 cell, by using the braces. It is not a matrix and you cannot use writematrix. Try writecell
- When you provide a Sheet name and starting point ('A1'), you have to specify that using "Sheet" and "Range" key arguments.
The following code should address these issues:
file_name = "YourFileNameHere";
save_name = strcat(file_name,'.xlsx');
A1 = {'Eccentricity', 'D_position (mm)' ,...
'Majaxis (mm)', 'Minaxis (mm)','Orientation (Degrees)','Centroid_x (Pixels)','Centroid_y (Pixels)', ...
'Equiv_Diam (mm)', 'Droplet Area (mm^2)','Dsquared','circularity','width(mm)'};
writecell(A1, save_name,"Sheet", 'Sheet1',"Range", 'A1');
Matthew Loewer
on 11 Jul 2023
Mathieu NOE
on 11 Jul 2023
have you run your code step by step and checked that the image loading is working ?
are you processing / looking for tif or jpg files ?
this is a bit confusing : (jpg or tif ?)
find_jpg = [path '\' folder_name '\*.tif'];
Mathieu NOE
on 11 Jul 2023
also , I would do the code a bit simpler , maybe this way ? (does not contain all the details, just the core structure)
%% define path
yourpath = pwd; % or your specific path
list=dir(yourpath); %get info of files/folders in current directory
isfile=~[list.isdir]; %determine index of files vs folders
dirnames={list([list.isdir]).name}; % directories names (including . and ..)
dirnames=dirnames(~(strcmp('.',dirnames)|strcmp('..',dirnames))); % remove . and .. directories names from list
%% Loop on each folder
for ci = 1:length(dirnames) %
fileDir = char(dirnames(ci)); % current directory name
S = dir(fullfile(fileDir,'*.tif')); % get list of data files in directory according to name structure '*.tif'
S = natsortfiles(S); % sort file names into natural order (what matlab does not) , see FEX :
%(https://fr.mathworks.com/matlabcentral/fileexchange/47434-natural-order-filename-sort)
%% Loop inside folder
for k = 1:length(S) % read data
I = imread(fullfile(fileDir, S(k).name)); % or use a structure (S(k).data ) to store the full data structure
% your own code here for data processing. this is just for my demo
% for now :
title_str = [fileDir ' / ' S(k).name];
figure,image(I),title(title_str);
end
end
Accepted Answer
More Answers (0)
Categories
Find more on Spreadsheets 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!