closed countour around a given point
Hi and I’d like to say thanks in advance.
I am processing some gridded data using contour/countourc commands and I am trying to do 3 things.
find all the closed countours that are centered and suround a given x,y cordinate
find the outermost closed countour from the above list
find the maximum, min and average distance of the above outermost contour from the center x,y cordinate (countours are not always circular and may be of irregular shape in my data).
I started with a sample data (from matlab called peaks) to build my code and impliment on my actual data but I am still stuck on 1 and 2.
(image attaced) I want my code to detect contour levels 18, 16, 14, 12 which are closed and surrounding my center point (blue dot) but instead it is picking 8, 10, 14, 16, 18. How can i modify the code to only detect the prefered contour levels (18,16,14,12) from which i can find the outermost contour and the 3 distances from the center. Note, level 18 is behind the center dot. Also attached countour z data… just incase
%___________________________________________________________________________
% Specify the center point and search radius
centerPoint = [25, 37.5];
searchRadius = 20;
% get and plot contour data
Z = peaks+10; % make colorbar positive. using built in sample data from matlab (peaks)
[M,h] = contour(Z);
clabel(M, h);
colorbar
% plot centerPoint
hold on
scatter(25, 37.5,"filled")
hold off
% crop portion of search area for easy visual
xlim([25 – searchRadius 25 + searchRadius])
ylim([37.5 – searchRadius 37.5 + searchRadius])
%___________________________________________________________________________
% Calculate contours
contourData = contourc(Z);
% Parse contour data
idx = 1;
while idx < size(contourData, 2)
contourLevel = contourData(1, idx); % Contour level
numPoints = contourData(2, idx); % Number of points in the contour
contourPoints = contourData(:, idx+1:idx+numPoints); % Contour points (x, y coordinates)
% Check if contour is closed (first and last points are the same)
isClosed = isequal(contourPoints(:,1), contourPoints(:,end));
% Check if contour surrounds the specified point
if isClosed
% Calculate distance of contour points to center point
distToCenter = sqrt((contourPoints(1, – centerPoint(1)).^2 + (contourPoints(2,
– centerPoint(2)).^2);
% Check if all points are within search radius
if all(distToCenter <= searchRadius)
disp([‘Contour Level: ‘, num2str(contourLevel)]);
disp([‘Contour is closed and surrounds point (‘, num2str(centerPoint(1)), ‘, ‘, num2str(centerPoint(2)), ‘)’]);
disp(‘Contour Points:’);
%disp(contourPoints);
disp(‘———————————————‘);
end
end
% Move to the next contour
idx = idx + numPoints + 1;
end
clear Z M h contourData contourLevel numPoints contourPoints …
centerPoint searchRadius contourLevels isClosed distToCenter idx;Hi and I’d like to say thanks in advance.
I am processing some gridded data using contour/countourc commands and I am trying to do 3 things.
find all the closed countours that are centered and suround a given x,y cordinate
find the outermost closed countour from the above list
find the maximum, min and average distance of the above outermost contour from the center x,y cordinate (countours are not always circular and may be of irregular shape in my data).
I started with a sample data (from matlab called peaks) to build my code and impliment on my actual data but I am still stuck on 1 and 2.
(image attaced) I want my code to detect contour levels 18, 16, 14, 12 which are closed and surrounding my center point (blue dot) but instead it is picking 8, 10, 14, 16, 18. How can i modify the code to only detect the prefered contour levels (18,16,14,12) from which i can find the outermost contour and the 3 distances from the center. Note, level 18 is behind the center dot. Also attached countour z data… just incase
%___________________________________________________________________________
% Specify the center point and search radius
centerPoint = [25, 37.5];
searchRadius = 20;
% get and plot contour data
Z = peaks+10; % make colorbar positive. using built in sample data from matlab (peaks)
[M,h] = contour(Z);
clabel(M, h);
colorbar
% plot centerPoint
hold on
scatter(25, 37.5,"filled")
hold off
% crop portion of search area for easy visual
xlim([25 – searchRadius 25 + searchRadius])
ylim([37.5 – searchRadius 37.5 + searchRadius])
%___________________________________________________________________________
% Calculate contours
contourData = contourc(Z);
% Parse contour data
idx = 1;
while idx < size(contourData, 2)
contourLevel = contourData(1, idx); % Contour level
numPoints = contourData(2, idx); % Number of points in the contour
contourPoints = contourData(:, idx+1:idx+numPoints); % Contour points (x, y coordinates)
% Check if contour is closed (first and last points are the same)
isClosed = isequal(contourPoints(:,1), contourPoints(:,end));
% Check if contour surrounds the specified point
if isClosed
% Calculate distance of contour points to center point
distToCenter = sqrt((contourPoints(1, – centerPoint(1)).^2 + (contourPoints(2,
– centerPoint(2)).^2);
% Check if all points are within search radius
if all(distToCenter <= searchRadius)
disp([‘Contour Level: ‘, num2str(contourLevel)]);
disp([‘Contour is closed and surrounds point (‘, num2str(centerPoint(1)), ‘, ‘, num2str(centerPoint(2)), ‘)’]);
disp(‘Contour Points:’);
%disp(contourPoints);
disp(‘———————————————‘);
end
end
% Move to the next contour
idx = idx + numPoints + 1;
end
clear Z M h contourData contourLevel numPoints contourPoints …
centerPoint searchRadius contourLevels isClosed distToCenter idx; Hi and I’d like to say thanks in advance.
I am processing some gridded data using contour/countourc commands and I am trying to do 3 things.
find all the closed countours that are centered and suround a given x,y cordinate
find the outermost closed countour from the above list
find the maximum, min and average distance of the above outermost contour from the center x,y cordinate (countours are not always circular and may be of irregular shape in my data).
I started with a sample data (from matlab called peaks) to build my code and impliment on my actual data but I am still stuck on 1 and 2.
(image attaced) I want my code to detect contour levels 18, 16, 14, 12 which are closed and surrounding my center point (blue dot) but instead it is picking 8, 10, 14, 16, 18. How can i modify the code to only detect the prefered contour levels (18,16,14,12) from which i can find the outermost contour and the 3 distances from the center. Note, level 18 is behind the center dot. Also attached countour z data… just incase
%___________________________________________________________________________
% Specify the center point and search radius
centerPoint = [25, 37.5];
searchRadius = 20;
% get and plot contour data
Z = peaks+10; % make colorbar positive. using built in sample data from matlab (peaks)
[M,h] = contour(Z);
clabel(M, h);
colorbar
% plot centerPoint
hold on
scatter(25, 37.5,"filled")
hold off
% crop portion of search area for easy visual
xlim([25 – searchRadius 25 + searchRadius])
ylim([37.5 – searchRadius 37.5 + searchRadius])
%___________________________________________________________________________
% Calculate contours
contourData = contourc(Z);
% Parse contour data
idx = 1;
while idx < size(contourData, 2)
contourLevel = contourData(1, idx); % Contour level
numPoints = contourData(2, idx); % Number of points in the contour
contourPoints = contourData(:, idx+1:idx+numPoints); % Contour points (x, y coordinates)
% Check if contour is closed (first and last points are the same)
isClosed = isequal(contourPoints(:,1), contourPoints(:,end));
% Check if contour surrounds the specified point
if isClosed
% Calculate distance of contour points to center point
distToCenter = sqrt((contourPoints(1, – centerPoint(1)).^2 + (contourPoints(2,
– centerPoint(2)).^2);
% Check if all points are within search radius
if all(distToCenter <= searchRadius)
disp([‘Contour Level: ‘, num2str(contourLevel)]);
disp([‘Contour is closed and surrounds point (‘, num2str(centerPoint(1)), ‘, ‘, num2str(centerPoint(2)), ‘)’]);
disp(‘Contour Points:’);
%disp(contourPoints);
disp(‘———————————————‘);
end
end
% Move to the next contour
idx = idx + numPoints + 1;
end
clear Z M h contourData contourLevel numPoints contourPoints …
centerPoint searchRadius contourLevels isClosed distToCenter idx; contour MATLAB Answers — New Questions