百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 热门文章 > 正文

LBP matlab代码

bigegpt 2024-09-24 07:14 3 浏览

  • 代码由解释下型语言编写,理解较为容易
  • 大多数都是为了增强API的稳定性进行的设计,无需过多研究
function result = lbp(varargin) % image,radius,neighbors,mapping,mode)
% Version 0.3.3
% Authors: Marko Heikkil?and Timo Ahonen
% Changelog
% Version 0.3.2: A bug fix to enable using mappings together with a
% predefined spoints array
% Version 0.3.1: Changed MAPPING input to be a struct containing the mapping
% table and the number of bins to make the function run faster with high number
% of sampling points. Lauge Sorensen is acknowledged for spotting this problem.
% Check number of input arguments.
error(nargchk(1,5,nargin));
image=varargin{1};
%差值用浮点型数值
d_image=double(image);
% 对于不同数目的参数进行分别讨论
% 给出对应的提示信息
if nargin==1
 spoints=[-1 -1; -1 0; -1 1; 0 -1; -0 1; 1 -1; 1 0; 1 1];
 neighbors=8;
 mapping=0;
 mode='h';
end
if (nargin == 2) && (length(varargin{2}) == 1)
 error('Input arguments');
end
if (nargin > 2) && (length(varargin{2}) == 1)
 radius=varargin{2};
 neighbors=varargin{3};
 
 spoints=zeros(neighbors,2);
 % Angle step.
 % 按照给定的neighbour进行平均采样
 a = 2*pi/neighbors;
 
 for i = 1:neighbors
 spoints(i,1) = -radius*sin((i-1)*a);
 spoints(i,2) = radius*cos((i-1)*a);
 end
 
 if(nargin >= 4)
 mapping=varargin{4};
 if(isstruct(mapping) && mapping.samples ~= neighbors)
 error('Incompatible mapping');
 end
 else
 mapping=0;
 end
 
 if(nargin >= 5)
 mode=varargin{5};
 else
 mode='h';
 end
end
if (nargin > 1) && (length(varargin{2}) > 1)
 spoints=varargin{2};
 neighbors=size(spoints,1);
 
 if(nargin >= 3)
 mapping=varargin{3};
 if(isstruct(mapping) && mapping.samples ~= neighbors)
 error('Incompatible mapping');
 end
 else
 mapping=0;
 end
 
 if(nargin >= 4)
 mode=varargin{4};
 else
 mode='h';
 end 
end
% Determine the dimensions of the input image.
[ysize xsize] = size(image);
miny=min(spoints(:,1));
maxy=max(spoints(:,1));
minx=min(spoints(:,2));
maxx=max(spoints(:,2));
% Block size, each LBP code is computed within a block of size bsizey*bsizex
bsizey=ceil(max(maxy,0))-floor(min(miny,0))+1;
bsizex=ceil(max(maxx,0))-floor(min(minx,0))+1;
% Coordinates of origin (0,0) in the block
origy=1-floor(min(miny,0));
origx=1-floor(min(minx,0));
% Minimum allowed size for the input image depends
% on the radius of the used LBP operator.
if(xsize < bsizex || ysize < bsizey)
 error('Too small input image. Should be at least (2*radius+1) x (2*radius+1)');
end
% Calculate dx and dy;
dx = xsize - bsizex;
dy = ysize - bsizey;
% Fill the center pixel matrix C.
C = image(origy:origy+dy,origx:origx+dx);
d_C = double(C);
bins = 2^neighbors;
% Initialize the result matrix with zeros.
result=zeros(dy+1,dx+1);
%Compute the LBP code image
for i = 1:neighbors
 y = spoints(i,1)+origy;
 x = spoints(i,2)+origx;
 % Calculate floors, ceils and rounds for the x and y.
 fy = floor(y); cy = ceil(y); ry = round(y);
 fx = floor(x); cx = ceil(x); rx = round(x);
 % Check if interpolation is needed.
 % 取整产生的误差小于阈值就不插值
 if (abs(x - rx) < 1e-6) && (abs(y - ry) < 1e-6)
 % Interpolation is not needed, use original datatypes
 N = image(ry:ry+dy,rx:rx+dx);
 D = N >= C; 
 else
 % Interpolation needed, use double type images 
 % 开始差值操作
 ty = y - fy;
 tx = x - fx;
 % Calculate the interpolation weights.
 w1 = roundn((1 - tx) * (1 - ty),-6);
 w2 = roundn(tx * (1 - ty),-6);
 w3 = roundn((1 - tx) * ty,-6) ;
 % w4 = roundn(tx * ty,-6) ;
 w4 = roundn(1 - w1 - w2 - w3, -6);
 
 % Compute interpolated pixel values
 N = w1*d_image(fy:fy+dy,fx:fx+dx) + w2*d_image(fy:fy+dy,cx:cx+dx) + ...
w3*d_image(cy:cy+dy,fx:fx+dx) + w4*d_image(cy:cy+dy,cx:cx+dx);
 N = roundn(N,-4);
 D = N >= d_C; 
 end 
 % Update the result matrix.
 v = 2^(i-1);
 result = result + v*D;
end
%Apply mapping if it is defined
if isstruct(mapping)
 bins = mapping.num;
 for i = 1:size(result,1)
 for j = 1:size(result,2)
 result(i,j) = mapping.table(result(i,j)+1);
 end
 end
