原始LBP特征计算-MATLAB
原始的LBP算子定义在像素3*3的邻域内,以邻域中心像素为阈值,相邻的8个像素的灰度值与邻域中心的像素值进行比较,若周围像素大于中心像素值,则该像素点的位置被标记为1,否则为0。这样,3*3邻域内的8个点经过比较可产生8位二进制数,将这8位二进制数依次排列形成一个二进制数字,这个二进制数字就是中心像素的LBP值,LBP值共有28种可能,因此LBP值有256种。中心像素的LBP值反映了该像素周围区域的纹理信息。
备注:计算LBP特征的图像必须是灰度图,如果是彩色图,需要先转换成灰度图。
matlab源码
<matlab>
function OriginLBP(img)
imgSize = size(img);
if numel(imgSize) > 2
imgG = rgb2gray(img);
else
imgG = img;
end
[rows, cols] = size(imgG);
rows=int16(rows);
cols=int16(cols);
imglbp = uint8(zeros(rows-2, cols-2));
for i=2:rows-2
for j=2:cols-2
center = imgG(i,j);
lbpCode = 0;
lbpCode = bitor(lbpCode, (bitshift(compareCenter(i-1, j-1, center, imgG), 7)));
lbpCode = bitor(lbpCode, (bitshift(compareCenter(i-1,j, center, imgG), 6)));
lbpCode = bitor(lbpCode, (bitshift(compareCenter(i-1,j+1, center, imgG), 5)));
lbpCode = bitor(lbpCode, (bitshift(compareCenter(i,j+1, center, imgG), 4)));
lbpCode = bitor(lbpCode, (bitshift(compareCenter(i+1,j+1, center, imgG), 3)));
lbpCode = bitor(lbpCode, (bitshift(compareCenter(i+1,j, center, imgG), 2)));
lbpCode = bitor(lbpCode, (bitshift(compareCenter(i+1,j-1, center, imgG), 1)));
lbpCode = bitor(lbpCode, (bitshift(compareCenter(i, j-1, center, imgG), 0)));
imglbp(i-1,j-1) = lbpCode;
end
end
imshow(imglbp);
end
function flag = compareCenter(x, y, center, imgG)
if imgG(x, y) > center
flag = 1;
else
flag = 0;
end
end
</matlab>