Question

I know how to check an 8-neighbourhood in matlab (i.e; nlfilter). But, I want to assign the value which is more repeated to the center value. So, say for instance that I have the following values in the 8-neighbourhood:

2-values = 56

3-values = 64

1-value = 70

1-value = 87

1-value = 65

In this case we would assign 64 to the center pixel.

How can we do that?

Thanks.

Was it helpful?

Solution

I think you want either the mode or the histc function.

M=mode(X) for vector X computes M as the sample mode, or most frequently occurring value in X.

Example with your data:

x = [56 56 64 64 64 70 87 65];
mode(x)

ans =

64

But this will only get you the most frequently occurring value.

If you want the count of each unique item in the array, you could do,

unqx = unique(x);

unqx =

56    64    65    70    87

valueCount = histc(x, unqx)

ans =

 2     3     1     1     1

You could then sort this and take the first N values

valueCount = sort(valueCount, 'descend');
% Use unqx(valueCount(1:N))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top