【问题标题】:MATLAB Object Detection using Edge Detection and Bounding Boxes使用边缘检测和边界框的 MATLAB 对象检测
【发布时间】:2014-08-02 17:51:50
【问题描述】:

我正在尝试使用精明的边缘检测来检测纯色背景上的对象。

我能够获取所有边缘并在它们周围绘制矩形,但我正在努力在所有矩形周围绘制一个矩形以希望用于裁剪对象。

我对以下代码感到相当满意(它从原始图像中得到了上面的图像):

original = imread('1.jpg');
img = rgb2gray(original);

BW = edge(img,'canny',0.09);

[B,L,N,A] = bwboundaries(BW);
figure; imshow(BW); hold on;
for k=1:length(B),
    if(~sum(A(k,:)))
       boundary = B{k};
     plot(boundary(:,2),boundary(:,1),'r','LineWidth',2);hold on;
    end
end


blobMeasurements = regionprops(logical(BW), 'BoundingBox');
numberOfBlobs = size(blobMeasurements, 1);


rects = [];
for k = 1 : numberOfBlobs % Loop through all blobs.
rects = blobMeasurements(k).BoundingBox; % Get list ofpixels in current blob.
x1 = rects(1);
y1 = rects(2);
x2 = x1 + rects(3);
y2 = y1 + rects(4);
x = [x1 x2 x2 x1 x1];
y = [y1 y1 y2 y2 y1];
plot(x, y, 'LineWidth', 2);
end

但我没有得到以下工作(我希望得到外部矩形并裁剪原始图像):

% get min max
xmin=min(rects(:,1));
ymin=min(rects(:,2));
xmax=max(rects(:,1)+rects(:,3));
ymax=max(rects(:,2)+rects(:,4));

% define outer rect:
outer_rect=[xmin ymin xmax-xmin ymax-ymin];


rect = rectangle('Position',outer_rect));

crop = imcrop(original,rect);
figure
imshow(crop);

【问题讨论】:

    标签: matlab edge-detection bounding-box


    【解决方案1】:

    用下面的代码搞定

    original = imread('1.jpg');
    img = rgb2gray(original);
    
    BW = edge(img,'canny',0.08);
    
    [B,L,N,A] = bwboundaries(BW);
    
    for k=1:length(B),
        if(~sum(A(k,:)))
           boundary = B{k};
        end
    end
    
    
    blobMeasurements = regionprops(logical(BW), 'BoundingBox');
    numberOfBlobs = size(blobMeasurements, 1);
    
    
    rectCollection = [];
    for k = 1 : numberOfBlobs % Loop through all blobs.
    rects = blobMeasurements(k).BoundingBox; % Get list ofpixels in current blob.
    x1 = rects(1);
    y1 = rects(2);
    x2 = x1 + rects(3);
    y2 = y1 + rects(4);
    x = [x1 x2 x2 x1 x1];
    y = [y1 y1 y2 y2 y1];
    rectCollection(k,:,:,:) = [x1; y1; x2; y2];
    end
    
    % get min max
    xmin=min(rectCollection(:,1));
    ymin=min(rectCollection(:,2));
    xmax=max(rectCollection(:,3));
    ymax=max(rectCollection(:,4));
    
    % define outer rect:
    outer_rect=[xmin ymin xmax-xmin ymax-ymin];
    
    crop = imcrop(original,outer_rect);
    
    imshow(crop);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多