let's say i have the image:
img =
[1 0 -1;
0 -1 0;
1 1 -1];
and I'd like to get the values on the 8-connected area around the center:
vals = [0 -1 0 1 0 1 1 -1];
and then get the most common and least common values, and if there are two, to pick the lowest value.
I can only thing to do:
meh = im2col(img,[3 3],'distinct');
meh(5,:) = [];
answer = mode(meh);
but i can't figure out a way to get the least common thing unless i do ~mode a bunch of times...
Any ideas? Thanks :)

 Akzeptierte Antwort

Image Analyst
Image Analyst am 29 Mai 2012

0 Stimmen

You could use blockproc and have the blockproc function call hist. But you know that there could be up to 8 values that are the least common. What if your 8 neighbors are 8 unique values? What do you want to do then?

5 Kommentare

Stephen
Stephen am 29 Mai 2012
ah or histc if I knew the range. with unique values, this would act kinda like ordfilt2(img, 1 or 9,ones(3)); thanks for the hist idea :)
Image Analyst
Image Analyst am 29 Mai 2012
No, not at all. ordfilt2() is nothing like hist(). ordfilt2 is a rank order filter and sorts the array from lowest value to highest value. The least common value could be the lowest value, or it could be the highest value, or it could be any other value. ordfilt2 gives you no information about how common a value is. For that you need hist.
Teja Muppirala
Teja Muppirala am 29 Mai 2012
Also, check out NLFILTER. It's similar to BLOCKPROC, but for this specific problem I think the syntax might be a little simpler
Image Analyst
Image Analyst am 29 Mai 2012
Yes, you could do the same thing with nlfilter and it is easier to understand. Good point.
Stephen
Stephen am 4 Jun. 2012
yeah, but you asked if all values were unique, what would happen. It should order from lowest to highest and return the extremes. Thanks Teja, I'll check out nlfilter

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by