How to automatically save the extracted features from images in a single excel file

I have extratced some features from medical images which I have segmented the preferred ROI and divided into four quadrants. Thus each image is having four quadrants and I have extraced all of six features from each of these quadrant image. Can you help me how to automize this code to save the extracted features i a single excel .xlsx file? Thanks in advance.
Here is my code;
%Load the current directory
MyFolder = 'C:\Users\User\Documents\MATLAB\CXR Images\Local DB\NHRD_IMAGES';
% Get the list of all images in the folder with the desired file name pattern
FilePattern = fullfile (MyFolder, 'NHIMG-*.jpg');
TheFiles = dir (FilePattern);
for k = 1 : length(TheFiles)
BasefileName = TheFiles (k).name;
FullFileName = fullfile (MyFolder, BasefileName);
fprintf (1, 'Now reading %s\n', FullFileName);
% Read in the CXR images
I= imread (FullFileName);
% Segmentation of the lung field from the CXR image
[S] = LungMask(I);
% Dividing Segmented lung image into quadrants
[RUQ, LUQ, RLQ, LLQ] = crop(S);
% Extracting Features from each quadrant of the segmented images
[Stats, MaxProb, Entropy] = Prop(RUQ);
[Stats, MaxProb, Entropy] = Prop(LUQ);
[Stats, MaxProb, Entropy] = Prop(RLQ);
[Stats, MaxProb, Entropy] = Prop(LLQ);
end

 Risposta accettata

Put them into 4 different variables, then call xlswrite or writematrix():
[Stats1, MaxProb1, Entropy1] = Prop(RUQ);
[Stats2, MaxProb2, Entropy2] = Prop(LUQ);
[Stats3, MaxProb3, Entropy3] = Prop(RLQ);
[Stats4, MaxProb4, Entropy4] = Prop(LLQ);
xlswrite(fileName, Stats1, 'Sheet1', 'A1')
xlswrite(fileName, Stats2, 'Sheet2', 'A1')
xlswrite(fileName, Stats3, 'Sheet3', 'A1')
xlswrite(fileName, Stats4, 'Sheet4', 'A1')

4 Commenti

Thank you Image Analyst.
I just tried this, however I am still getting an error.
saying;
Error using xlswrite (line 170)
File name must be a string scalar or character vector.
I am bit confused now.
Sorry - the file name comes first in the argument list.
Thanks again for your kind reply.
Now with this change when I run the code I got another error saying;
Error using xlswrite (line 170)
Input data must be a numeric, cell, or logical array.
Kindly help me in solving this.
The features to be saved are given in these [Stats1, MaxProb1, Entropy1] variables and inside Stats1 there are four feaures (contrast, correlation, homogeneity and energy) which will be extracted using GLCM.
Thank you in advance.
You have to make up a matrix. So what is Stats1? A structure? A 4x1 column vector? A 1x4 row vector? What does this show
Stats1
MaxProb1
Entropy1
See if there is some way to rearrange them into either a dobule array or a cell array.

Accedi per commentare.

Più risposte (1)

Hi Image Analyst, thank you.
Stats is a Structure. This is how I get my results;
Stats1 =
struct with fields:
Contrast: 11.0484
Correlation: 0.9878
Energy: 0.6379
Homogeneity: 0.9191
MaxProb1 =0.7986
Entropy1 =1.8655
After many times coming up with certain errors I tried writecell function to save the feature values.
First I have converted the structure element to a table and then to an array.
So I have changed my code like this;
T1 = struct2table(Stats1);
T2 = struct2table(Stats2);
T3 = struct2table(Stats3);
T4 = struct2table(Stats4);
and then;
writecell ({table2array(T1),MaxProb1, Entropy1}, 'FILE1.xlsx');
writecell({table2array(T2),MaxProb2,Entropy2}, 'FILE2.xlsx');
writecell({table2array(T3),MaxProb3,Entropy3}, 'FILE3.xlsx');
writecell({table2array(T4),MaxProb4,Entropy4}, 'FILE4.xlsx');
end
This gave me all six features of the last image I have processed saved onto the sepcified .xlsx data sheet.
I need to save all feature data belongs to each and every image I am processing to be saved in this same .xlsx data sheet.
Your valuable inputs are most welcome.
Thank you so much.

