Using xlswrite() to store the output from regionprops()

1 view (last 30 days)
Hi everyone,
I am using the following code to output the results from a regionprops operation:
clc
clear all
files = dir('*.jpg');
for k = 1:numel(files)
rgb = imread(files(k).name);
rgb = imcrop(rgb,[1 21 1279 988]);
cform = makecform('srgb2lab');
lab_Image = applycform(im2double(rgb),cform);
rgb = lab_Image(:, :, 3);
rgb = adapthisteq(rgb, 'NumTiles', [9 9], ...
'Cliplimit', 0.001);
rgb = edge(rgb,'canny');
se90 = strel('line',2,90);
se75 = strel('line',2,75);
se60 = strel('line',2,60);
se45 = strel('line',2,45);
se30 = strel('line',2,30);
se0 = strel('line',2,0);
rgb = imdilate(rgb,[se90 se75 se60 se45 se30 se0]);
% Use regionprops to generate area and solodity information.
s = regionprops(rgb,'area', 'Solidity');
areas = cat(1,s.Area);
solidity = cat(1,s.Solidity);
xlswrite('trialdata.xlsx', areas,'Sheet1');
xlswrite('trialdata.xlsx', solidity,'Sheet2');
end
The data is currently stored in the excel file on the first column (A:A). Unfortunately, it doesn't capture as to which image the measurements belong to.
I was wondering if someone could please help me out with how to output the file name for whcih each entry belongs to. I think what I am looking for is a format like:
In Sheet1,
  • Column A = name of the image
  • Column B = regionprops measurement (area)
  • Column C = regionprops measurement (solidity)Thanks in advance.
Cheers

Accepted Answer

Image Analyst
Image Analyst on 19 Jan 2012
You need to make up a cell array. First you need to find out how many blobs there are in your binary image.
numberOfBlobs = length(s); % or something like that.
Then put them into the cell array
for blob = 1 : numberOfBlobs
% Put these 3 things into this single row.
ca(row, 1) = {files(k).name};
ca(row, 2) = {s(blob).Area};
ca(row, 3) = {s(blob).Solidity}
% Go down to the next row.
row = row + 1; % Go down, preparing for the next blob in this image.
end
This loop should be nested inside your loop over image files. "row" is initialized to 1 before your loop over the images. So image1 might take up 10 rows for its 10 blobs, then image2 might take up the next 35 rows for its 35 blobs, and so on.
After your loop over all images you should have a huge cell array, ca. Now write this out all in one shot to your worksheet.
% Now send the cell array to the spreadsheet.
xlswrite('trialdata.xlsx', ca,'Sheet1');
  3 Comments
Image Analyst
Image Analyst on 20 Jan 2012
Moved: DGM on 29 Dec 2023
Sidath: You didn't follow my advice 100% accurately. See - in my code I had "blob" and "row": two different counters, while you have only one counter "j". So your "ca" gets overwritten every single image, unlike the way I did it.
Moreover, you don't need all those se assignments inside your loop - they can be taken out and placed before any loop. Just build up [se90 se75 se60 se45 se30 se0] before the loop ever starts and pass in one array into imdilate and don't force it to recompute and stitch them all together for every image. Same with computing cform.
Sid
Sid on 20 Jan 2012
Moved: DGM on 29 Dec 2023
Hi,
wow! thank you very much for the explanation - really appreciate that. I managed to fix the code and it's running without any issues. And thank you very much for the suggestion on the se and cform.
Regards,
Sid

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!