MATLAB Plots *.tif According to Light
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1761819/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1761824/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1761829/image.png)
10 Comments
Accepted Answer
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1763854/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1763859/image.png)
2 Comments
More Answers (2)
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
![](/matlabcentral/answers/uploaded_files/1761934/IMG_7598.jpeg)
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
% 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.
![](/matlabcentral/answers/uploaded_files/1761924/IMG_7600.jpeg)
![](/matlabcentral/answers/uploaded_files/1761919/IMG_7599.jpeg)
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 Comments
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.
See Also
Categories
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!