How to automatically save the extracted features from images in a single excel file
Mostra commenti meno recenti
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
Più risposte (1)
Nileema Abedeera
il 17 Mar 2020
Modificato: Nileema Abedeera
il 17 Mar 2020
0 voti
6 Commenti
Image Analyst
il 17 Mar 2020
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.
Nileema Abedeera
il 18 Mar 2020
Modificato: Nileema Abedeera
il 18 Mar 2020
Image Analyst
il 18 Mar 2020
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'])
Nileema Abedeera
il 18 Mar 2020
Modificato: Nileema Abedeera
il 18 Mar 2020
Image Analyst
il 18 Mar 2020
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.
Nileema Abedeera
il 18 Mar 2020
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!