您的位置:首页 > 编程语言 > MATLAB

matlab Hog特征提取浅析

2016-12-03 19:14 603 查看
最近,利用自己的业余时间学习了Hog特征的提取方法,并编写了一个简单的matlab程序。限于自身的程序水平,有很多地方写的不是很好,请大家多多指正,我也会逐步完善代码,其实,这又何尝不是在完善我自己呢?

相关知识:http://blog.csdn.net/liulina603/article/details/8291093

close all;clear all;clc;

SrcImg = imread('E:\HOG\MFile\lena.jpg');

figure('Name','Hog');

subplot(2,2,1); imshow(SrcImg); title('SrcImg');

[H,W,D] = size(SrcImg);

if(D ~= 1)

    GrayImg = rgb2gray(SrcImg);

else

    GrayImg = SrcImg;

end

    subplot(2,2,2); imshow(GrayImg); title('GrayImg');

    

GrayImg = double(GrayImg);

Gx = zeros(W,H);

Gy = zeros(W,H);

for i = 2:H - 1

    for j = 2:W - 1

        Gx(i,j) = GrayImg(i,j + 1) - GrayImg(i,j - 1);

        Gy(i,j) = GrayImg(i + 1,j) - GrayImg(i - 1,j);

    end

end

Gxy = sqrt(Gx .^ 2 + Gy .^ 2);

subplot(2,2,3); imshow(Gxy); title('Gxy');

Axy = (atan(Gy ./ Gx) / pi) * 180;    

Axy(isnan(Axy)) = 0;

Axy(find(Axy < 0)) = Axy(find(Axy < 0)) + 180;

Axy = floor((Axy / 20) + 1);

subplot(2,2,4); imshow(Axy); title('Axy');

BinNum = 9;

CellStep = 4;

Hist = zeros(H / CellStep, W / CellStep, BinNum);

TempAxy = zeros(CellStep,CellStep);

TempGxy = zeros(CellStep,CellStep);

for i = 1:H / CellStep

    for j = 1:W / CellStep

        TempAxy = Axy((i - 1) * CellStep + 1:i * CellStep,(j - 1) * CellStep + 1:j * CellStep);

        TempGxy = Gxy((i - 1) * CellStep + 1:i * CellStep,(j - 1) * CellStep + 1:j * CellStep);

        for w = 1:CellStep

            for l = 1:CellStep

                Hist(i,j,TempAxy(w,l)) = Hist(i,j,TempAxy(w,l)) + TempGxy(w,l);

            end

        end

    end

end

% CellNumH and CellNumW is the number of cell in every row and column.

CellNumH = H / CellStep;

CellNumW = W / CellStep;

Overlap = 1;

BlockStep = 2;

BlockNumH = (CellNumH - Overlap) / (BlockStep - Overlap);

BlockNumW = (CellNumW - Overlap) / (BlockStep - Overlap);

Feature = zeros(BinNum,1);

Temp = zeros(BinNum,1);

for i = 1:BlockNumH

    for j = 1:BlockNumW

        TempFea = Hist((i - 1) * (BlockStep - Overlap) + 1:(i - 1) * (BlockStep - Overlap) + BlockStep,(j - 1) * (BlockStep - Overlap) + 1:(j - 1) * (BlockStep - Overlap) + BlockStep,:);

        for w = 1:BlockStep

            for l = 1:BlockStep

                if i == 1 && j == 1 && w == 1 && l == 1

                    Feature(:) = TempFea(w,l,:);

                else

                    Temp(:) = TempFea(w,l,:); 

                    Feature = [Feature;Temp];

                end

            end

        end

    end

end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息