Highlight peaks/dents in a heatmap imagesc()

Hi,
I am creating an image based on laser distance data (z) that I am collecting using imagesc(x,y,z). This gives me a heatmap/surface map of my object.
How can I automatically highlight the peaks/dents in this map or areas where z is higher/lower than a user defined threshold? I would like to create a circle around that area that has values higher or lower than a threshold or highlight those areas in any other way.
Thanks!

2 comentarios

yanqi liu
yanqi liu el 17 de En. de 2022
yes,sir,may be upload your data to do some analysis
Konvictus177
Konvictus177 el 17 de En. de 2022
Editada: Konvictus177 el 17 de En. de 2022
Imagine you have the following data set:
Z = peaks;
x = linspace(0,2*pi);
y = linspace(0,2*pi);
figure
imagesc(x,y,Z)
Now I want the user to set a threshold value for Z interactively and create contour lines for everything either lower or higher than what the user has set as a threshold.
Thanks.

Iniciar sesión para comentar.

 Respuesta aceptada

Mathieu NOE
Mathieu NOE el 18 de En. de 2022
hello
a very simple code that combines surf and contour (will not draw a circle but maybe it's still a valuable solution)
[X,Y,Z] = peaks(100);
figure(1),hold on
s = surf(X,Y,Z,'Facealpha',0.75,'Edgecolor','none');
view(2);
colorbar('vert');
threshold = 0.3*max(Z,[],'all');
[c,h] = contour(X,Y,Z,[threshold threshold],'-k','Linewidth',5);
hold off

5 comentarios

Image Analyst
Image Analyst el 18 de En. de 2022
Just to clarify, this code draws contours at a fixed height regardless of how high the peak is. So each contour is at the same level.
My code draws the contours relative to a specified amount below the peak value. So it's relative and thus each contour is at a different absolute level.
It's not clear (to me anyway) from the original post which method was wanted.
Mathieu NOE
Mathieu NOE el 18 de En. de 2022
hello
sure , I had no big pretention here ; maybe because I'm (half) lazy , I interpreted the requirement as "not to much" sophisticated
I would like to create a circle around that area that has values higher or lower than a threshold or highlight those areas in any other way.
Konvictus177
Konvictus177 el 19 de En. de 2022
Editada: Konvictus177 el 19 de En. de 2022
Thanks both to you for your input! Both solutions are definitely legitmate since I was not very clear in my question. In fact, its totally fine for all "cirles" or contours drawn to be on the same level.
There are 3 more things I would like to add:
  1. How can I make this more user interactive? I want a simple interface where the user sets the Z threshold value, clicks on a button e.g. "Ok" and it then creates these contours around those areas that are either lower or higher than the set Z threshold. The user can then reset, set another value and the contours (circles) will be drawn again for another input. Actually I am interested in lower peaks, not the high peaks.
  2. I want to create a list for the X and Y center mass points (maybe also get the corresponding Z value) for those contours drawn (depending on the Z threshold value that was set by the user).
  3. This goes back to item 1.) additionally to the Z threshold value I would like to have a setting for width and length. A contour should be drawn for all blobs that fulfill the settings made by the user for Z, width (x) and length (y). This is more a nice to have item.
Any further support would be greatly appreciated!!
Image Analyst
Image Analyst el 19 de En. de 2022
Use App Designer to make a GUI. I'm sure there is a tutorial on that somewhere.
Konvictus177
Konvictus177 el 22 de En. de 2022
Yes, will do. Thanks!

Iniciar sesión para comentar.

Más respuestas (1)

Use imregionalmax():
Z = mat2gray(peaks);
x = linspace(0,2*pi);
y = linspace(0,2*pi);
subplot(2, 2, 1);
imagesc(x,y,Z)
axis('on', 'image')
% get regional max:
peaks = imregionalmax(Z);
subplot(2, 2, 2);
imshow(peaks, []);
% Overlay it on the original
rgbImage = imoverlay(Z, peaks, 'r');
subplot(2, 2, 3);
imshow(rgbImage);
Now that you have the locations, you can get the values at those locations and set up thresholded images, one for each peak. Then use bwboundaries() to get all the contours and use inpolygon to figure out which contours contain each point. Not hard at all, so I imagine you can do that part yourself, right? Here's my version:
% Initialization steps.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
Z = mat2gray(peaks);
x = linspace(0,2*pi);
y = linspace(0,2*pi);
subplot(2, 2, 1);
imagesc(x,y,Z)
impixelinfo;
axis('on', 'image')
% get regional max:
peaks = imregionalmax(Z);
% Some may have flat tops, so get down to a single point on each peak.
peaks = bwulterode(peaks);
subplot(2, 2, 2);
imshow(peaks, []);
% Overlay it on the original
rgbImage = imoverlay(Z, peaks, 'r');
subplot(2, 2, 3);
imshow(rgbImage);
impixelinfo;
% Get values of the peaks.
values = unique(Z(peaks))
% Get locations of the peaks.
[yp, xp] = find(peaks)
% Get contours
deltaz = 0.05;
subplot(2, 2, 4);
imshow(peaks, []);
hold on;
for k = 1 : length(values)
mask = Z > (values(k) - deltaz);
% imshow(mask);
boundaries = bwboundaries(mask);
thisx = xp(k);
thisy = yp(k);
plot(thisx, thisy, 'r+', 'MarkerSize', 20)
if ~isempty(boundaries)
for k2 = 1 : length(boundaries)
thisBoundary = boundaries{k2};
x = thisBoundary(:, 2);
y = thisBoundary(:, 1);
pointIsInside = inpolygon(thisx, thisy, x, y);
if pointIsInside
plot(x, y, '-', 'LineWidth', 2);
end
end
end
end

2 comentarios

yanqi liu
yanqi liu el 18 de En. de 2022
yes,sir,it is great idea,use imregionalmax to get region max and clear boarder,may be use to watermark segment
Oliver Tolfts
Oliver Tolfts el 2 de Mzo. de 2023
@Image Analyst, >20% of the graphs of my Ph.D were plotted using your help. Thank you for sharing all your knowledge of Matlab.
thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you

Iniciar sesión para comentar.

Categorías

Más información sobre Data Distribution Plots en Centro de ayuda y File Exchange.

Preguntada:

el 15 de En. de 2022

Comentada:

el 2 de Mzo. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by