Matlab中的本地二进制模式原始代码和参考 [英] Local Binary Patterns original code and references in matlab

查看:104
本文介绍了Matlab中的本地二进制模式原始代码和参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用matlab找到许多本地二进制模式的实现,对此我有些困惑.

I'm founding lots of implementations of Local Binary Patterns with matlab and i am a little confusing about them.

Wikipedia解释了基本LBP 的工作原理:

Wikipedia explains how the basic LBP works:

1- Divide the examined window into cells (e.g. 16x16 pixels for each cell).
2- For each pixel in a cell, compare the pixel to each of its 8 neighbors (on its left-top, left-middle, left-bottom, right-top, etc.). Follow the pixels along a circle, i.e. clockwise or counter-clockwise.
3- Where the center pixel's value is greater than the neighbor's value, write "1". Otherwise, write "0". This gives an 8-digit binary number (which is usually converted to decimal for convenience).
4- Compute the histogram, over the cell, of the frequency of each "number" occurring (i.e., each combination of which pixels are smaller and which are greater than the center).
5- Optionally normalize the histogram.
6- Concatenate (normalized) histograms of all cells. This gives the feature vector for the window.

看一下这个算法,我可以得出结论,每个LBP特征向量将具有num_cels * 256维,其中num_cels是图像的16x16像素单元数.每个像元都有256个可能的值(0到255),因此特征向量的大小可能相差很大.

looking at this algorithm I can conclude that each LBP feature vector will have num_cels*256 dimensions, where num_cels is the number of 16x16 pixels cells of images. Each cell will have 256 possible values (0 to 255) and so the feature vector size can vary a lot.

但是,查看一些LBP实现, VLFEAT_LBP 返回一个矩阵,而不是特征向量.在此实现中,LBP作为256个特征向量返回,我认为(不确定)是所有单元格的所有直方图的总和.我想知道的是:这是经典的LBP解释和matlab源代码.

But, looking at some LBP implementations, the VLFEAT_LBP returns a matrix instead of a feature vector. In this implementation LBP is returned as a 256 feature vector which I think (not sure) is the sum of all histograms of all cells. All I want to know is: which is the classic LBP explanation and matlab source code.

推荐答案

% clc;    % Clear the command window.
% close all;  % Close all figures (except those of imtool.)
% imtool close all;  % Close all imtool figures.
% clear;  % Erase all existing variables.
% workspace;  % Make sure the workspace panel is showing.
% fontSize = 20;
% % Read in a standard MATLAB gray scale demo image.
% folder = fullfile(matlabroot, '\toolbox\images\imdemos');
% baseFileName = 'cameraman.tif';
% % Get the full filename, with path prepended.
% fullFileName = fullfile(folder, baseFileName);
% if ~exist(fullFileName, 'file')
%   % Didn't find it there.  Check the search path for it.
%   fullFileName = baseFileName; % No path this time.
%   if ~exist(fullFileName, 'file')
%       % Still didn't find it.  Alert user.
%       errorMessage = sprintf('Error: %s does not exist.', fullFileName);
%       uiwait(warndlg(errorMessage));
%       return;
%   end
% end
grayImage = imread('fig.jpg');
% Get the dimensions of the image.  numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);

% Display the original gray scale image.
subplot(2, 2, 1);
imshow(grayImage, []);
%title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Position', get(0,'Screensize')); 
set(gcf,'name','Image Analysis Demo','numbertitle','off') 
% Let's compute and display the histogram.
[pixelCount grayLevels] = imhist(grayImage);
subplot(2, 2, 2); 
bar(pixelCount);
%title('Histogram of original image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
% Preallocate/instantiate array for the local binary pattern.
localBinaryPatternImage = zeros(size(grayImage));
for row = 2 : rows - 1   
    for col = 2 : columns - 1    
        centerPixel = grayImage(row, col);
        pixel7=grayImage(row-1, col-1) > centerPixel;  
        pixel6=grayImage(row-1, col) > centerPixel;   
        pixel5=grayImage(row-1, col+1) > centerPixel;  
        pixel4=grayImage(row, col+1) > centerPixel;     
        pixel3=grayImage(row+1, col+1) > centerPixel;    
        pixel2=grayImage(row+1, col) > centerPixel;      
        pixel1=grayImage(row+1, col-1) > centerPixel;     
        pixel0=grayImage(row, col-1) > centerPixel;       
        localBinaryPatternImage(row, col) = uint8(...
            pixel7 * 2^7 + pixel6 * 2^6 + ...
            pixel5 * 2^5 + pixel4 * 2^4 + ...
            pixel3 * 2^3 + pixel2 * 2^2 + ...
            pixel1 * 2 + pixel0);
    end  
end 
subplot(2,2,3);
imshow(localBinaryPatternImage, []);
%title('Local Binary Pattern', 'FontSize', fontSize);
subplot(2,2,4);
[pixelCounts, GLs] = imhist(uint8(localBinaryPatternImage));
bar(GLs, pixelCounts);
%title('Histogram of Local Binary Pattern', 'FontSize', fontSize);

这篇关于Matlab中的本地二进制模式原始代码和参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