【问题标题】:MATLAB - Colour an Area around a bounding box with BlackMATLAB - 用黑色为边界框周围的区域着色
【发布时间】:2014-12-02 04:01:15
【问题描述】:

我有一个代码可以检测图像中的人脸并在图像周围放置一个边界框,如下所示。

但我想更进一步,将边界框外的区域涂成黑色,这样就只能看到脸,背景变成黑色。 原始代码..

FDetect = vision.CascadeObjectDetector;
I = imread('PresidentClinton.jpg');

%Returns Bounding Box values based on number of objects
BB = step(FDetect,I);

figure,
imshow(I); hold on
for i = 1:size(BB,1)
rectangle('Position',BB(i,:),'LineWidth',5,'LineStyle','-','EdgeColor','r');
end
title('Face Detection');
hold off;

【问题讨论】:

    标签: matlab detection bounding-box


    【解决方案1】:

    这是一个简单的方法,您首先创建一个与原始图像大小/类别相同的目标图像,然后用黑色填充它。然后获取矩形坐标并将原始图像中的数据分配给目标图像:

    clear
    close all
    
    A = imread('peppers.png');
    B = zeros(size(A),class(A)); % //Pre-define target image of identical size and class than original.
    
    %// You could also use this line: 
    %//B = zeros(size(A),'like',A);
    
    
    hRect = rectangle('Position',[100 100 200 160],'LineWidth',3); %// Define rectangle
    
    RectPos = get(hRect, 'Position'); %// Get the coordinates of the rectangle.
    
    x = RectPos(1):RectPos(1)+RectPos(3); %// Define x- and y-span
    y = RectPos(2):RectPos(2)+RectPos(4);
    
    B(x,y,:) = A(x,y,:); %// Assign the selected part of the image to B
    
    figure
    subplot(1,2,1)
    imshow(A)
    subplot(1,2,2)
    imshow(B)
    

    给这样的东西:

    当然还有其他方法,但我认为这种方法简单明了,易于在循环中实现。

    【讨论】:

    • 这就是你想要的吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-18
    • 1970-01-01
    • 1970-01-01
    • 2011-10-21
    • 2023-04-02
    • 1970-01-01
    • 2016-01-04
    相关资源
    最近更新 更多