How to save information from a for loop

2 views (last 30 days)
%% DEFINE input and output path
full_folder = 'C:\Users\RR\Desktop\practice\input_folder_T_60_inj_05';
output_folder = uigetdir("select folder");
input_files = fullfile(full_folder,"*.tif");
subfolders = dir(input_files);
%% background subtraction
background = im2uint16(mat2gray(imread(subfolders(1).name)));
%% Image processing
for i = 1 : length(subfolders)
image = im2uint16(mat2gray(imread(subfolders(i).name)));
image_back_sub = image - background;
image_gray = im2gray(image_back_sub);
tilt = imrotate(image_gray,-0.7,'bilinear','crop');
%color_inversion = imcomplement(image_gray); % this code works for color inversion
BinaryImage=imbinarize(tilt, 0.1);
bw1 = imopen(BinaryImage, strel('line', round(size(BinaryImage,2)*0.03), 0));
bw2 = imdilate(bw1, strel('square', 4));
bw3 = imdilate(bw2, strel('square', 2));
stats = regionprops(bw3);
data = struct2table(stats)
[~,idx] = max(data.Area(:,1))
r = data(idx,:)
sprayRadius = r.BoundingBox(1,3)
sprayHeight = r.BoundingBox(1,4)
imwrite(bw3,[output_folder '/', subfolders(i).name]);
end
stats is the table shown above. I want to store last two values as sprayRadius and sprayHeight in every iteration. How can I do this? Thanks in advance. Reference images are in the comments (img1,img2,background)
  3 Comments
Rohit Thokala
Rohit Thokala on 9 Jan 2022
Hello sir (@Image Analyst), I want only width and height of the spray associated with maximum area in the table, not all the heights and widths. I am attaching my full code here with three images (one background and two spray images) for code to run. This code is working to process single image and I want to loop this over a range of images and finally get width and height of all images. I am also attaching single image processing code.

Sign in to comment.

Accepted Answer

yanqi liu
yanqi liu on 10 Jan 2022
result = [];
%% Image processing
for i = 1 : length(subfolders)
image = im2uint16(mat2gray(imread(subfolders(i).name)));
image_back_sub = image - background;
image_gray = rgb2gray(image_back_sub);
tilt = imrotate(image_gray,-0.7,'bilinear','crop'); % impingement plate has little inclination
BinaryImage=imbinarize(tilt, 0.2);
bw1 = imopen(BinaryImage, strel('line', round(size(BinaryImage,2)*0.03), 0));
bw2 = imdilate(bw1, strel('square', 4));
bw3 = imdilate(bw2, strel('square', 2));
stats = regionprops(bw3);
data = struct2table(stats)
[~,idx] = max(data.Area(:,1))
r = data(idx,:)
figure; imshow(image, []); hold on;
for j = 1 : length(stats)
rectangle('Position', stats(j).BoundingBox, 'EdgeColor', 'r', 'LineWidth', 2);
end
% loop this over a range of images and finally get width and height of all images
sprayRadius = r.BoundingBox(1,3);
sprayHeight = r.BoundingBox(1,4);
% sn(i).filename = subfolders(i).name;
% sn(i).sprayRadius = sprayRadius;
% sn(i).sprayHeight = sprayHeight;
result{i,1} = subfolders(i).name;
result{i,2} = sprayRadius;
result{i,3} = sprayHeight;
imwrite(BinaryImage,[output_folder '/', subfolders(i).name]);
end
xlswrite('result.xlsx',result)
  7 Comments
Rohit Thokala
Rohit Thokala on 10 Jan 2022
Hello @yanqi liu, code is working properly. Thank you so much for your help
yanqi liu
yanqi liu on 10 Jan 2022
clear all;
clc;
close all
%% DEFINE input and output path
full_folder = './input_folder_T_80_inj_10/';
output_folder = './out';
input_files = fullfile(full_folder,"*.jpg");
subfolders = dir(input_files);
result = [];
%% background subtraction
background = im2uint16(mat2gray(imread(fullfile(subfolders(1).folder,subfolders(1).name))));
%% Image processing
for i = 1 : length(subfolders)
image = im2uint16(mat2gray(imread(fullfile(subfolders(1).folder,subfolders(i).name))));
image_back_sub = image - background;
image_gray = rgb2gray(image_back_sub);
tilt = imrotate(image_gray,-0.7,'bilinear','crop');
%color_inversion = imcomplement(image_gray); % this code works for color inversion
BinaryImage=imbinarize(tilt, 0.1);
B2 = bwareafilt(BinaryImage, 1); % select the largest component with bwareafilt
% imshowpair(B,B2,"montage")
bw1 = imopen(B2, strel('line', round(size(BinaryImage,2)*0.07), 0));
bw2 = imdilate(bw1, strel('square', 2));
widthIndex = any(B2);
horizontalPixelWidth = find(widthIndex,1,'last')-find(widthIndex,1,'first');
verticalPixelHeight = find(any(B2,2),1,'last') - find(any(bw2,2),1,'first');
imwrite(bw2,[output_folder '/', subfolders(i).name]);
result{i,1} = subfolders(i).name;
result{i,2} = horizontalPixelWidth;
result{i,3} = verticalPixelHeight;
end
result
xlswrite('result.xlsx',result)

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 9 Jan 2022
What is b? It shows b in the error but I don't see it in your code. You might have to attach your entire m-file.
Maybe try
bb = vertcat(stats.BoundingBox);
allWidths = bb(:, 3);
allHeights = bb(:, 4);
sprayRadius(i) = max(allWidths);
sprayHeights(i) = max(allHeights);

Categories

Find more on Graphics Performance in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!