提取BW图像中对象的边界框并将其打印在原始图像上。

时间:2014-03-23 21:20:42

标签: matlab bounding-box

我正在开展有关图像检测的项目,我需要检测汽车挡风玻璃上的徽标,然后在原始图像上绘制相应的边界框。

我之前从未使用过MATLAB,所以我遇到了很多麻烦。到目前为止,这是我的代码。我在这里已经阅读了类似主题的一些答案,但我不明白如何使用它。

  %Assumption :  Image aquistion condition in terms of illuniation and Zoom
    clc;
close all;
clear all;

A=imread('C:\Users\MKN\Desktop\1\64.jpg');
figure, imshow(A);

A2=rgb2gray(A);
figure, imshow(A2);


A2=A2>1.45*mean2(A2);
 figure, imshow(A2)
A3 = imclearborder(A2);
figure, imshow(A3)

A3= imclose(A3,strel('disk',5));
 figure, imshow(A3);

bw=A3;
bw = bwareaopen(bw,600);
se = strel('disk',4);
bw = imclose(bw,se);
bw = imfill(bw,'holes');
figure, imshow(bw)

se=strel('square', 15);
X=bw;
for t=1:4;
X=imerode(X, se)
end
X=bwmorph(X,'bridge');
 figure, imshow(X);


[B,L] = bwboundaries(bw,'noholes');
imshow(label2rgb(L, @jet, [.7 .7 .7]));
hold on
for k = 1:length(B);
  boundary = B{k};
  plot(boundary(:,2), boundary(:,1), 'w', 'LineWidth', 2);
end

L=bw;
stats = regionprops(bwlabel(L),'Area','Centroid','Perimeter','BoundingBox');
areas=cat(1,stats.Area);
average_area=mean(areas);
prems=cat(1,stats.Perimeter);
cents=cat(1,stats.Centroid);
Boxes=cat(1,stats.BoundingBox)
[areas,prems,cents,Boxes]
metric = (4*pi*areas)./prems.^2  ; % Circularity
threshold = 0.7;
for i=1:length(metric)
ibw=bwlabel(bw)==i ; % 

if areas(i)< 0.3*max(areas);% use ith area is less average_area 
  ibw=imresize(ibw,3); % use imreseize to scale up 

  istat = regionprops(bwlabel(ibw),'Area','Perimeter');                             % update metric (i)= (4*pi*areas(i))./prems(i).^2
  iarea=cat(1,istat.Area);
  iprem=cat(1,istat.Perimeter)
  iexmetric=metric(i);
  metric(i) = (4*pi*iarea)/iprem^2; 
  [ i  metric(i) iexmetric ]
end

end

threshold = 0.7;
for k = 1:length(B)
  boundary = B{k};

   % compute a simple estimate of the object's perimeter
   delta_sq = diff(boundary).^2;
   perimeter = sum(sqrt(sum(delta_sq,2)));

  % obtain the area calculation corresponding to label 'k'
  area = stats(k).Area;

  % compute the roundness metric
  kmetric = 4*pi*area/perimeter^2;

   % display the results
  metric_string = sprintf('%2.2f',kmetric);

  % mark objects above the threshold with a black circle
  if kmetric > threshold
    centroid = stats(k).Centroid;
    plot(centroid(1),centroid(2),'ko');
  end

  text(boundary(1,2)-35,boundary(1,1)+13,metric_string,'Color','m',...
       'FontSize',11,'FontWeight','bold');

end

1 个答案:

答案 0 :(得分:1)

将此添加到您的代码中,以提取由Bounding Box中提到的值表示的原始图像部分 -

for k1 = 1:size(Boxes,1)

    %%// Initialize a mask representing each bounding box
    mask1 = false(size(A,1),size(A,2));

    %%// Get the coordinates of the boxes
    starty = round(Boxes(k1,1));
    stopy = starty+round(Boxes(k1,3))-1;
    startx = round(Boxes(k1,2));
    stopx = startx+round(Boxes(k1,4))-1;

    %%// Finaly create the mask
    mask1(startx:stopx,starty:stopy) = true;
    mask11 = repmat(mask1,[1 1 size(A,3)]);

    %%// Show only the mask region by zeroing out rest of the original image
    A1 = A;
    A1(~mask11) = 0;
    figure,imshow(A1) %%// Show the bounding box regions from the original image

end