How to extract the cdata of a 2D surface from 3D surface and save it as a matrix?

조회 수: 8 (최근 30일)
Hello, I generated a 3D surface. The task is to extract the cdata of a 2D surface from the 3D surface and save it as png files. For example, extract the surface (0,90). I attached the X and Y and Z values here. You can use the below codes to generate a 3D surface in my example:
%generate a 3D surface
figure(1);subplot(1,2,1);
surf(X,Y,Z);colormap jet;
shading interp;
%see the (0,90) 2D surface;
view(0,90);subplot(1,2,2);
3D 2D
This is my method to extract the 2D surface:
view(0,90);
c=getframe(gca);
imwrite(c.cdata,'myimage.png');
I know this is good method to get the gca cdata. However, I have to generate millions of images. This method is a little slow. Could I know a direct method to generate a cdata matrix from some calculation with X Y and Z instead of using getframe()??
By the way, could I know how to write the cdata matrix to png files using the function writematrix()?
Thank you very much!!!!!!!!

채택된 답변

Image Analyst
Image Analyst 2022년 3월 13일
편집: Image Analyst 2022년 3월 13일
You can just assign the matrix directly, like (untested)
load("X.mat");
load("Y.mat");
load("Z.mat");
tic
outputImage = zeros(1, 500, 'uint8');
xs = round(rescale(X, 1, 500));
ys = round(rescale(Y, 1, 500));
zs = round(rescale(Z, 0, 255));
for k = 1 : numel(xs)
outputImage(ys(k), xs(k)) = zs(k);
end
imshow(outputImage, 'ColorMap', jet(256), ...
'XData', [min(X, [], 'all'), max(X, [], 'all')], ...
'YData', [min(Y, [], 'all'), max(Y, [], 'all')] ...
);
axis('on', 'image')
toc
  댓글 수: 9
Ke Zhang
Ke Zhang 2022년 3월 14일
편집: Ke Zhang 2022년 3월 14일
I ran your code thanks. By the way, Could I know how to change the color in the four cornors to white? Thanks!
Image Analyst
Image Analyst 2022년 3월 14일
If you're doing deep learning, those Z images are just sitting in a folder that you have an imageDataStore set up to point to.
To set the corners to white, set the values to 255
Z(1,1) = 255;
Z(1,end) = 255;
Z(end, 1) = 255;
Z(end, end) = 255;
Z=cat(3,Z,Z,Z);
% Then save to disk so your imageDatastore will point to it.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Lighting, Transparency, and Shading에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by