How to save information from a for loop

%% 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 件のコメント

Image Analyst
Image Analyst 2022 年 1 月 8 日
Why is there a loop? if bw2 does not change, then the regionprops won't change and so no loop is needed.
Rohit Thokala
Rohit Thokala 2022 年 1 月 8 日
Hello sir @Image Analyst, bw2 change everytime
Rohit Thokala
Rohit Thokala 2022 年 1 月 9 日
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.

サインインしてコメントする。

 採用された回答

yanqi liu
yanqi liu 2022 年 1 月 10 日

1 投票

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 件のコメント

Rohit Thokala
Rohit Thokala 2022 年 1 月 10 日
Hello @yanqi liu, I am getting the following error message.
yanqi liu
yanqi liu 2022 年 1 月 10 日
now b is empty,may be add if judge,such as
if isempty(data)
continue;
end
Rohit Thokala
Rohit Thokala 2022 年 1 月 10 日
Hello @yanqi liu, where can I add this to loop? sorry, I am new to coding
yanqi liu
yanqi liu 2022 年 1 月 10 日
yes,sir,please upload your new m file,and we can debug it
now,use last files,we can not find this error
Rohit Thokala
Rohit Thokala 2022 年 1 月 10 日
Rohit Thokala
Rohit Thokala 2022 年 1 月 10 日
Hello @yanqi liu, code is working properly. Thank you so much for your help
yanqi liu
yanqi liu 2022 年 1 月 10 日
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)

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2022 年 1 月 9 日

0 投票

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);

カテゴリ

ヘルプ センター および File ExchangeLanguage Support についてさらに検索

製品

リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by