How do I export image features matrix data to Excel for multiple images in the same folder?

I have 100 image files which I need to process (Images are in the same folder). The idea is to take one image, perform pre-processing (noise removal, binarization etc.) followed by features extraction.I've used Wavelets (db1) to extract approximation & directional coefficients (horizontal, vertical and diagonal)-4 features per image.I need to store this features matrix in Excel format.How do I automate the process for 100 image files, so that I get an Excel sheet containing 100x4 entries?
if true
% clc;
%Code begins from here
clc;
clear all;
close all;
%Load current directory
myFolder = 'E:\BE Project\Devnagiri Character Recognition\test_images\trial';
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(myFolder, '*.jpg');
jpegFiles = dir(filePattern);
for k = 1:length(jpegFiles)
baseFileName = jpegFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
imageArray = imread(fullFileName);
%imshow(imageArray); % Display image.
%drawnow; % Force display to update immediately.
end
a=imageArray;
%Scale to 512x512 since images are of higher resolution
a=imresize(a, [512 512]);
%RGB to gray
if size(a,3)==3
b=rgb2gray(a);
end
figure;
imshow(a);
title('Original Image')
%Noise removal by median filter
f=medfilt2(b);
figure;
imshow(f);
title('Noise removal by median filter')
%Determine threshold by Otsu's method
th=graythresh(f);
g=im2bw(f,th);
figure;
imshow(g);
title('Binarized Image (Otsu Method)')
%Resize image for Wavelet Transformation
g=imresize(g, [128 128]);
figure;
imshow(g);
title('Resized Image')
%Find wavelet coefficients using 2D Wavelet Transform
[cA,cH,cV,cD]=dwt2(g,'db1');
disp('---WAVELET FEATURES---');
%Calculate max value for approximation coefficient
disp('Approximation Coefficient (Max Value) :');
ca_max=max(mean(double(cA)));
disp((ca_max));
%Calculate max value for horizontal coefficient
disp('Horizontal Coefficient (Max Value) :');
ch_max=max(mean(double(cH)));
disp((ch_max));
%Calculate max value for vertical coefficient
disp('Vertical Coefficient (Max Value) :');
cv_max=max(mean(double(cV)));
disp((cv_max));
%Calculate max value for diagonal coefficient
disp('Diagonal Coefficient (Max Value) :');
cd_max=max(mean(double(cD)));
disp(mean(cd_max));
%Obtain features in a matrix form
ft=cat(2,ca_max,ch_max,cv_max,cd_max);
%Export features matrix to Excel sheet
xlswrite('testdata.xlsx',ft);
end

1 Kommentar

hello ! Can i ask you something ? Why you take the max value for all the coefficients?
Thanks in advance!

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Something like this:
numberOfResults = 42; % Whatever it is for a single image
numberOfImages = length(filenames); % Whatever...
allResults = zeros(numberOfImages, numberOfResults);
for k = 1 : numberOfImages
% Read in image into variable called thisImage.
% Analyze the image with function AnalyzeSingleImage and get results.
thisImagesResults = AnalyzeSingleImage(thisImage);
% Store results in allResults:
allResults(k, :) = thisImagesResults;
end
xlswrite(excelFullFileName, allResults);

9 Kommentare

Yes! It works. Thank you!
Hi. Can you please explain how to save the features of 100 images in excel sheet?
That's what my answer does. Just set numberOfImages equal to 100, and write your custom function AnalyzeSingleImage() to compute the features that you want. Obviously I don't know what particular features you want - only you know that.
Getting error at the line AnalyzeSingleImage. Its showing that the function or varaible is undefined
Yes, of course you have to define it. Did you? You have to write it, not me. I have no idea what you want to do with your image. You can start your own question attaching your code and an image or two after you read this link.
I am asking, If I have particles in a single image, and there position i tracked. I saved an excel sheet ofr one image. But I have 100 different images, and want to save excel files for each image through single code? , AND ALSO TO COMBINE ALL THE CSV FILES AND SAVE IT, HOW TO DO SO?
@Devashish Tiwari how are you tracking with a single image? Usually you have multiple images, either still images in separate files on disk, or all together in a video file.
If you have multiple videos you can make a function called "ProcessSingleVideo" and in there read all the frames for one video, find the particles and save their positions to a workbook.
I don't know why you'd want a new workbook for every still image or every frame.
Yeah Exactly sir, I have multiple images to do so. But, I actually wanted to know, from those multiple images, how do I call them, and how to save the tracked particles in each image. Also, How to save the excel sheets of each image?
But, to solve this problem, I needed the work of each image and then the for loop, so I started doing so.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (3)

