Counting the neighbors in matrix

Hi everyone,
I have matrix R in which each element is integer of range [0 7]. I want to calculate the count of neighbor, let say for pixel value 0 how many times it has the neighbor 1, 2, ..., 7. every pixel has eight neighbors, for example
[n1 n2 n3
n4 x n5
n6 n7 n8]
where pixel x have eight neighbors. As a resultant I think we will have an other matrix of 8 x 8, or we can say 8 histograms as we have eight possible values in given matrix R.
I hope I have explained what I want :-(

9 commentaires

Image Analyst
Image Analyst le 11 Août 2012
Why do you want to do this? It seems like the gray level co-occurrence matrix (done by graycomatrix() in the Image Processing Toolbox), but not quite. What is the use case? Why do you want the histogram of neighbors, but not including the central pixel? What is the context - the big picture?
Aravin
Aravin le 11 Août 2012
Actually, in my application, each value in R is some computed value, whose range is between 0-7. I just want to see the corelation of neighbors of these computed values.
Image Analyst
Image Analyst le 11 Août 2012
Then is there any reason why you're not using the gray level cooccurrence matrix, which is usually what people use?
Aravin
Aravin le 12 Août 2012
Thanks a lot, gray level cooccurrence matrix really a one line solution. Setting offset, I can get the desired results.
Before, I want not using this as I didn't know about this function and Matrix. :-) Thanks.
Matt Fig
Matt Fig le 12 Août 2012
Modifié(e) : Matt Fig le 12 Août 2012
What command did you use to get the results in one line? Usually I can figure out how things work by reading the documentation, but I have no idea how GRAYCOMATRIX produces the result. Please show the code you used. Thanks!
Matt Fig
Matt Fig le 12 Août 2012
Nevermind, my copy of GRAYCOMATRIX has a severe bug in it anyway. It doesn't even reproduce the output shown in the doc, so I doubt it would give the desired result here. Bummer!
I will search the FEX and elsewhere for a more reliable implementation.
Granted, their graycomatrix is very confusing and has some weird defaults, and you have to do things to get it to do what you'd intuitively want it to do. For example, it only looks at pairings to the right of the pixel, not all around at all 8 neighbors (unless you pass in the correct "offset" parameter). Matt, try this code and see if you get the tables they show in their explanatory diagram:
m = [1 1 5 6 8; 2 3 5 7 1; 4 5 7 1 2; 8 5 1 2 5]
glcm = graycomatrix(m, 'GrayLimits', [min(m(:)) max(m(:))])
Aravin
Aravin le 13 Août 2012
Hi Matt,
Here is the one line solution I did for my problem.
final_H= graycomatrix(x, 'offset', [0 1; -1 1; -1 0; -1 -1; 0 -1; 1 -1;1 0;1 1],'NumLevels',15);
I will have 8 number of channels, which I have sum for final result.
Matt Fig
Matt Fig le 13 Août 2012
Modifié(e) : Matt Fig le 13 Août 2012
Thanks, IA. Indeed you are correct. The illustration I was looking at in the doc seemed to imply it was showing graycomatrix(I).
I see now the difference between what this function gives and what I (and others) showed below. If we have:
x = randi([0 7],1000,1000);
Then to get the same result as:
G = sum(graycomatrix(x,...
'graylimits',[0 7],...
'Offset',[0 1; 0 -1;-1 1; -1 0; -1 -1;1 -1;1 0;1 1]),3)
I would modify my code to:
R = ones(3);
R(5) = 0;
for ii = 7:-1:0
I = conv2(single(x==ii),R,'same');
for jj = 0:7
M(ii+1,jj+1) = sum(I(x(:)==jj));
end
end
Thus M and G are equal. M took half the time to compute, but the code is definitely longer! Note that the calculation of M could perhaps be made more efficient by storing the x(:)==jj in a cell outside the loop.
Anyway, I'm glad Aravin got what was required.

Connectez-vous pour commenter.

 Réponse acceptée

Matt Fig
Matt Fig le 8 Août 2012
Modifié(e) : Matt Fig le 8 Août 2012

1 vote

If you don't have the image processing toolbox, or if you want your code to be useful to those who don't, this is just as fast. Note that the example only looks for the neighbors of the number 7. You can put this into a loop as needed. I assume your matrix is named x.
H = single(x==7);
H = logical(conv2(H,ones(3),'same'))~=H;
H = x(H(:));
Y = unique(H);
H = histc(H,Y);
H = [Y H];

Plus de réponses (3)

Sean de Wolski
Sean de Wolski le 7 Août 2012

1 vote

I would take an image processing approach:
x = zeros(5); %sample matrix
x(3:5,3:5) = [1 2 1;1 7 2;3 4 1];
x(5) = 7;
BW = x==7; %We'll count neighbors of 7s
M = xor(imdilate(BW,ones(3)),BW); %neighbors of 7s
uv = unique(x(M)); %unique neighbors
n = histc(x(M),uv); %count unique neighbors
[uv n] %display number of unique values to occurences

2 commentaires

Matt Fig
Matt Fig le 8 Août 2012
Nice!
Aravin
Aravin le 11 Août 2012
Thanks Sean... Really nice solution.

Connectez-vous pour commenter.

Andrei Bobrov
Andrei Bobrov le 7 Août 2012
Modifié(e) : Andrei Bobrov le 8 Août 2012

1 vote

A = randi([0 7],10);
A1 = nan(size(A)+2);
A1(2:end-1,2:end-1) = A;
k = (0:7)';
n = numel(k);
out = zeros(n);
d = true(3);
d(5) = false;
for i1 = 1:n
[ii,jj] = find(A1 == k(i1));
p = zeros(n,1);
for i2 = 1:numel(ii)
q = A1(ii(i2) + (-1:1),jj(i2) + (-1:1));
p = p + histc(q(d&(~isnan(q))),k);
end
out(:,i1) = p;
end
variant based on the idea of Sean and Matt:
k = (0:7)';
out = zeros(numel(k));
for a = 1:numel(k)
out(:,a) = histc(x(bwdist(x == k(a),'chessboard') == 1),k);
end

3 commentaires

Aravin
Aravin le 11 Août 2012
Thanks Andrei Bobrov :-)
Aravin
Aravin le 11 Août 2012
Indeed, this is the perfect solution to my problem. Matts and sean Solution doesn't include the count of searching key value. In their given examples, if we are seaching 7, then their solution doesn't count the 7 as neighbor, but your solution does.
Thanks Andrei.
Sean de Wolski
Sean de Wolski le 13 Août 2012
Our solutions could count the 7 as a nieghbor! Just remove the xor() from mine or the ~= from Matt's. We were intentionally not counting the 7, but if you want to count it it makes our solutions even simpler :)

Connectez-vous pour commenter.

Teja Muppirala
Teja Muppirala le 8 Août 2012

1 vote

R = randi([0 7],100); % Input
H = zeros(8); % The 8x8 Matrix
for k = 0:7
C = conv2(double(R == k),[1 1 1; 1 0 1; 1 1 1],'same');
H(:,k+1) = accumarray(1+R(C~=0),nonzeros(C),[8 1]);
end
bar(H);

Catégories

En savoir plus sur Data Distribution Plots dans Centre d'aide et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by