MATLAB Plots *.tif According to Light

62 次查看(过去 30 天)
Hi,
I am trying to plot a lunar terrain from a *.stl file. You can see the real image and plot in photos. MATLAB is plotting the deepest place as dark blue, the highest as yellow, but the thing is MATLAB's dark blue is not actually the deepest place, it is just shadow, likewise MATLAB's yellow actually isn't the highest place, it just takes the most light so its brighter. This is my code snippet to plot it. My real aim is to create a simulation, therefore I need the surface model.
[dem, ~] = readgeoraster('moon1m-a.tiff');
gridSize = 3900;
start = 1;
[X, Y] = meshgrid(1:gridSize, 1:gridSize);
Z = dem(start:start+gridSize-1, start+gridSize-1:-1:start);
figure
mesh(X, Y, Z);
Images are aligned, you can see the shadow places at the bottom right.
Any help would be appreciated.
Thanks in advance.
Edit:
The *.tiff file is in the attachments.
  10 个评论
DGM
DGM about 23 hours 前
@Umar I think this is less about displaying the height data, and more about trying to estimate terrain height from a photograph.
Umar
Umar about 21 hours 前
Hi @DGM,
I do agree with your comments but this is the best I can do. Hopefully, @Image Analyst might do better job at this than me.

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst about 20 hours 前
This is what I got so far:
% Initialization steps.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 18;
[img, cmap] = imread('moon1m-a.tiff');
h1 = subplot(2, 2, 1);
imshow(img, []);
axis('on', 'image')
impixelinfo;
title('Original Gray Scale Reflectance Image', 'FontSize', fontSize)
h2 = subplot(2, 2, 3:4);
histogram(img);
title('Histogram of Gray Scale Image', 'FontSize', fontSize)
grid on;
xlabel('Gray Level', 'FontSize', fontSize)
ylabel('Count', 'FontSize', fontSize)
h3 = subplot(2, 2, 2);
imshow(img, []);
axis('on', 'image')
impixelinfo;
colormap(h3, "parula")
colorbar
%clim([15, 255])
title('Gray Scale Image, Pseudocolored', 'FontSize', fontSize)
This is simply pseudocoloring the image. There is no way to get height/elevation information from this single image since highly reflective things could be on mountain tops or in valleys. Same for dark things. If you had more images you might be able to do something with "shape from shading" (Google it or go here for articles: http://www.visionbib.com/bibliography/contentsshapefrom.html#3-D%20Shape%20from%20X%20--%20Shading,%20Textures,%20Lasers,%20Structured%20Light,%20Focus,%20Line%20Drawings ) but that goes beyond the scope of us here.
If you had an image with reflectance data AND another image with elevation data, then you could apply the pseudcolors from the relectance image to a surface made from the height data. See attached example.
I think you must have the height data somewhere since it was used in Fusion 360 to make your surface rendering. If you want to do something in MATLAB then you will have to figure out how to get the elevation image out of Fusion 360.
  2 个评论
Barbaros Teoman Kosoglu
Barbaros Teoman Kosoglu about 18 hours 前
I think Fusion was just guessing the heights from colors. Thank you for your effors, I will try to find another lunar surface file.
Image Analyst
Image Analyst about 16 hours 前
If it's just assuming that brightness is proportional to elevation (not a good assumption in my opinion) then I think you can just apply a colormap. Not sure I understand what the problem is in that case.

请先登录,再进行评论。

更多回答(2 个)

Umar
Umar 2024-8-29,3:01

Hi @ Barbaros Teoman Kosoglu,

Please let me address your query regarding, “I am trying to plot a lunar terrain from a *.stl file. You can see the real image and plot in photos. MATLAB is plotting the deepest place as dark blue, the highest as yellow, but the thing is MATLAB's dark blue is not actually the deepest place, it is just shadow, likewise MATLAB's yellow actually isn't the highest place, it just takes the most light so its brighter. This is my code snippet to plot it. My real aim is to create a simulation, therefore I need the surface model. [dem, ~] = readgeoraster('moon1m-a.tif'); gridSize = 3900; start = 1; [X, Y] = meshgrid(1:gridSize, 1:gridSize); Z = dem(start:start+gridSize-1, start+gridSize-1:-1:start); figure mesh(X, Y, Z); Images are aligned, you can see the shadow places at the bottom right. I can't put the file into the attachments, it is too big even after compressing. I can put the *.tif into a 3D drawing tool, this is the output.Any help would be appreciated.”

Please see my response to your comments below.

First, I saved your output image “*.tif into a 3D drawing tool, this is the output.” as shown in your comments to JPG image and converted to TIF format in matlab.

% Load the JPG image
img = imread('/MATLAB Drive/IMG_7597.PNG');
% Write the image to a TIF file
imwrite(img, '/MATLAB Drive/IMG_7597.tif');

Please see attached IMG_7597.tif

Then, to visualize a Digital Elevation Model (DEM), I loaded the DEM data from a TIFF file (Reference: '/MATLAB Drive/IMG_7597.tif') using the readgeoraster function. It then checks the dimensions of the loaded DEM array against a predefined grid size of 3900. If the DEM is smaller than this grid size, it adjusts the grid size to match the smaller dimension of the DEM. Next, it initializes a meshgrid for the specified grid size and extracts the elevation data (Z) from the DEM, flipping it for correct orientation. Afterwards, a figure is created to plot the mesh, with shading applied to interpolate colors between points, enhancing the visual representation of the terrain. The view is set to 3D, and a colormap (jet or parula) is applied to reflect elevation data effectively. A colorbar is added for interpretation, and axes are labeled for clarity. Finally, the plot is titled "Lunar Terrain Elevation Model," providing context for the visualization.

% Load the DEM data
[dem, ~] = readgeoraster('/MATLAB Drive/IMG_7597.tif');

For more information on readgeoraster, please refer to

https://www.mathworks.com/help/map/ref/readgeoraster.html

    % Define grid size
    gridSize = 3900;
    % Check the size of the dem array
    [rows, cols] = size(dem);
    if rows < gridSize || cols < gridSize
      warning('The dem array is smaller than the required grid size. Adjusting 
      grid size to match DEM dimensions.');
      gridSize = min(rows, cols); % Adjust grid size to the smaller dimension
    end
    % Initialize meshgrid for the specified grid size
    start = 1;
    [X, Y] = meshgrid(1:gridSize, 1:gridSize);
    % Extract and flip Z data for correct orientation
    Z = dem(start:start+gridSize-1, start+gridSize-1:-1:start);
    % Create figure for plotting
    figure;
    % Plot the mesh with appropriate shading and colormap
    mesh(X, Y, Z);
    % Set shading to interpolate colors between points
    shading interp;

For more information on shading, please refer to

https://www.mathworks.com/help/matlab/ref/shading.html?searchHighlight=shading%20interp&s_tid=srchtitle_support_results_1_shading%20interp

    % Adjust view angle for better visualization
    view(3); % Set view to 3D
    % Set colormap to better reflect elevation data
    colormap(jet); % or use parula for a different gradient
    % Add colorbar to interpret values
    colorbar;
    % Label axes for clarity
    xlabel('X Coordinate');
    ylabel('Y Coordinate');
    zlabel('Elevation (Z)');
    % Set title for context
    title('Lunar Terrain Elevation Model');

Please see attached.

Since you mentioned issues with file sizes, consider compressing your .tif files or breaking them into smaller sections if necessary. This can facilitate easier handling in MATLAB. In case, if you want to explore more advanced visualization techniques (like contour plots or 3D surface plots), MATLAB offers functions like surf or contour3, which might also help in conveying depth and features more effectively. Hopefully, by implementing these changes, you should achieve a more accurate representation of the lunar terrain based on your elevation data while mitigating issues related to shadowing and light intensity. Please let me know if you have any further questions.

  3 个评论
Umar
Umar 2024-8-30,3:29

Hi @ Barbaros Teoman Kosoglu,

Let me address your concerns , I don't understand what you changed, I still have the same output but different colors, thats it.

The reason why are you getting the same output is because you have not shared your code and major reason of not sharing file as mentioned by you in your posted comments, I can't put the file into the attachments, it is too big even after compressing. I can put the *.tif into a 3D drawing tool, this is the output.

So, to help you out while you were struggling, I had to save the output image provided by you which by default saved as IMG_7597.PNG and then I had to convert this PNG image into tiff format as mentioned in my posted comments and when I implemented the code it provided me the lunar terrain elevation model which is depicted in the code above. But your code which is implemented by you, not shared with us is producing entirely different results. The confusion is not matching apples to apples in order to get what you are looking for.

So, at this point do what is mentioned by @Image Analyst below such as, “ having Fusion 360 crop down the image to get it less than 5 MB ” so we can help you with aiming towards creating a simulation, and help you get the surface model you need. Hope this helps clarify the confusion.

Barbaros Teoman Kosoglu
Barbaros Teoman Kosoglu 2024-8-30,19:44
I've just attached the *.tiff file, into the question

请先登录,再进行评论。


Image Analyst
Image Analyst 2024-8-30,0:47
In your 3D-ish rendering, where exactly are the shadows? And the yellow spots? Can you point to them with arrows or something?
If the image is a reflectance image, representing the brightness of light reflected off the surface, then that's not a depth image. You'd need a separate depth (elevation) image inorder to pseudocolor a depth as a color.
Can you have Fusion 360 crop down the image to get it less than 5 MB so you can attach it with the paperclip icon?
  1 个评论
Barbaros Teoman Kosoglu
Barbaros Teoman Kosoglu 2024-8-30,19:47
I don't know why but the 3D render plots a big area, while the tiff actually is not such big area.
And another weird thing is imfinfo() says the file is grayscale, so I don't know how it can see shadows and lights.
And I just compressed and attached the file into the question.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Surface and Mesh Plots 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by