- bwboundaries: https://www.mathworks.com/help/images/ref/bwboundaries.html
- imread: https://www.mathworks.com/help/matlab/ref/imread.html
- imbinarize: https://www.mathworks.com/help/images/ref/imbinarize.html
- reducepoly: https://www.mathworks.com/help/images/ref/reducepoly.html
- createpde: https://www.mathworks.com/help/pde/ug/createpde.html
How to make PDE geometry from the BW mask?
11 views (last 30 days)
Show older comments
Hello, I'd like to analyzed my data with temporally changed boundary shape by adapting the FEM.
Here, I already got the BW mask for the boundary, but I still don't how to change it as a geometry structure for the FEM which can put into the "geometryFromEdges" function. My BW mask data have the all the coorrdinate of x and y position as a matrix. In this case, how can I change it to the geometry feature which can fit on the geometryFromEdges ?
0 Comments
Answers (1)
Abhishek Chakram
on 24 Apr 2024
Hi Hyuntae Jeong,
To adapt a BW mask representing the boundary of a domain into a geometry structure suitable for FEM analysis in MATLAB, especially for use with the “geometryFromEdges” function, you will need to convert the boundary represented by your BW mask into a set of edges or curves that describe the geometry. This process involves several steps:
1.Extract Boundary Coordinates: You can use the “bwboundaries” function to extract the boundaries from the binary mask.
BW = imread('path_to_your_mask_image.png'); % Load your BW mask
BW = imbinarize(BW); % Ensure it's binary in case it's not
boundaries = bwboundaries(BW, 'noholes');
This will give you a cell array boundaries where each cell contains the (x, y) coordinates of a boundary. If your mask has one main object, you can focus on the first cell.
2. Simplify Boundary: Simplify the extracted boundary to reduce the number of points while maintaining the shape. You can use “reducepoly” function for the same.
boundary = boundaries{1}; % Assuming one main boundary
simplifiedBoundary = reducepoly(boundary, tolerance); % Set 'tolerance' appropriately
3. Create Geometry Definition: Once you have the simplified boundary, you need to create a set of edges that can be used with “geometryFromEdges”. In MATLAB, this often involves defining the boundary using lines, circles, and other shapes programmatically. For a custom shape, you might need to interpolate the boundary points into a spline or a set of line segments that approximate the boundary well. Here is how you might approach it with line segments:
% Example: Creating a custom geometry function for a simple shape
function [sf, name] = customGeometry(simplifiedBoundary)
% Assuming simplifiedBoundary is an Nx2 matrix of (x,y) points
% Create line segments between consecutive points
str = [];
for i = 1:size(simplifiedBoundary, 1)-1
str = [str, 'L', '(' num2str(simplifiedBoundary(i,1)), ',', num2str(simplifiedBoundary(i,2)), ')', '-', ...
'(' num2str(simplifiedBoundary(i+1,1)), ',', num2str(simplifiedBoundary(i+1,2)), ')'];
if i < size(simplifiedBoundary, 1)-1
str = [str, '+'];
end
end
sf = str;
name = 'customBoundary';
end
Then, use your custom geometry in the PDE model:
model = createpde();
[sf, name] = customGeometry(simplifiedBoundary);
geometryFromEdges(model, @(x) eval(name));
You can refer to the following documentation to know more about functions used:
Best Regards,
Abhishek Chakram
See Also
Categories
Find more on Geometry and Mesh in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!