clear all; close all; clc; img=imread('9.pgm'); [m n]=size(img); imgn=zeros(m,n); for i=2:m-1 for j=2:n-2 pow=0; for p=i-1:i+1 for q =j-1:j+1 if img(p,q) > img(i,j) if p~=i || q~=j imgn(i,j)=imgn(i,j)+2^pow; pow=pow+1; end end end end end end figure; imshow(imgn,[]); hist=cell(1,4); %划分四个区域求直方图,10*10的太多了,这里搞简单点 hist{1}=imhist(img(1:floor(m/2),1:floor(n/2))); hist{2}=imhist(img(1:floor(m/2),floor(n/2)+1:n)); hist{3}=imhist(img(floor(m/2)+1:m,1:floor(n/2))); hist{4}=imhist(img(floor(m/2)+1:m,floor(n/2)+1:n)); for i=1:4 figure; plot(hist{i}); end添加一段程序将直方图显示横纵坐标,分别为LBP值和次数
时间: 2023-11-12 11:08:25 浏览: 86
可以在每个直方图画图之前添加以下代码来显示横纵坐标:
```matlab
x = 0:255;
figure;
plot(x, hist{i});
xlabel('LBP值');
ylabel('次数');
```
完整代码如下:
```matlab
clear all; close all; clc;
img=imread('9.pgm');
[m n]=size(img);
imgn=zeros(m,n);
for i=2:m-1
for j=2:n-2
pow=0;
for p=i-1:i+1
for q =j-1:j+1
if img(p,q) > img(i,j)
if p~=i || q~=j
imgn(i,j)=imgn(i,j)+2^pow;
pow=pow+1;
end
end
end
end
end
end
figure; imshow(imgn,[]);
hist=cell(1,4); %划分四个区域求直方图,10*10的太多了,这里搞简单点
hist{1}=imhist(img(1:floor(m/2),1:floor(n/2)));
hist{2}=imhist(img(1:floor(m/2),floor(n/2)+1:n));
hist{3}=imhist(img(floor(m/2)+1:m,1:floor(n/2)));
hist{4}=imhist(img(floor(m/2)+1:m,floor(n/2)+1:n));
for i=1:4
x = 0:255;
figure;
plot(x, hist{i});
xlabel('LBP值');
ylabel('次数');
end
```
注意,这里的横坐标是LBP值,而不是像素值。LBP算法将每个像素的8邻域编码成一个二进制数,因此LBP值的范围是0到255。
阅读全文