0

I have an image where I want to get region per region from the same using BoundingBox on MATLAB, this is the example where I use BoundingBox:

Ic=regionprops(logical(I3),'BoundingBox');

Where I3 is the image that I want to get region per region and then display region per region, the unique thing that I know about BoundingBox is that in my case, Ic is the variable where they saved the regions from the I3 Image that are 103 field or region, but I don't know how to display region per region in different figure, I understand that doing that, MATLAB will show 103 figures, I dont have any problem with that.

rebatoma
  • 734
  • 1
  • 17
  • 32

2 Answers2

0

The result of finding the BoundingBox with regionprops is a rectangle defined by [x, y, width, height]. You can use these results to plot a rectangle using the built-in rectangle function.

If you wanted to put them all on the same axis you could do:

fig = figure;
him = imshow(I3);
hold on;

colors = hsv(numel(Ic));

% Now plot all the rectangles
for k = 1:numel(Ic)
    rectangle('Position', Ic(k).BoundingBox, 'EdgeColor', colors(k,:));
end

And if you want a new figure for every bounding box result:

% Anonymous function to help with the conversion from rect
rect2rng = @(pos,len)ceil(pos):(ceil(pos)+len-1);

for k = 1:numel(Ic)
    rect = Ic(k).BoundingBox;
    subImage = I3(rect2rng(rect(2), rect(4)), rect2rng(rect(1), rect(3)));
    fig = figure;
    him = imshow(subImage);
    title(sprintf('Bounding Box #%d', k)); 
end
Suever
  • 64,497
  • 14
  • 82
  • 101
  • The idea about plot all the rectangles is a great idea and, exactly, I want a new figure for every bounding box, but the code that you suggest, only display the same image 103 times, and not each figure per separated, the code that I used is: Ic=regionprops(logical(I3),'BoundingBox'); fig = figure; him = imshow(I3); hold on; colors = hsv(numel(Ic)); for k = 1:numel(Ic) fig = figure; him = imshow(I3); hold on; rectangle('Position', Ic(k).BoundingBox); end – christian briseño Feb 29 '16 at 21:40
  • @christianbriseño are you saying you want a figure showing an image of *just* what's inside each bounding box? – Suever Feb 29 '16 at 21:42
  • yes, I want one figure per field from the BoundingBox – christian briseño Feb 29 '16 at 22:00
  • @christianbriseño I have updated the second part of my answer – Suever Feb 29 '16 at 22:10
0

The idea about plot all the rectangles is a great idea and, exactly, I want a new figure for every bounding box, but the code that you suggest, only display the same image 103 times, and not each figure per separated, the code that I used is:

Ic=regionprops(logical(I3),'BoundingBox'); 
fig = figure; 
him = imshow(I3); 
hold on; colors = hsv(numel(Ic)); 
for k = 1:numel(Ic) 
  fig = figure;  
  him = imshow(I3); 
hold on; 
rectangle('Position', Ic(k).BoundingBox); 
end