How do I save a variable in a for loop by using the same name but changing the number.
3 次查看(过去 30 天)
显示 更早的评论
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')
0 个评论
回答(2 个)
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Processing Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!