# How can I convert the shape of one class to another using Matlab?

2 views (last 30 days)
Gobert on 14 Dec 2021
Commented: Gobert on 15 Dec 2021
Hi,
I have a situation that looks like this and I need your help. I want to convert/modify the "shape of the edge outline" between the gray and white area (SEO-GWA) to the "shape of the edge outline" between the white and black area (SEO-WBA).
Does any one has a suggestion or solution to this problem?

yanqi liu on 15 Dec 2021
yes，sir，is the target to make inner gray inclose to outer white？
so，may be use the optimal method，such as
clc; clear all; close all;
bw = im2bw(img);
bw2 = im2bw(img, 0.9);
bw3 = logical(bw - bw2);
gray_info = mean2(img(bw3));
% change shape
for i = 1 : 20
bw4 = bwmorph(bw,'thin',i);
bw5 = bw3&bw4;
ins(i) = abs(length(find(bw5(:)))/length(find(bw3(:))) - 1);
end
[~,ind] = max(ins);
bw4 = bwmorph(bw,'thin',ind);
% set shape
img2 = zeros(size(img));
img2(bw) = 255;
img2(bw4) = gray_info;
figure; montage({mat2gray(img),mat2gray(img2)}, 'Size', [1 2], 'BackgroundColor', 'w', 'BorderSize', [2 2]); now，we can find，left is origin，right is target，and inner right shape close to white
Gobert on 15 Dec 2021
Thank you! Your code gives an idea on how to perfectly match the SEO-GWA with SEO-WBA.

John D'Errico on 14 Dec 2021
All you have is a picture, not a shape. Until you express those boundaries as a shape of some sort, you cannot do anything.
1. Since both of them appear to be essentially convex obvjects, I might start by identifying pixels on the boundaries.
2. Next, pick some point that lies in the "center" of both domains.
3. Convert to polar coordinates, around that center. This will allow you to plor r(theta) for both curves.
4. Smooth those relationships for r(theta). A simple scheme might be as a smoothing spline of some sort, though you would want it to have periodic end conditions. (My SLM toolbox would offer that constraint. You can download it for free from the File Exchange.) Alternatively, you can fit both expressions as essentially a Fourier series, just a sum of sin and cosine terms, of the form sin(n*theta) and cos(n*theta) for integer n. The superposed sum will be periodic, and continuously differentiable at theta = 0, 2*pi.
5. Once you have the two relationships for r1(theta) and r2(theta), for any point on the first curve, you will simply scale it by the factor r2(theta)/r1(theta).
6. Finally, convert back to cartesian coordinates.
Gobert on 15 Dec 2021
Thank you!

Walter Roberson on 14 Dec 2021
If that boundary is close enough to be considered an ellipse, then there are File Exchange contributions to fit ellipses, and see also https://www.mathworks.com/matlabcentral/answers/584243-ellipse-fitting-with-matlab-using-fittype for some more methods.
The idea would be that you would fit an ellipse to the outer boundary and then use its orientation and eccentricty and foci to create an interior ellipse.
John's suggestions about fft and so on might be needed if you needed if the bumps in the outer edge are important features.
##### 2 CommentsShowHide 1 older comment
Gobert on 15 Dec 2021
Thank you, Walter!

R2021b

### Community Treasure Hunt

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

Start Hunting!