3 Kommentare

I still do not get 100 entries for the modified code.It only processes the latest image.
Save the entries into a single common array. xlswrite() the array only after all 100 are done.
i am also have a same problem with exporting extracted features from matlab to excel.. can explain me to .how to save the entries in a single array?

Melden Sie sich an, um zu kommentieren.

i trained two-class-classification (svmtrain) with 15 features for 18 images and i have different number of objects in a single image. I want to save single .mat file of features of these images?my problem is when i run this code i have just the variables of last image!!This is my code :
NbIm = size(names1,1);
n1 = 1;
n2 = NbIm;
for n=n1:n2
%1- Read the original image
%2- Processing : Segmentation
%3- characterization :
[B3,L3,N3] = bwboundaries(ICellules); (see picture)
CC = bwconncomp(L3);
BW=bwlabel(L3); stats1=regionprops(CC,'Area','Centroid','Eccentricity','Perimeter','ConvexArea','ConvexHull','ConvexImage','MajorAxisLength','MinorAxisLength','Orientation','Solidity','BoundingBox');
for k=1:length(B3),
V=[];glcm=[];
V=Im_originale(BW==k);
glcm = graycomatrix(V,'Offset',[2 0],'Symmetric', true);
stats= graycoprops(glcm);
Contrast_Cellule =stats.Contrast;
Correlation_Cellule =stats.Correlation;
Energy_Cellule =stats.Energy;
Homogeneity_Cellule =stats.Homogeneity;
Area_cellule = stats1(k).Area;
Perimeter_cellule = stats1(k).Perimeter;
Circularity_cellule= (4*pi*Area_cellule)/Perimeter_cellule^2;
Centroid_cellule = stats1(k).Centroid;
Compactness_cellule=Perimeter_cellule^2/(4*pi*Area_cellule);
MajorAxis_cellule=stats1(k). MajorAxisLength;
MinorAxis_cellule=stats1(k). MinorAxisLength;
Orientation_cellule =stats1(k).Orientation;
Eccentricity_cellule=stats1(k).Eccentricity;
Solidity_cellule=stats1(k).Solidity;
boundary3 = B3{k};
[cc] = chaincode(boundary3);
ai=cc.code;
ai = ai.';
output = calc_harmonic_coefficients(ai,30);
Ampl=0.5*sqrt((output(1)^2)+(output(2)^2)+(output(3)^2)+(output(4)^2));
Feat(k,:)=[Area_cellule,Perimeter_cellule,Circularity_cellule,Compactness_cellule, Solidity_cellule,Eccentricity_cellule,MajorAxis_cellule,MinorAxis_cellule, Centroid_cellule,Ampl,Contrast_Cellule,Correlation_Cellule,Energy_Cellule,Homogeneity_Cellule];
end
end

3 Kommentare

THis
for k=1:length(B3),
should be
for k = 1 : length(stats1)
Then you just need to index things, like Area_cellule(k) instead of just Area_cellule which will get overwritten on every loop iteration. By the way, you can get all the props in a single array before the loop, like
Area_cellule= [stats1.Area];
No need to assign it in a loop one at a time.
Hi, thanks for your response...can you please explain more because i don't understand
Actually I thought B3 was an image, but it's not, so your for loop line should also work as it.
For the line
Area_cellule = stats1(k).Area;
you're just taking the k'th area variable and assigning it to Area_cellule. So for k=1 (first iteration) Area_cellule = the first area. For k=2, Area_cellule = the second area. For the last iteration Area_cellule = the last area. And when the loop finally exits, Area_cellule is just a single number equal to the last area, NOT an array of all areas like you said you wanted. To get an array, you either have to do
Area_cellule*k( = stats1(k).Area;
inside the loop, or better, don't assign Area_cellule at all inside the loop and have this outside the loop:
Area_cellule = [stats1.Area];
Same for all the other properties of stats1 you're trying to make into individual arrays, like Perimeter_cellule etc.

Melden Sie sich an, um zu kommentieren.

numberOfResults = 42; % Whatever it is for a single image
numberOfImages = length(filenames); % Whatever...
allResults = zeros(numberOfImages, numberOfResults);
for k = 1 : numberOfImages
% Read in image into variable called thisImage.
% Analyze the image with function AnalyzeSingleImage and get results.
thisImagesResults = AnalyzeSingleImage(thisImage);
% Store results in allResults:
allResults(k, :) = thisImagesResults;
end
xlswrite(excelFullFileName, allResults);
can u show with the code please? i dont understand

Kategorien

Community Treasure Hunt

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

Start Hunting!

Translated by