Today's post shows how to use ismember to conveniently locate all of the highest pixel values in an image. For example, in the following Hough transform image, what are the 20 highest values, and where are all the pixels that have those values?
I got the idea from some example code written by Image Processing Toolbox writer Megan Mancuso. Megan says she got the idea to use ismember this way from Ahmed's answer on MATLAB Answers.
Here's where the Hough transform image above comes from.
A = imread("gantrycrane.png");
bw = edge(rgb2gray(A),"canny");
Now let's sort to figure out the highest 20 values of H.
sorted_H_values = sort(H(:),"descend");
highest_H_values = sorted_H_values(1:20)
Finally, I'll use ismember to compute a binary image showing all the elements of H that have one of those 20 highest values. (And I'll use xlim and ylim to zoom in so that we can see a few of those elements clearly.)
mask_20_highest_values = ismember(H,highest_H_values);
Any element-wise logical function, such as ismember, can be used in this way to produce a binary image. So add this to your bag of MATLAB tricks!