Getting Rid of Nested For Loops

2 ビュー (過去 30 日間)
Jacob Mevorach
Jacob Mevorach 2017 年 3 月 30 日
コメント済み: Jacob Mevorach 2017 年 4 月 5 日
So I have a bunch of nested for loops in the following code but I feel like there has to be a faster and better way to eliminate these things so it will run faster. If anyone had some insight into how I might do this I would be greatly appreciative.
function [mask] = bbox_analysis(bboxes, mask, blackwhiteframe, th, fractional_th)
% Cycle through every bbox in the frame
bbox_dim = size(bboxes);
for b = 1:bbox_dim(1)
x_1 = bboxes(b, 1);
y_1 = bboxes(b, 2);
x_2 = x_1 + bboxes(b, 3);
y_2 = y_1 + bboxes(b, 4);
cropped_image = imcrop(blackwhiteframe, [x_1, y_1, bboxes(b, 3), bboxes(b, 4)]); % Section of frame captured in bbox
max_image = max(cropped_image);
threshold = max(th/255, max_image*fractional_th);
new_image = (cropped_image > threshold);
for i = x_1:x_2-1
for j = y_1:y_2-1
mask(j, i) = new_image(j+1-y_1, i+1-x_1);
end
end
end
end
  2 件のコメント
Matt J
Matt J 2017 年 3 月 30 日
Are you sure you don't mean
max_image = max(cropped_image(:));
Jacob Mevorach
Jacob Mevorach 2017 年 3 月 31 日
You're right I do! Thanks for letting me know.

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

採用された回答

Jan
Jan 2017 年 4 月 3 日
Replace
for i = x_1:x_2-1
for j = y_1:y_2-1
mask(j, i) = new_image(j+1-y_1, i+1-x_1);
end
end
by
mask(y_1:y_2-1, x_1:x_2-1) = new_image((y_1+1-y_1):(y_2-1+1-y_1), (x_1+1-x_1):(x_2-1+1-x_1));
  3 件のコメント
Matt J
Matt J 2017 年 4 月 5 日
編集済み: Matt J 2017 年 4 月 5 日
The above should simplify to
mask(y_1:y_2-1, x_1:x_2-1) = new_image;
Jacob Mevorach
Jacob Mevorach 2017 年 4 月 5 日
Thank you! This also worked!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by