You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How may I change certain areas of a geometry (specified by point point) with reference to a geometry?
1 view (last 30 days)
Show older comments
I have two pointclouds containing x,y,z cordinates of two similar geometry. Say A and B. A is suppose a square of size 1mm by 1mm. B is also a square of size 0.5mm by 0.5mm. However, in B one edge is distorted (see attached figure). Now I wish to correct it, which means, change it to sqaure of size 0.5mm by 0.5mm. Is there a way to do it?
Accepted Answer
yanqi liu
on 5 Jan 2022
clc; clear all; close all;
[X,map,alpha] = imread('https://ww2.mathworks.cn/matlabcentral/answers/uploaded_files/852940/Picture1.png');
figure; imshow(alpha);
bw = im2bw(alpha);
bw = bwareaopen(imfill(bw, 'holes'), 1000);
bw2 = bwareafilt(bw, 2);
bw(bw2) = 0;
[r,c] = find(bw);
bw(min(r):max(r),min(c):max(c)) = 1;
bw = logical(bw);
be = imdilate(bwperim(bw), strel('disk', 3));
alpha(be) = 255;
figure; imshow(be);
figure; imshow(alpha);
13 Comments
Ajay Goyal
on 5 Jan 2022
Dear Yanqi Liu,
As much as I understood, this code has transferred the pixels of image 2 to form a sqaure by using code:
bw(min(r):max(r),min(c):max(c)) = 1;
However, my purpose is to map the geometry in Figure 1 to Figure 2 such that shape wil alter with respect to figure 1. Please comment. What if first figure is a circle and I have to map a start shape (Figure2). Actually I have two 3D geometry with known cordinates. One of them is a reference while other has some model issues at certain areas. I have to correct those areas with reference to 1st frame
yanqi liu
on 6 Jan 2022
yes,sir
map the geometry in Figure 1 to Figure 2
square---->square
circle---->circle
……
the size should use Figure2 size?
yanqi liu
on 6 Jan 2022
clc; clear all; close all;
[X,map,alpha] = imread('https://ww2.mathworks.cn/matlabcentral/answers/uploaded_files/852940/Picture1.png');
figure; imshow(alpha);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/854585/image.png)
bw = im2bw(alpha);
bw = bwareaopen(imfill(bw, 'holes'), 1000);
% select 1、2 block
[L,~] = bwlabel(bw);
stats = regionprops(bw);
rects = cat(1,stats.BoundingBox);
[~,id] = sort(rects, 1);
% first
bw1 = L == id(1);
[r,c] = find(bw1);
bw1 = bw1(min(r):max(r),min(c):max(c));
% second
bw2 = L == id(2);
[r,c] = find(bw2);
bw2 = bw2(min(r):max(r),min(c):max(c));
% transform bw1 to bw2
at = [];
while 1
at =[at; abs(length(find(bw1(:)))-length(find(bw2(:))))];
bw1 = bwmorph(bw1,'thin');
if length(find(bw1(:)))/length(find(bw2(:))) < 0.5
break;
end
end
[~, ind] = min(at);
bwt = L == id(1);
for i = 1 : ind
bwt = bwmorph(bwt,'thin');
end
% replace
bwt = logical(bwt);
be = imdilate(bwperim(bwt), strel('disk', 3));
cen1 = stats(id(1)).Centroid;
cen2 = stats(id(2)).Centroid;
ts = round(cen2-cen1);
be2 = imtranslate(be,ts);
% make to origin
alpha(be2) = 255;
figure; imshow(alpha);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/854590/image.png)
Ajay Goyal
on 6 Jan 2022
Dear Yanqi Liu,
Thank you for the updated code. Great work! It is working well for different problems in a 2D section (please see the attached figures.)
It will be interesting to understand how it can be modified to fit in with 3D point clouds (geometries)? For example, consider I have a sphere of radius 2mm to be mapped on a ellipsoid with axis rius of 2, 1.5 and 3mm in x, y and z direction, respectively. Please check the attached code to generate the data for mapping. Please let me know, if I could upvote all your responses. I believe, similar problems are encountered by other researchers and this discussion will help them to solve such issues. My research problem in particular is much complicated. This is one stage where I got stuck and need your help. Thanks again!
Ajay Goyal
on 6 Jan 2022
clc; clear; close all
%% Reference geometry is a sphere of radius 2 units
[x,y,z]=sphere;
x=2*x(:);y=2*y(:);z=2*z(:);
%% Geometry to be morphed is an ellipsoid with centre (0,0,0) and axis radius of 2, 1.5 and 3 units along x, y and z directions
[x1,y1,z1]=ellipsoid(0,0,0,2,1.5,3);
x1=x1(:);y1=y1(:); z1=z1(:);
%% creating point clouds of both data. We can ship this is not needed
ptCloud1 = pointCloud([x y z]);
ptCloud2 = pointCloud([x1 y1 z1]);
%% plotting the data
plot3(x,y,z,'k*')
hold on, plot3(x1,y1,z1,'r*')
axis square
yanqi liu
on 6 Jan 2022
clc; clear; close all
%% Reference geometry is a sphere of radius 2 units
[x,y,z]=sphere;
x=2*x(:);y=2*y(:);z=2*z(:);
%% Geometry to be morphed is an ellipsoid with centre (0,0,0) and axis radius of 2, 1.5 and 3 units along x, y and z directions
[x1,y1,z1]=ellipsoid(0,0,0,2,1.5,3);
x1=x1(:);y1=y1(:); z1=z1(:);
%% creating point clouds of both data. We can ship this is not needed
% ptCloud1 = pointCloud([x y z]);
% ptCloud2 = pointCloud([x1 y1 z1]);
%% plotting the data
plot3(x,y,z,'k*')
axis square
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/854700/image.png)
% first
[xf1,xps1] = mapminmax(x,-1,1);
[yf1,yps1] = mapminmax(y,-1,1);
[zf1,zps1] = mapminmax(z,-1,1);
% second
[xf2,xps2] = mapminmax(x1,-1,1);
[yf2,yps2] = mapminmax(y1,-1,1);
[zf2,zps2] = mapminmax(z1,-1,1);
% make first to second
xf = mapminmax('reverse', mapminmax('apply', x1, xps1), xps2);
yf = mapminmax('reverse', mapminmax('apply', y1, yps1), yps2);
zf = mapminmax('reverse', mapminmax('apply', z1, zps1), zps2);
% plot
figure;
plot3(xf,yf,zf,'k*')
axis square
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/854705/image.png)
Ajay Goyal
on 6 Jan 2022
Dear Yanqi,
I really appreciate your valuable help. I am pleased to vote your answer. Please note that your code for 3D is not working. The resulting geometry is same (ellipsoid) and not closer to sphere. There is no difference between any point (xf-x1, yf-y1, and zf-z1 are all zero at each point).
yanqi liu
on 6 Jan 2022
yes,sir,may be check
[x, y, z] and [xf, yf, zf]
is it right?
clc; clear; close all
%% Reference geometry is a sphere of radius 2 units
[x,y,z]=sphere;
x=2*x(:);y=2*y(:);z=2*z(:);
%% Geometry to be morphed is an ellipsoid with centre (0,0,0) and axis radius of 2, 1.5 and 3 units along x, y and z directions
[x1,y1,z1]=ellipsoid(0,0,0,2,1.5,3);
x1=x1(:);y1=y1(:); z1=z1(:);
%% creating point clouds of both data. We can ship this is not needed
% ptCloud1 = pointCloud([x y z]);
% ptCloud2 = pointCloud([x1 y1 z1]);
%% plotting the data
plot3(x,y,z,'k*')
hold on;
plot3(x1,y1,z1,'ro')
axis square
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/854835/image.png)
% loop
at = [];
rs = [];
x1t = x1;y1t = y1;z1t = z1;
rate = 1;
while 1
at =[at; norm(x1-x)+norm(y1-y)+norm(z1-z)];
rs = [rs; rate*0.9];
rate = rate-0.01;
x1t = x1t*rate;
y1t = y1t*rate;
z1t = z1t*rate;
if norm(x1t)+norm(y1t)+norm(z1t) < 0.3*norm(x1)+norm(y1)+norm(z1)
break;
end
end
[~, ind] = min(at);
xf = x1*rs(ind);
yf = y1*rs(ind);
zf = z1*rs(ind);
% plot
figure;
plot3(x,y,z,'k*')
hold on;
plot3(xf,yf,zf,'bs')
plot3(x1,y1,z1,'ro')
axis square
legend('first','fist->second','second');
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/854840/image.png)
Ajay Goyal
on 6 Jan 2022
Edited: Ajay Goyal
on 6 Jan 2022
Dear Users,
This is indeed a beautiful code developed by yanqi liu. It can now be used for morphing 3D pointclouds as well. Thanks Yanqi Liu.
Regards,
Ajay
Ajay Goyal
on 6 Jan 2022
Dear Yanqi,
I was checking that in both geometries, number of points are same (441). Well it should be like that otherwise we could do further calculations. Fair enough! In some cases, if there are more points than the other, problem can be solved using "pcdownsample" command. It reduces number of points without effecting the shape of geometry.
In addition to that; two points are corresponding such as x(1) corresponds to x1(1), x(2) corresponds to x1(2) and so on. In most cases, we could not get the correspondence. For example, please see the attached pictures. Point 1-4 are not corresponding but only 5. Can we solve such issues? Is there a way via which we can renumber them such that correspondence can be maintained.
Regards,
Ajay
yanqi liu
on 7 Jan 2022
yes,sir,may be make relation between them,such as
clc; clear all; close all;
[X,map,alpha] = imread('https://ww2.mathworks.cn/matlabcentral/answers/uploaded_files/855030/Picture1.png');
figure; imshow(alpha);
bw = im2bw(alpha);
bw2 = bwareaopen(imfill(bw, 'holes'), 1000);
bw2 = bwareafilt(bw2, 2);
bw3 = bw;
bw3(bw2) = 0;
bw4 = bw;
bw4(~bw2) = 0;
[L,num] = bwlabel(bw4);
statsp = regionprops(L);
areap = cat(1,statsp.Area);
[~,id] = sort(areap, 'descend');
bw4a = L == id(1);
bw4b = L == id(2);
cen1 = statsp(id(1)).Centroid;
cen2 = statsp(id(2)).Centroid;
ts = round(cen1-cen2);
bw4b2 = imtranslate(bw4b,ts);
bw4ab = logical(bw4a+bw4b2);
% locate every number
[r, c] = find(bw4);
stats = regionprops(bw3);
pts1 = [];
pts2 = [];
for i = 1 : length(stats)
ceni = stats(i).Centroid;
disi = pdist2(ceni, [c, r]);
[~,ind] = min(disi);
hold on;
plot([ceni(1) c(ind)], [ceni(2) r(ind)], 'r-', 'LineWidth', 2);
if bw4a(r(ind), c(ind))
pts1 = [pts1; c(ind) r(ind)];
end
if bw4b(r(ind), c(ind))
pts2 = [pts2; c(ind) r(ind)];
end
end
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/855750/image.png)
% compute
pts3 = pts2;
pts3(:,1) = pts3(:,1)+ts(1);
pts3(:,2) = pts3(:,2)+ts(2);
figure; imshow(bw4ab);
hold on;
plot(pts1(:,1), pts1(:,2), 'm*');
plot(pts3(:,1), pts3(:,2), 'ro');
% get relation
relation = [];
for i = 1 : size(pts3,1)
disi = pdist2(pts1, pts3(i,:));
[~,ind] = min(disi);
relation = [relation; i, ind];
plot([pts3(i,1) pts1(ind,1)], [pts3(i,2) pts1(ind,2)], 'r-', 'LineWidth', 2);
end
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/855755/image.png)
figure; imshow(alpha);
hold on;
for i = 1 : size(relation, 1)
plot([pts1(relation(i, 2), 1) pts2(relation(i, 1), 1)], [pts1(relation(i, 2), 2) pts2(relation(i, 1), 2)], 'm-', 'LineWidth', 2);
end
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/855760/image.png)
now,we can get the point relation
Ajay Goyal
on 7 Jan 2022
Thank you very much my friend. It will solve the purpose. I am home quarantined since yesterday and will try to study it on Monday, if report is in my favor :). Take care Regards, Dr. Ajay
yanqi liu
on 8 Jan 2022
it is my pleasure,in thie question,we use the loop iteration for compute,and choose the best result. of course,may be use some parameter to adjust
More Answers (0)
See Also
Categories
Find more on Fixed-Point Conversion in Help Center and File Exchange
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)