Problem to Threshold a Matrix

I need to threshold the surrounding pixels of the given matrix with respect to the centre pixel of the given matrix. If the surrounding values are greater than or equal to the center of the pixel they are given a 1 otherwise they are given a 0. Then I need to store all the values in the shown order to result in a vector which contains the binary value.

2 Comments

Have you tried coding this? What problems are you having? Not working, or too slow, or ???
matrix = [ 85 99 21; 54 54 86; 57 12 13];
%matrix(2,2) is the centre pixel
thres_mat = matrix > matrix(2,2); % which results in the binary matrix shown
my problem is to store those binary values in that order

Sign in to comment.

 Accepted Answer

Image Analyst
Image Analyst on 12 Apr 2017
Let's call it what it is, okay? You're asking for the " local binary pattern".
For a FULL demo on the whole image, see the attached m-file. it creates this image

6 Comments

Simply amazing that's what I am working on the achieve :) thank you!! definitely the best answer.
Hi! I have used what you gave me to apply it to cells in a cell array where the cell array is an original image split into various cells 16x16 each. Now my problem is that the line
localBinaryPatternImage(rows, cols) = eightBitNumber;
which is found in the for loop only gives the values of the last 15x15 unit8 cell array. How can i concatenate all of the cell arrays to form the lbp image?
You either need to extract the 16x16 image from the cell and do it on the small arrays, or just don't split the image up into cells. Not sure why you're doing that anyway.
I am doing it because I want to plot the histogram and lbp features for every cell then concatenate them rather than have 1 histogram and 1 lbp feature vector for the entire image.
%% Grayscale Baboon Image I2 = imread('baboon.png'); [rows,columns,dim] = size (I2); sz2 = [rows, columns];
chunk_size2 = [16 16]; % your desired size of the chunks image is broken into sc2 = sz2 ./ chunk_size2; % number of chunks in each dimension; must be integer % split to chunk_size(1) by chunk_size(2) chunks X2 = mat2cell(I2, chunk_size2(1) * ones(sc2(1),1), chunk_size2(2) *ones(sc2(2),1));
[r, c] = size(X2); z = cell2mat(X2(1));
%Extracting LBP features for each cell and concatinating them into a %histogram
% localBinaryPatternImage = zeros(size(I2), 'uint8'); %for celliter3 = 1:numel(X2)
result = []; for row = 1 : r for col = 1 : c
Z = cell2mat(X2(row, col)); [row_cell, col_cell] = size(Z);
for rows = 2 : row_cell - 1
for cols = 2 : col_cell - 1
centerPixel = Z(rows,cols);
pixel7= Z(rows-1, cols-1) > centerPixel;
pixel6= Z(rows-1, cols) > centerPixel;
pixel5= Z(rows-1, cols+1) > centerPixel;
pixel4= Z(rows, cols+1) > centerPixel;
pixel3= Z(rows+1, cols+1) > centerPixel;
pixel2= Z(rows+1, cols) > centerPixel;
pixel1= Z(rows+1, cols-1) > centerPixel;
pixel0= Z(rows, cols-1) > centerPixel;
eightBitNumber = uint8(...
pixel7 * 2^7 + pixel6 * 2^6 + ...
pixel5 * 2^5 + pixel4 * 2^4 + ...
pixel3 * 2^3 + pixel2 * 2^2 + ...
pixel1 * 2 + pixel0);
% Or you can use the built-in function bwpack(), which is somewhat simpler but a lot slower.
% eightBitNumber = uint8(bwpack([pixel0; pixel1; pixel2; pixel3; pixel4; pixel5; pixel6; pixel7]));
localBinaryPatternImage(rows, cols) = eightBitNumber;
end
end
end
end
I don't understand why you want to do that. And anyway, you don't have one LBP feature for the entire image. Every pixel has its own local binary pattern, so you have millions of patterns.

Sign in to comment.

More Answers (1)

James Tursa
James Tursa on 12 Apr 2017
Edited: James Tursa on 12 Apr 2017
Using your small example:
>> x = 2;
>> y = 2;
>> matrix = [ 85 99 21; 54 54 86; 57 12 13]
matrix =
85 99 21
54 54 86
57 12 13
>> t = matrix >= matrix(y,x)
t =
1 1 0
1 1 1
1 0 0
>> b = [t(y,x-1) t(y+1,x-1:x+1) t(y,x+1) t(y-1,x+1:-1:x-1)]
b =
1 1 0 0 1 0 1 1
>> d = sum(b.*2.^(7:-1:0))
d =
203

Categories

Find more on Graph and Network Algorithms in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!