Today, I'm continuing my recent theme of thinking about peak-finding in images. When I wrote the first one (19-Aug-2021), I didn't realize it was going to turn into a series. This might be the last one—but no promises! My previous post (17-Sep-2021) was based on 1-D examples. Today's post focuses on an image example (in 2-D), and it connects to using regionprops to compute gray-weighted centroids of peaks. I'll also throw in some 3-D surface visualization, just for fun. Here is today's image: url = "https://blogs.mathworks.com/steve/files/snowflakes2.png";
I'd like to see if we can focus algorithmically on these white blobby things (that's a technical term that I learned in engineering school). First, let's try computing the regional maxima.
A_regmax = imregionalmax(A);
At first glance, that does not look like what I'm after. Let's find out what's going on here.
A_regmax_overlay = imoverlay(A,A_regmax,"green");
I want to look closely at one white blobby thing in particular.
imshow(A_regmax_overlay(30:47,263:285,:))
One, or maybe two, of these locations looks like the kind of peak that we are interested in. What about the others? Let me show you two different ways to explore further. The first is with a 3-D visualization, where we treat the image pixel values as heights of a gray surface. The code below shows that surface, and then it displays the detected regional maxima as blue-and-yellow dots just above the surface.
A_cropped = A(30:47,263:285);
A_regmax_cropped = A_regmax(30:47,263:285);
surf(A_cropped, EdgeColor = "none")
[y,x,~] = find(A_regmax_cropped);
z = A_cropped(A_regmax_cropped);
plot3(x,y,z+1,LineStyle="none",Marker="o",...
set(gca,YDir = "reverse")
A second way is to go back to 2-D, but using an extremely zoomed-in view, with the actual pixel values superimposed on the individual pixels. To accomplish, I used the Image Viewer and the Pixel Region Tool. Below is a screen shot of the Pixel Region Tool. I have annotated the screen shot with the locations of the regional maxima. With either technique, you can get the idea that several of these maxima locations are just small, uninteresting fluctuations.
As I discussed in the previous posts, you can use the h-maxima transform to filter out these small peaks.
B_regmax = imregionalmax(B);
imshow(B_regmax(30:47,263:285))
B_regmax_overlay = imoverlay(B,B_regmax,"green");
I want to finish up with a connection to the regionprops function. This function is used most often with a binary image input, and it computes various geometries properties of objects (connected components) in the binary image. But you can also provide a gray-scale image at the same time. With that additional info, you can compute other properties. Here, I am interested in the weighted centroids of the various detected peaks. The original image pixel values will be used as the weights. Here's how to do it: props = regionprops("table",B_regmax,A,"WeightedCentroid")
props = 99×1 table
| WeightedCentroid |
---|
1 | 1.6702 | 24.6139 |
---|
2 | 2.4948 | 43.3596 |
---|
3 | 1.4239 | 97.7269 |
---|
4 | 14.5813 | 52.3127 |
---|
5 | 20.5567 | 100.2771 |
---|
6 | 28.5663 | 89.2373 |
---|
7 | 29.9773 | 18.0773 |
---|
8 | 31.1731 | 50.3474 |
---|
9 | 41.2062 | 2.7718 |
---|
10 | 45.1645 | 39.6400 |
---|
11 | 44.1222 | 25.3352 |
---|
12 | 43.9098 | 67.8786 |
---|
13 | 44.8320 | 103.4289 |
---|
14 | 50.9091 | 16.5648 |
---|
⋮ |
---|
And here's one way to visualize the results:
plot(props.WeightedCentroid(:,1),props.WeightedCentroid(:,2),...
LineStyle="none", Marker="o", MarkerEdgeColor="y",...
title("Gray-scale weighted centroids")
Comments
To leave a comment, please click here to sign in to your MathWorks Account or create a new one.