フィルターのクリア

Assign values for different layers of a 3D matrix

2 ビュー (過去 30 日間)
Anh Mai
Anh Mai 2021 年 8 月 9 日
Hi all;
I am trying to create a 3D domain, for example a parking lot with 100x100x10 size. Each element of the matrix represents a tile/concrete block with given value of 1. Now, I would like to build different structures (with different depth, up to 10) over it and replace the value of 1 by 2 for those specific new structures. I wrote the following code. It works well except the depth of different structure. For example, in this code, I set the depth of 1st structure is 3, 2nd is 4 and 3rd is 7. However, the code creates all structure with same depth as the last structure instead of 3,4 and 7 respectively. This mean when i use imagesc(squeeze(domain(:,:,7))), i expect to see only 3rd structure, but the code actually shows all structure. Please help me out with this, my domain2D here definitely not working well as expected.
clc; clear all; close all;
%Define size of the domain, user input
nx=100;
ny=100;
nz=10;
%Create domain baseline with value #1
domain=ones(nx,ny,nz);
%Create 2D matrix of x & y- grid coordinates for meshgrid command
[cols rows]=meshgrid(1:nx,1:ny);
%Create 2D domain in x & y- directions with value #1, same as baseline
%domain
domain2D=ones(nx,ny); %This is used for shapes other than rectangular and square
%Set number of structure to be in place
count=3;
for k=1:count %repeat input 3 times, with 1, 3 ,4 respectively to represent different structures.
fprintf('Please enter BMP number %0.0f information \n',k);
shape=input('1 = rectangular,2 = circle,3 = ellipse,4 = polygon: ');
nlayers=input('Enter depth of this structure: ');
if shape==1 %Rectangular
disp('This is a rectangular')
rec_x=input('length in x direction: ');
rec_y=input('length in y direction: ');
rec_x0=input('top left corner x-direction: '); %Define location of ...
%top left coordinate of the rectangular
rec_y0=input('top left corner y-direction: '); %Define location of...
%top left coordinate of the rectangular
%Set value #2 for this shape
domain2D(rec_y0:rec_y0+rec_y,rec_x0:rec_x0+rec_x)=2;
for r=1:nlayers %copy top layers to the z-direction upto nlayers of BMPs
domain(:,:,r)=domain2D;
end
elseif shape==2 %Circle
disp('This is a circle')
centerX = input('Enter x-coordinate of the center: ');
centerY = input('Enter y-coordinate of the center: ');
radius = input('Enter radius of the circle: ');
circle = (rows - centerY).^2 ...
+ (cols - centerX).^2 <= radius.^2; %conditions for BMPs
domain2D(circle)=2;%set value #2 for this BMP
for c=1:nlayers %copy top layers to the z-direction upto nlayers of BMPs
domain(:,:,c)=domain2D;
end
elseif shape==3 %Ellipse
disp('This is a ellipse')
centerX = input('Enter x-coordinate of the center: ');
centerY = input('Enter y-coordinate of the center: ');
radiusX = input('Enter x-radius of the ellipse: ');
radiusY= input('Enter y-radius of the ellipse: ');
ellipse = (rows - centerY).^2 ./ radiusY^2 ...
+ (cols - centerX).^2 ./ radiusX^2 <= 1;
domain2D(ellipse)=2;
for e=1:nlayers %copy top layers to the z-direction upto nlayers of BMPs
domain(:,:,e)=domain2D;
end
else disp('This is polygon (triangle, rhomnus, hexagon, etc)')
%For any other polygon, inputs are the x and y coordinates
%of each corners of the polygon to create vertices and use
%inpolygon syntax to navigate elements of matrix within the
%polygon
count2=1
for p=1:nx
for m=1:ny
xq(count2)=p;
yq(count2)=m;
count2=count2+1;
end
end
xv =input('Enter x-coordinates of polygon corners in the following format[value1 value2 value3]');
yv =input('Enter y-coordinates of polygon corners in the following format[value1 value2 value3]');
in=inpolygon(xq,yq,xv,yv);
domain2D(in)=2;
for h=1:nlayers %copy top layers to the z-direction upto nlayers of BMPs
domain(:,:,h)=domain2D;
end
end
end
  5 件のコメント
