How do I save a variable in a for loop by using the same name but changing the number.

3 ビュー (過去 30 日間)
Adel
Adel 2014 年 4 月 18 日
回答済み: Image Analyst 2014 年 4 月 18 日
Hello,
As you can see from my code below, I am getting an image of 5 cells and placing a bounding box around each one. After producing the bounding box, I simply wanted to crop each separate cell into its own image separate image labeled 'Crop_1', 'Crop_2', etc. In order to do that I need to save the variable within each loop in order for it to not be overwritten by the next loop. Currently, my code will only crop the image of the last loop run.
So in other words, how do I write code within a loop to save the output of the loop as a variable and just simply making the variable name Crop_(i) with i representing the specific number cell.
Thank you in advanced
for i = 1:length(stats)
Cell_i = stats(i).BoundingBox;
rectangle('Position', [Cell_i(1),Cell_i(2),Cell_i(3),...
Cell_i(4)],'EdgeColor',cstring(mod(i,5)+1),'LineWidth',2);
disp(['Cell ', num2str(i), ' : ',num2str(stats(i).Area)]);
end
crop_img = imcrop(final_img,Cell_i);
imshow(crop_img)
imwrite(crop_img,'cell1.tif')

回答 (2 件)

Walter Roberson
Walter Roberson 2014 年 4 月 18 日

Image Analyst
Image Analyst 2014 年 4 月 18 日
See my tutorial where I extract/crop subimages that have blobs of interest (coins) in them: http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862

カテゴリ

Help Center および File ExchangeImage Processing Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by