MATLAB计算机视觉与机器认知----Haar矩形遍历演示

286 篇文章 31 订阅
236 篇文章 15 订阅
clc; clear; close all;

% Haar-like特征矩形计算

board = 24                                              % 检测窗口宽度
num = 24                                                % 检测窗口分划数

show = 1;                                               % 1为作图
time = 0.001;                                           % 作图间隔

%%

if mod(board,num)~=0
    error('检测窗口宽度必须是分划数的整数倍')
else
    delta = board/num                                   % 滑动步进值 
end

%% Haar特征1:左白,右黑,(s,t)=(1,2)

s = 1;
t = 2;
R = s:s:floor(num/s)*s;                                 % Haar窗口高
C = t:t:floor(num/t)*t;                                 % Haar窗口宽
NUM = 0;                                                % Haar特征总数

'---- Haar特征1:左白,右黑,(s,t)=(1,2) ---'
for I = 1:length(R)
    for J = 1:length(C)
       
        r = R(I)*delta;                                   % Haar窗口高
        c = C(J)*delta;                                  % Haar窗口宽
        nr = num-R(I)+1;                                 % 行方向移动个数
        nc = num-C(J)+1;                                 % 列方向移动个数
       
        Px0 = [0 r];                                     % 矩形坐标初始化
        Py0 = [0 c/2 c];
        for i = 1:nr
            for j = 1:nc
                Px = Px0+(i-1)*delta;                    % 滑动取点
                Py = Py0+(j-1)*delta;
                NUM = NUM+1;
               
                if show
                    plot([0 board],repmat((0:delta:board)',1,2),'k'); hold on;
                    plot(repmat((0:delta:board)',1,2),[0 board],'k'); axis tight; axis square;
                    title('Haar矩形遍历演示');xlabel('x');ylabel('y');
                   
                    plot(Px,repmat(Py',1,2),'r','LineWidth',5)
                    plot(repmat(Px,2,1),repmat([Py(1) Py(end)]',1,2),'r','LineWidth',5); hold off
                    pause(time)
                end
               
            end
        end
       
    end
end
NUM

%% Haar特征2:上白,下黑,(s,t)=(2,1)

s = 2;
t = 1;
R = s:s:floor(num/s)*s;                                 % Haar窗口高
C = t:t:floor(num/t)*t;                                 % Haar窗口宽
NUM = 0;                                                % Haar特征总数

'---- Haar特征2:上白,下黑,(s,t)=(2,1) ---'
for I = 1:length(R)
    for J = 1:length(C)
       
        r = R(I)*delta;                                  % Haar窗口高
        c = C(J)*delta;                                  % Haar窗口宽
        nr = num-R(I)+1;                                 % 行方向移动个数
        nc = num-C(J)+1;                                 % 列方向移动个数
       
        Px0 = [0 r/2 r];                                 % 矩形坐标初始化
        Py0 = [0 c];
        for i = 1:nr
            for j = 1:nc
                Px = Px0+(i-1)*delta;                    % 滑动取点
                Py = Py0+(j-1)*delta;
                NUM = NUM+1;

                if show
                    plot([0 board],repmat((0:delta:board)',1,2),'k'); hold on;
                    plot(repmat((0:delta:board)',1,2),[0 board],'k'); axis tight; axis square;
                    title('Haar矩形遍历演示');xlabel('x');ylabel('y');
                   
                    plot(repmat(Px,2,1),repmat(Py',1,length(Px)),'r','LineWidth',3);
                    plot(repmat([Px(1) Px(end)]',1,2),repmat(Py,2,1),'r','LineWidth',3); hold off
                    pause(time)
                end
               
            end
        end
       
    end
end
NUM

%% Haar特征3:左右白,中间黑,(s,t)=(1,3)

s = 1;
t = 3;
R = s:s:floor(num/s)*s;                                 % Haar窗口高
C = t:t:floor(num/t)*t;                                 % Haar窗口宽
NUM = 0;                                                % Haar特征总数

'---- Haar特征3:左右白,中间黑,(s,t)=(1,3) ---'
for I = 1:length(R)
    for J = 1:length(C)
       
        r = R(I)*delta;                                  % Haar窗口高
        c = C(J)*delta;                                  % Haar窗口宽
        nr = num-R(I)+1;                                 % 行方向移动个数
        nc = num-C(J)+1;                                 % 列方向移动个数
       
        Px0 = [0 r];                                     % 矩形坐标初始化
        Py0 = [0 c/3 c*2/3 c];
        for i = 1:nr
            for j = 1:nc
                Px = Px0+(i-1)*delta;                    % 滑动取点
                Py = Py0+(j-1)*delta;
                NUM = NUM+1;
               
                if show
                    plot([0 board],repmat((0:delta:board)',1,2),'k'); hold on;
                    plot(repmat((0:delta:board)',1,2),[0 board],'k'); axis tight; axis square;
                    title('Haar矩形遍历演示');xlabel('x');ylabel('y');
                   
                    plot(Px,repmat(Py',1,2),'r','LineWidth',5)
                    plot(repmat(Px,2,1),repmat([Py(1) Py(end)]',1,2),'r','LineWidth',5); hold off
                    pause(time)
                end

            end
        end
       
    end
end
NUM

%% Haar特征4:左右白,中间黑(2倍宽度),(s,t)=(1,4)

s = 1;
t = 4;
R = s:s:floor(num/s)*s;                                 % Haar窗口高
C = t:t:floor(num/t)*t;                                 % Haar窗口宽
NUM = 0;                                                % Haar特征总数

'---- Haar特征4:左右白,中间黑(2倍宽度),(s,t)=(1,4) ---'
for I = 1:length(R)
    for J = 1:length(C)
       
        r = R(I)*delta;                                  % Haar窗口高
        c = C(J)*delta;                                  % Haar窗口宽
        nr = num-R(I)+1;                                 % 行方向移动个数
        nc = num-C(J)+1;                                 % 列方向移动个数
       
        Px0 = [0 r];                                     % 矩形坐标初始化
        Py0 = [0 c/4 c*3/4 c];
        for i = 1:nr
            for j = 1:nc
                Px = Px0+(i-1)*delta;                    % 滑动取点
                Py = Py0+(j-1)*delta;
                NUM = NUM+1;
       
                if show
                    plot([0 board],repmat((0:delta:board)',1,2),'k'); hold on;
                    plot(repmat((0:delta:board)',1,2),[0 board],'k'); axis tight; axis square;
                    title('Haar矩形遍历演示');xlabel('x');ylabel('y');
                   
                    plot(Px,repmat(Py',1,2),'r','LineWidth',5)
                    plot(repmat(Px,2,1),repmat([Py(1) Py(end)]',1,2),'r','LineWidth',5); hold off
                    pause(time)
                end
               
            end
        end
       
    end
end
NUM

%% Haar特征5:上下白,中间黑,(s,t)=(3,1)

s = 3;
t = 1;
R = s:s:floor(num/s)*s;                                 % Haar窗口高
C = t:t:floor(num/t)*t;                                 % Haar窗口宽
NUM = 0;                                                % Haar特征总数

'---- Haar特征5:上下白,中间黑,(s,t)=(3,1) ---'
for I = 1:length(R)
    for J = 1:length(C)
       
        r = R(I)*delta;                                  % Haar窗口高
        c = C(J)*delta;                                  % Haar窗口宽
        nr = num-R(I)+1;                                 % 行方向移动个数
        nc = num-C(J)+1;                                 % 列方向移动个数
       
        Px0 = [0 r/3 r*2/3 r];                           % 矩形坐标初始化
        Py0 = [0 c];
        for i = 1:nr
            for j = 1:nc
                Px = Px0+(i-1)*delta;                    % 滑动取点
                Py = Py0+(j-1)*delta;
                NUM = NUM+1;
               
                if show
                    plot([0 board],repmat((0:delta:board)',1,2),'k'); hold on;
                    plot(repmat((0:delta:board)',1,2),[0 board],'k'); axis tight; axis square;
                    title('Haar矩形遍历演示');xlabel('x');ylabel('y');
                   
                    plot(repmat(Px,2,1),repmat(Py',1,length(Px)),'r','LineWidth',3);
                    plot(repmat([Px(1) Px(end)]',1,2),repmat(Py,2,1),'r','LineWidth',3); hold off
                    pause(time)
                end
               
            end
        end
       
    end
end
NUM

%% Haar特征6:上下白,中间黑(2倍宽度),(s,t)=(4,1)

s = 4;
t = 1;
R = s:s:floor(num/s)*s;                                 % Haar窗口高
C = t:t:floor(num/t)*t;                                 % Haar窗口宽
NUM = 0;                                                % Haar特征总数

'---- Haar特征6:上下白,中间黑(2倍宽度),(s,t)=(4,1) ---'
for I = 1:length(R)
    for J = 1:length(C)
       
        r = R(I)*delta;                                  % Haar窗口高
        c = C(J)*delta;                                 % Haar窗口宽
        nr = num-R(I)+1;                                 % 行方向移动个数
        nc = num-C(J)+1;                                 % 列方向移动个数
       
        Px0 = [0 r/4 r*3/4 r];                           % 矩形坐标初始化
        Py0 = [0 c];
        for i = 1:nr
            for j = 1:nc
                Px = Px0+(i-1)*delta;                    % 滑动取点
                Py = Py0+(j-1)*delta;
                NUM = NUM+1;

                if show
                    plot([0 board],repmat((0:delta:board)',1,2),'k'); hold on;
                    plot(repmat((0:delta:board)',1,2),[0 board],'k'); axis tight; axis square;
                    title('Haar矩形遍历演示');xlabel('x');ylabel('y');
                   
                    plot(repmat(Px,2,1),repmat(Py',1,length(Px)),'r','LineWidth',3);
                    plot(repmat([Px(1) Px(end)]',1,2),repmat(Py,2,1),'r','LineWidth',3); hold off
                    pause(time)
                end
               
            end
        end
       
    end
end
NUM

%% Haar特征7:左上右下白,其它黑,(s,s)=(2,2)


s = 2;
t = 2;
R = s:s:floor(num/s)*s;                                 % Haar窗口高
C = t:t:floor(num/t)*t;                                 % Haar窗口宽
NUM = 0;                                                % Haar特征总数

'---- Haar特征7:左上右下白,其它黑,(s,s)=(2,2) ---'
for I = 1:length(R)
    for J = 1:length(C)
       
        r = R(I)*delta;                                  % Haar窗口高
        c = C(J)*delta;                                  % Haar窗口高
        nr = num-R(I)+1;                                 % 行方向移动个数
        nc = num-C(J)+1;                                 % 行方向移动个数
       
        Px0 = [0 r/2 r];                           % 矩形坐标初始化
        Py0 = [0 c/2 c];                           % 矩形坐标初始化
        for i = 1:nr
            for j = 1:nc
                Px = Px0+(i-1)*delta;                    % 滑动取点
                Py = Py0+(j-1)*delta;
                NUM = NUM+1;
               
                if show
                    plot([0 board],repmat((0:delta:board)',1,2),'k'); hold on;
                    plot(repmat((0:delta:board)',1,2),[0 board],'k'); axis tight; axis square;
                    title('Haar矩形遍历演示');xlabel('x');ylabel('y');
                   
                    plot(repmat(Px,3,1),repmat(Py',1,length(Px)),'r','LineWidth',3);
                    plot(repmat([Px(1) Px(end)]',1,3),repmat(Py,2,1),'r','LineWidth',3); hold off
                    pause(time)
                end
               
            end
        end
       
    end
end
NUM

%% Haar特征8:四周白,中间黑,(s,s)=(3,3)

s = 3;
t = 3;
R = s:s:floor(num/s)*s;                                 % Haar窗口高
C = t:t:floor(num/t)*t;                                 % Haar窗口宽
NUM = 0;                                                % Haar特征总数

'---- Haar特征8:四周白,中间黑,(s,s)=(3,3) ---'
for I = 1:length(R)
    for J = 1:length(C)
       
        r = R(I)*delta;                                  % Haar窗口高
        c = C(J)*delta;                                  % Haar窗口高
        nr = num-R(I)+1;                                 % 行方向移动个数
        nc = num-C(J)+1;                                 % 行方向移动个数
       
        Px0 = [0 r/3 r*2/3 r];                           % 矩形坐标初始化
        Py0 = [0 c/3 c*2/3 c];                           % 矩形坐标初始化
        for i = 1:nr
            for j = 1:nc
                Px = Px0+(i-1)*delta;                    % 滑动取点
                Py = Py0+(j-1)*delta;
                NUM = NUM+1;
               
                if show
                    plot([0 board],repmat((0:delta:board)',1,2),'k'); hold on;
                    plot(repmat((0:delta:board)',1,2),[0 board],'k'); axis tight; axis square;
                    title('Haar矩形遍历演示');xlabel('x');ylabel('y');
                   
                    plot(repmat(Px,4,1),repmat(Py',1,length(Px)),'r','LineWidth',3);
                    plot(repmat([Px(1) Px(end)]',1,4),repmat(Py,2,1),'r','LineWidth',3); hold off
                    pause(time)
                end
               
            end
        end
       
    end
end

在这里插入图片描述

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Python中,可以使用OpenCV库的 `cv2.CascadeClassifier` 类来进行人脸检测,它使用的是基于Haar特征的级联分类器。而这个级联分类器的训练需要用到Haar特征。下面是如何提取Haar特征的步骤: 1. 安装OpenCV库。可以使用 `pip install opencv-python` 命令进行安装。 2. 下载Haar特征文件。可以在OpenCV官方网站上下载 `haarcascade_frontalface_default.xml` 文件,该文件包含了用于检测人脸的Haar特征。 3. 加载Haar特征文件。可以使用 `cv2.CascadeClassifier` 类加载Haar特征文件。 ```python import cv2 # 加载Haar特征文件 face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') ``` 4. 加载图像。可以使用 `cv2.imread` 函数加载图像。 ```python # 加载图像 img = cv2.imread('test.jpg') ``` 5. 将图像转换为灰度图像。Haar特征是基于灰度图像的,因此需要将彩色图像转换为灰度图像。 ```python # 将图像转换为灰度图像 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) ``` 6. 检测人脸。可以使用 `detectMultiScale` 函数检测图像中的人脸。该函数可以检测多个不同大小的人脸。 ```python # 检测人脸 faces = face_cascade.detectMultiScale(gray, 1.3, 5) ``` 7. 遍历检测到的人脸列表,并在图像上绘制出矩形框。 ```python # 在图像上绘制人脸矩形框 for (x,y,w,h) in faces: cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2) ``` 完整代码如下: ```python import cv2 # 加载Haar特征文件 face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') # 加载图像 img = cv2.imread('test.jpg') # 将图像转换为灰度图像 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 检测人脸 faces = face_cascade.detectMultiScale(gray, 1.3, 5) # 在图像上绘制人脸矩形框 for (x,y,w,h) in faces: cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2) # 显示图像 cv2.imshow('img',img) cv2.waitKey(0) cv2.destroyAllWindows() ``` 需要注意的是,Haar特征的提取是训练Haar分类器的过程,而不是在Python中提取Haar特征。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值