end
if (strcmp(mode,'h') || strcmp(mode,'hist') || strcmp(mode,'nh'))
 % Return with LBP histogram if mode equals 'hist'.
 result=hist(result(:),0:(bins-1));
 if (strcmp(mode,'nh'))
 result=result/sum(result);
 end
else
 %Otherwise return a matrix of unsigned integers
 if ((bins-1)<=intmax('uint8'))
 result=uint8(result);
 elseif ((bins-1)<=intmax('uint16'))
 result=uint16(result);
 else
 result=uint32(result);
 end
end
end
function x = roundn(x, n)
error(nargchk(2, 2, nargin, 'struct'))
validateattributes(x, {'single', 'double'}, {}, 'ROUNDN', 'X')
validateattributes(n, ...
 {'numeric'}, {'scalar', 'real', 'integer'}, 'ROUNDN', 'N')
if n < 0
 p = 10 ^ -n;
 x = round(p * x) / p;
elseif n > 0
 p = 10 ^ n;
 x = p * round(x / p);
else
 x = round(x);
end
end
  • 至此,关于LBP的讲解告一段落,接下来会进行selective search相关的介绍

相关推荐

C#.NET Autofac 详解(c# autoit)

简介Autofac是一个成熟的、功能丰富的.NET依赖注入(DI)容器。相比于内置容器,它额外提供:模块化注册、装饰器(Decorator)、拦截器(Interceptor)、强o的属性/方法注...

webapi 全流程(webapi怎么部署)

C#中的WebAPIMinimalApi没有控制器,普通api有控制器,MinimalApi是直达型,精简了很多中间代码,广泛适用于微服务架构MinimalApi一切都在组控制台应用程序类【Progr...

.NET外挂系列:3. 了解 harmony 中灵活的纯手工注入方式

一:背景1.讲故事上一篇我们讲到了注解特性,harmony在内部提供了20个HarmonyPatch重载方法尽可能的让大家满足业务开发,那时候我也说了,特性虽然简单粗暴,但只能解决95%...

C# 使用SemanticKernel调用本地大模型deepseek

一、先使用ollama部署好deepseek大模型。具体部署请看前面的头条使用ollama进行本地化部署deepseek大模型二、创建一个空的控制台dotnetnewconsole//添加依赖...

C#.NET 中间件详解(.net core中间件use和run)

简介中间件(Middleware)是ASP.NETCore的核心组件,用于处理HTTP请求和响应的管道机制。它是基于管道模型的轻量级、模块化设计,允许开发者在请求处理过程中插入自定义逻辑。...

IoC 自动注入:让依赖注册不再重复劳动

在ASP.NETCore中,IoC(控制反转)功能通过依赖注入(DI)实现。ASP.NETCore有一个内置的依赖注入容器,可以自动完成依赖注入。我们可以结合反射、特性或程序集扫描来实现自动...

C#.NET 依赖注入详解(c#依赖注入的三种方式)

简介在C#.NET中,依赖注入(DependencyInjection,简称DI)是一种设计模式,用于实现控制反转(InversionofControl,IoC),以降低代码耦合、提高可...

C#从零开始实现一个特性的自动注入功能

在现代软件开发中,依赖注入(DependencyInjection,DI)是实现松耦合、模块化和可测试代码的一个重要实践。C#提供了优秀的DI容器,如ASP.NETCore中自带的Micr...

C#.NET 仓储模式详解(c#仓库货物管理系统)

简介仓储模式(RepositoryPattern)是一种数据访问抽象模式,它在领域模型和数据访问层之间创建了一个隔离层,使得领域模型无需直接与数据访问逻辑交互。仓储模式的核心思想是将数据访问逻辑封装...

C#.NET 泛型详解(c# 泛型 滥用)

简介泛型(Generics)是指在类型或方法定义时使用类型参数,以实现类型安全、可重用和高性能的数据结构与算法为什么需要泛型类型安全防止“装箱/拆箱”带来的性能损耗,并在编译时检测类型错误。可重用同一...

数据分析-相关性分析(相关性 分析)

相关性分析是一种统计方法,用于衡量两个或多个变量之间的关系强度和方向。它通过计算相关系数来量化变量间的线性关系,从而帮助理解变量之间的相互影响。相关性分析常用于数据探索和假设检验,是数据分析和统计建模...

geom_smooth()函数-R语言ggplot2快速入门18

在每节,先运行以下这几行程序。library(ggplot2)library(ggpubr)library(ggtext)#用于个性化图表library(dplyr)#用于数据处理p...

规范申报易错要素解析(规范申报易错要素解析)

为什么要规范申报?规范申报是以满足海关监管、征税、统计等工作为目的,纳税义务人及其代理人依法向海关如实申报的行为,也是海关审接单环节依法监管的重要工作。企业申报的内容须符合《中华人民共和国海关进出口货...

「Eurora」海关编码归类 全球海关编码查询 关务服务

  海关编码是什么?  海关编码即HS编码,为编码协调制度的简称。  其全称为《商品名称及编码协调制度的国际公约》(InternationalConventionforHarmonizedCo...

9月1日起,河南省税务部门对豆制品加工业试行新政7类豆制品均适用投入产出法

全媒体记者杨晓川报道9月2日,记者从税务部门获悉,为减轻纳税人税收负担,完善农产品增值税进项税额抵扣机制,根据相关规定,结合我省实际情况,经广泛调查研究和征求意见,从9月1日起,我省税务部门对豆制品...