Problem to Threshold a Matrix

3 views (last 30 days)
CharlesB
CharlesB on 12 Apr 2017
Commented: Image Analyst on 21 Apr 2017
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
James Tursa
James Tursa on 12 Apr 2017
Have you tried coding this? What problems are you having? Not working, or too slow, or ???
CharlesB
CharlesB on 12 Apr 2017
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
CharlesB
CharlesB on 21 Apr 2017
%% 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
Image Analyst
Image Analyst on 21 Apr 2017
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!