matlab中给图像加几个矩形框_在图像中画矩形框(matlab)

参考代码:https://github.com/cuijiaxun/FaceRecognitionByMatlab 中的LabelDetectWindow.m

在目标检测的时候,一般都需要用矩形框圈出目标在图片中的大致范围,这时候可以考虑使用下面总结的两种方式:

1 利用在给定图片中矩形边框所处的位置的像素值赋值,从而显示出矩形框

% 2017-12-18 席**

% 给原始图像指定位置加上矩形边框标记

% 原理:给图片中矩形边框所在的位置像素值赋值,从而显示出矩形框

% 输入:

% image 需要进行标记的图像

% windowLocation 矩形框位置,其格式为[x,y],即[x坐标,y坐标](该位置为矩形最靠下最靠右的顶点)

% windowSize 矩形框尺寸,其格式为[height,width],即[高度,宽度]

%

% 输出:

% drawRectangleImage 在原始图像上加上矩形框的标记图像(仍为图像)

%

function [drawRectangleImage] = drawRectangleFrame01(image,windowLocation,windowSize)

[row,col] = size(image); % 输入图像尺寸

x = windowLocation(1);%矩形框位置坐标,其格式为[x,y]

y = windowLocation(2);

height = windowSize(1);%矩形框尺寸,其格式为[height,width],即[高度,宽度]

width = windowSize(2);

if((x<=row && y<=col)&&(height<=row && width<=col))

disp('矩形框合法!');

LabelLineColor = 255; % 标记线颜色

drawRectangleImage = image;

topMost = x-height; % 矩形框上边缘

botMost = x; % 矩形框下边缘

lefMost = y-width; % 矩形框左边缘

rigMost = y; % 矩形框右边缘

drawRectangleImage(topMost:botMost,lefMost,:) = LabelLineColor; % 左边框

drawRectangleImage(topMost:botMost,rigMost,:) = LabelLineColor; % 右边框

drawRectangleImage(topMost,lefMost:rigMost,:) = LabelLineColor; % 上边框

drawRectangleImage(botMost,lefMost:rigMost,:) = LabelLineColor; % 下边框

else

disp('矩形框不合法!');

end

2 利用hold on / hold off和rectangle函数在figure中绘出矩形框

这种方法得到只是一个figure,要想利用该figure还需自己保存为图像,不像1中可直接为工作空间中的图像变量

% 2017-12-18 席**

% 给原始图像指定位置加上矩形边框标记

% 原理:利用hold on / hold off和rectangle函数在figure中绘出矩形框

% 输入:

% image 需要进行标记的图像

% windowLocation 矩形框位置,其格式为[x,y],即[x坐标,y坐标](该位置为矩形最靠下最靠右的顶点)

% windowSize 矩形框尺寸,其格式为[height,width],即[高度,宽度]

%

% 输出:

% drawRectangleImage 并不是一个图片,而是一个???

%

function [drawRectangleImage] = drawRectangleFrame(image,windowLocation,windowSize)

[row,col] = size(image); % 输入图像尺寸

x = windowLocation(1);%矩形框位置坐标,其格式为[x,y]

y = windowLocation(2);

height = windowSize(1);%矩形框尺寸,其格式为[height,width],即[高度,宽度]

width = windowSize(2);

if((x<=row && y<=col)&&(height<=row && width<=col))

disp('矩形框合法!');

figure;imshow(image);

hold on

drawRectangleImage = rectangle('Position',[y-width,x-height,width,height],'LineWidth',4,'EdgeColor','r');

hold off

else

disp('矩形框不合法!');

end

测试调用的函数为

clear;close all;clc

I = imread('F:\matlab\drawRectangle\image_0101.jpg');

[row,col,n] = size(I); % 输入图像尺寸

II = rgb2gray(I);

III = drawRectangleFrame(II,[360,480],[200,100]);

%figure();

%imshow(III);

运行效果

1:

2:(手动保存的figure)

  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值