How to extract an image from a bounding box?

8 次查看(过去 30 天)
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?

回答(2 个)

Lenin Falconi
Lenin Falconi 2018-12-29
编辑:Image Analyst 2018-12-29
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 2018-12-29
编辑:Image Analyst 2018-12-29
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

类别

Help CenterFile Exchange 中查找有关 Images 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by