Anh Mai
Anh Mai 2021 年 8 月 11 日
Yes, it's exactly the reason why. And to solve this, I just simply enter the nlayers for each loop in descending order instead of randomly. However, I am trying the way to resolve this limitation of the code.
Emmanuel J Rodriguez
Emmanuel J Rodriguez 2021 年 8 月 13 日
Okay, what about setting the variable domain2d to nlayers instead of 2? Is the value 2 something you're set on?
This will output different pixel colors according to their integer value (depth).
clc; clear all; close all;
%Define size of the domain, user input
nx=100;
ny=100;
nz=10;
%Create domain baseline with value #1
domain=ones(nx,ny,nz);
%Create 2D matrix of x & y- grid coordinates for meshgrid command
[cols rows]=meshgrid(1:nx,1:ny);
%Create 2D domain in x & y- directions with value #1, same as baseline
%domain
domain2D=ones(nx,ny); %This is used for shapes other than rectangular and square
%Set number of structure to be in place
count=3;
for k=1:count %repeat input 3 times, with 1, 3 ,4 respectively to represent different structures.
fprintf('Please enter BMP number %0.0f information \n',k);
shape=input('1 = rectangular,2 = circle,3 = ellipse,4 = polygon: ');
nlayers=input('Enter depth of this structure: ');
if shape==1 %Rectangular
disp('This is a rectangular')
rec_x=input('length in x direction: ');
rec_y=input('length in y direction: ');
rec_x0=input('top left corner x-direction: '); %Define location of ...
%top left coordinate of the rectangular
rec_y0=input('top left corner y-direction: '); %Define location of...
%top left coordinate of the rectangular
%Set value #2 for this shape
%domain2D(rec_y0:rec_y0+rec_y,rec_x0:rec_x0+rec_x)=2;
%ejr
domain2D(rec_y0:rec_y0+rec_y,rec_x0:rec_x0+rec_x)=nlayers;
for r=1:nlayers %copy top layers to the z-direction upto nlayers of BMPs
domain(:,:,r)=domain2D;
end
elseif shape==2 %Circle
disp('This is a circle')
centerX = input('Enter x-coordinate of the center: ');
centerY = input('Enter y-coordinate of the center: ');
radius = input('Enter radius of the circle: ');
circle = (rows - centerY).^2 ...
+ (cols - centerX).^2 <= radius.^2; %conditions for BMPs
%domain2D(circle)=2;%set value #2 for this BMP
%ejr
domain2D(circle)=nlayers;
for c=1:nlayers %copy top layers to the z-direction upto nlayers of BMPs
domain(:,:,c)=domain2D;
end
elseif shape==3 %Ellipse
disp('This is a ellipse')
centerX = input('Enter x-coordinate of the center: ');
centerY = input('Enter y-coordinate of the center: ');
radiusX = input('Enter x-radius of the ellipse: ');
radiusY= input('Enter y-radius of the ellipse: ');
ellipse = (rows - centerY).^2 ./ radiusY^2 ...
+ (cols - centerX).^2 ./ radiusX^2 <= 1;
%domain2D(ellipse)=2;
%ejr
domain2D(ellipse)=nlayers;
for e=1:nlayers %copy top layers to the z-direction upto nlayers of BMPs
domain(:,:,e)=domain2D;
end
else disp('This is polygon (triangle, rhomnus, hexagon, etc)')
%For any other polygon, inputs are the x and y coordinates
%of each corners of the polygon to create vertices and use
%inpolygon syntax to navigate elements of matrix within the
%polygon
count2=1
for p=1:nx
for m=1:ny
xq(count2)=p;
yq(count2)=m;
count2=count2+1;
end
end
xv =input('Enter x-coordinates of polygon corners in the following format[value1 value2 value3]');
yv =input('Enter y-coordinates of polygon corners in the following format[value1 value2 value3]');
in=inpolygon(xq,yq,xv,yv);
%domain2D(in)=2;
%ejr
domain2D(in)=nlayers;
for h=1:nlayers %copy top layers to the z-direction upto nlayers of BMPs
domain(:,:,h)=domain2D;
end
end
end
% Image display
imagesc(squeeze(domain(:,:,7)))
hold on;
grid on;
grid minor;
hold off;
% Okay, well by setting domain2d to nlayers, the image displays different
% colors, now it's just a matter of displaying only certain integer values.
The image below, I inputted a rectangle, circle, and ellipse, with depths 3, 4, and 7, respectively.
My background is not in image processing, so I am not eniterly sure how to display an image as a function integer value.

サインインしてコメントする。

回答 (0 件)

カテゴリ

Help Center および File ExchangeElementary Polygons についてさらに検索

タグ

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by