6 Commenti

You need to decide how to format your workbook. Often/usually what I do is to have one line per row in the workbook with the first column being the filename, and columns to the right of that being the 10 or 20 measurements that you've made.
So for one workbook for all the images and all 4 sets of data for each image in a single row, you could do
[folder, baseFileNameNoExt, ext] = fileparts(fullFileName);
resultsFolder = fullfile(folder, '/Results');
if ~isfolder(resultsFolder)
mkdir(resultsFolder); % Make results subfolder if it does not exist yet.
end
xlFullFileName = fullfile(resultsFolder, [resultsFolder, '.xlsx'])
% Make T's row vectors
T1 = reshape(T1, 1, []);
T2 = reshape(T2, 1, []);
T3 = reshape(T3, 1, []);
T4 = reshape(T4, 1, []);
% First write out line for set #1 with T1, MaxProb1, and Entropy 1
row = 2; % Change this depending on what image number you're processing.
% You might want to write in column headers into row 1 and start your data with row 2.
ca1 = {T1, MaxProb1, Entropy1};
ca2 = {T2, MaxProb2, Entropy2};
ca3 = {T3, MaxProb3, Entropy3};
ca4 = {T4, MaxProb4, Entropy4};
% Write filename first in column A
cellReference = sprintf('A%d', row);
writecell (fullFileName, xlFullFileName, 'Range', cellReference);
% Now write the rest of the data for #1:
cellReference = sprintf('B%d', row);
writecell (ca1, xlFullFileName, 'Range', cellReference);
% Now write the rest of the data for #2:
cellReference = sprintf('H%d', row);
writecell (ca2, xlFullFileName, 'Range', cellReference);
% Now write the rest of the data for #3:
cellReference = sprintf('N%d', row);
writecell (ca3, xlFullFileName, 'Range', cellReference);
% Now write the rest of the data for #4:
cellReference = sprintf('T%d', row);
writecell (ca4, xlFullFileName, 'Range', cellReference);
If it's a really complicated layout you want you can either write one workbook for each image, or use a different sheet name for each image, though I think Excel has a limit on the number of sheets in a workbook. It used to be 32 and may be that still, or might be more than that now.
Thank you!
I am still getting an error.
Error using tabular/reshape (line 216)
Undefined function 'reshape' for input arguments of type 'table'.
I tried your suggestion like below;
MyFolder = 'C:\Users\User\Documents\MATLAB\CXR Images\Local DB\NHRD_IMAGES';
FilePattern = fullfile(MyFolder, 'MC-*.png');
TheFiles = dir(FilePattern);
for k = 1 : length(TheFiles)
BasefileName = TheFiles (k).name;
FullFileName = fullfile(MyFolder, BasefileName);
fprintf(1, 'Now reading %s\n', FullFileName);
I = imread(FullFileName);
[S] = LungMask(I);
figure, [RUQ, LUQ, RLQ, LLQ] = crop(S);
[Stats1, MaxProb1, Entropy1] = Prop(RUQ)
[Stats2, MaxProb2, Entropy2] = Prop(LUQ)
[Stats3, MaxProb3, Entropy3] = Prop(RLQ)
[Stats4, MaxProb4, Entropy4] = Prop(LLQ)
T1 = struct2table (Stats1)
T2 = struct2table (Stats2)
T3 = struct2table (Stats3)
T4 = struct2table (Stats4)
[folder, baseFileNameNoExt, ext] = fileparts(FullFileName);
resultsFolder = fullfile(folder, '/Results');
if ~isfolder(resultsFolder)
mkdir(resultsFolder);
end
xlFullFileName = fullfile(resultsFolder, [resultsFolder, 'A.xlsx'])
T1 = reshape(T1, 1, []);
T2 = reshape(T2, 1, []);
T3 = reshape(T3, 1, []);
T4 = reshape(T4, 1, []);
row = 2;
ca1 = {T1, MaxProb1, Entropy1};
ca2 = {T2, MaxProb2, Entropy2};
ca3 = {T3, MaxProb3, Entropy3};
ca4 = {T4, MaxProb4, Entropy4};
cellReference = sprintf('A%d', row);
writecell (FullFileName, xlFullFileName, 'Range', cellReference);
cellReference = sprintf('B%d', row);
writecell(ca1, xlFullFileName, 'Range', cellReference);
cellReference = sprintf('H%d', row);
writecell (ca2, xlFullFileName, 'Range', cellReference);
cellReference = sprintf('N%d', row);
writecell (ca3, xlFullFileName, 'Range', cellReference);
cellReference = sprintf('T%d', row);
writecell (ca4, xlFullFileName, 'Range', cellReference);
end
Also I got another errors when I tried it for the second time
Error using writecell (line 127)
Unable to save the workbook to file 'C:\Users\User\Documents\MATLAB\CXR Images\Local
DB\NHRD_IMAGES\Results\C:\Users\User\Documents\MATLAB\CXR Images\Local
DB\NHRD_IMAGES\ResultsA.xlsx'. Check that write permissions are available, there is
sufficient disk space, and the file can be written to or created.
I have no idea what mistake is there here. May be I haven't understood what you have given above.
I took several attempts even using writematrix when this went wrong and that also didn't work.
Would you kindly help me get through this part. Thank you.
Make them row vectors instead of tables, and get rid of the reshape call:
T1 = [Struct1.Contrast, Struct1.Correlation, Struct1.Energy, Struct1.Homogeneity];
etc.
As far as now being able to write to the workbook, the most common problem is that you already have it opened up in Excel. But it looks like your filename is messed up with the folder being in there twice, like it's [folder,folder,'ResultsA.xlsx']. That was my fault - type - it should be
xlFullFileName = fullfile(resultsFolder, [baseFileNameNoExt, '.xlsx'])
Ohh sorry, I could not notice this folder name being duplicated in file name row.
Now I have done the corrections with correcting the filename and run the code.
But for this;
T1 = [Struct1.Contrast, Struct1.Correlation, Struct1.Energy, Struct1.Homogeneity];
I had to use; and run the code.
T1 = [Stats1.Contrast, Stats1.Correlation, Stats1.Energy,Stats1.Homogeneity];
Also this line;
writecell (FullFileName, xlFullFileName, 'Range', cellReference);
I had to change it to;
writematrix (FullFileName, xlFullFileName, 'Range', cellReference);
As I got an errors saying "cannot use writecell for data type char".
And then It worked.
I got 6 .xlsx sheets with all six features for each quadrant images resulting 24 features for each single image.
Thank you so much for your precious time and valuable inputs.
I was wondering whether I can get all these feature data into a single .xlsx data sheet by any means without further complicating this existing code. I have studied some of your previous answers and tried but failed.
Thanks again.
Well yes, but you'll have to keep track of what row you're writing into and then increment as needed to keep writing lower and lower in the worksheet.
% Increment row number so the next time we write into the row below this one.
row = row + 1;
cellReference = sprintf('A%d', row);
etc.
Thank you!
Does it means that I have to repeat the lines after each row for each next image like below;
row = 2;
cellReference = sprintf('A%d', row);
writematrix (FullFileName, xlFullFileName, 'Range', cellReference);
cellReference = sprintf('B%d', row);
writecell(ca1, xlFullFileName, 'Range', cellReference);
cellReference = sprintf('H%d', row);
writecell (ca2, xlFullFileName, 'Range', cellReference);
cellReference = sprintf('N%d', row);
writecell (ca3, xlFullFileName, 'Range', cellReference);
cellReference = sprintf('T%d', row);
writecell (ca4, xlFullFileName, 'Range', cellReference);
row = row + 1;
cellReference = sprintf('A%d', row);
writematrix (FullFileName, xlFullFileName, 'Range', cellReference);
cellReference = sprintf('B%d', row);
writecell(ca1, xlFullFileName, 'Range', cellReference);
cellReference = sprintf('H%d', row);
writecell (ca2, xlFullFileName, 'Range', cellReference);
cellReference = sprintf('N%d', row);
writecell (ca3, xlFullFileName, 'Range', cellReference);
cellReference = sprintf('T%d', row);
writecell (ca4, xlFullFileName, 'Range', cellReference);
What if my image sample is about 200/300 images? Is there any easy method to do this?

Accedi per commentare.

Categorie

Scopri di più su Scripts in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by