(轉)Haar-like矩形遍歷檢測窗口演示Matlab源代碼

from:http://blog.sina.com.cn/s/blog_736aa0540101kzqb.htmlhtml

 

 

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)spa

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
htm

%% 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
NUM

% 畢業院校:海軍工程大學,水聲工程專業,博士
% 精通方向:數字信號(圖像、視頻)處理,人工智能與模式識別,羣體智能優化,非線性與混沌,支持向量機,Matlab與VC++混編
% 現任崗位:瀋陽聚德視頻技術有限公司,圖像處理及模式識別研發工程師
% 工做職責:車牌識別,視頻目標跟蹤等算法開發,C/C++實現,DSP植入
% 興趣愛好:金融時序的程式化交易
%
% 主要成果:
% [1] 實現車牌識別C/C實現,DSP植入,識別率:漢字不低於99%,數字字母不低於99.5%,整牌不低於97%
% [2] 精通數字信號(圖像、視頻)「特徵提取」與「模式識別」的研究與開發,開展了「支持向量機」應用研究,原創文章有《四種支持向量機工具箱使用要點》,獨立開發了「支持向量機Matlab工具箱Version1.0」。結題項目有:語音信號處理與識別,遙感圖像的特徵提取與分類,人臉識別,主被動聲納信號處理與識別等
% [3] 精通「羣體智能優化」,原創工具箱有「羣體智能算法」Matlab工具箱 Version2.0」,偏差精度優於現有公開發表文獻,工程中解決了各類高維複雜問題的優化計算
% [4] 精通「時間序列混沌建模和預測」,基於Matlab和VC 混編平臺,獨立開發了混沌分析和預測軟件包「混沌時間序列分析與預測工具箱 Version2.9」。結題項目有:金融數據波動性分析與程式化交易,銀行反洗錢異常檢測系統,混沌背景弱信號檢測,海洋混響背景弱目標檢測等
% [5] 精通Matlab與VC 混合編程:(a)以VC 爲界面,核心算法採用Matlab函數,原創文章有《如何將Matlab7.0函數轉換成VC 6.0動態連接庫》;(b)以Matlab爲界面,耗時算法在VC 環境中採用Mexfunction編譯。
%
% 聯繫方式

電子郵件:luzhenbo2@qq.com

我的博客: http://blog.sina.com.cn/luzhenbo2

相關文章
相關標籤/搜索