How to extract an image from a bounding box?

18 views (last 30 days)
Darsana P M
Darsana P M on 19 Nov 2016
Edited: Image Analyst on 29 Dec 2018
s1=bwareaopen(s1,500);
se=strel('disk',2);
s1=imdilate(s1,se);
s1=bwareaopen(s1,800);
stats=regionprops(s1,'area','boundingbox');
[LI,CO1]= bwlabeln(s1,18);
D=CO1;
subplot(2,2,3)
imshow(D);
hold on
k=0;
for i=1:length(stats)
bb=stats.boundingbox;
triangle('position',bb,'edgecolor','r','LineWidth','2');
k=k+1
end
hold off
cnt=0;
if k==1|d==1
d isp('road');
cnt=cnt+1;
printf(road,'1');
end
s1,is the input image. Can anybody help me out to extract the image from bounding box?

Answers (2)

Lenin Falconi
Lenin Falconi on 29 Dec 2018
Edited: Image Analyst on 29 Dec 2018
Hello I don't know if the following might help you even though it seems really late for answering this question.
So as far as I understand you need to binarize your image and look for connected areas. After that labelling the areas is needed and regionprops can be applied. In the example that I am sharing I usually need the widest area so I use a max. After that I get the bounding box as a rectangle with x, y, width and height. Then extracting the cropped image from the original is like getting a sub matrix from a matrix
bina=im2bw(img_gris,graythresh(img_gris)*0.7);
% Labelling
L=bwlabel(bina);
% Get area and boundingBox
prop=regionprops(L,{'Area','BoundingBox'});
% In my case I need the widest area
[m pam]=max([prop.Area]);
boundy = prop.BoundingBox;
% "roi" has widest area
roi=ismember(L,pam);%ismember:detetcs members of the group
SE=strel('disk',15,0);
roi=imdilate(roi, SE);
% % Multiplying original image with mask
img_gris= img_gris.*uint8(roi);
figure(1), imshow(img_gris), title('Imagen con BoundingBox');
hold on
rectangle('Position', boundy,...
'EdgeColor','r', 'LineWidth', 3)
hold off
x = boundy(1);
y = boundy(2);
w = boundy(3);
h = boundy(4);
size(img_gris)
img_cut = img_gris(x:x+h,y:y+w); % Img Cutted or extrated from bounding box
figure(2), imshow(img_cut), title('recorte');

Image Analyst
Image Analyst on 29 Dec 2018
Edited: Image Analyst on 29 Dec 2018
Not sure how I missed this question 2 years ago, but you can see my Image Processing Tutorial in My File Exchange
You can either use indexing, like Lenin showed, or you can use imcrop() if you pass in the bounding box.
screenshot.jpg

Community Treasure Hunt

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

Start Hunting!