How do I plot the upper view of a matrix and represent the intensity color?

2 views (last 30 days)
I'm trying to represent the shadow intensity of clouds on the ground. I developed a midpoints displacement algorithm to generate 3D fractal surfaces. This algorithm returns values of the height H in a matricial form, with a dimension equal to 257 x 2570. So, I'm using the contourf function to plot the intersection between the generated fractal and a horizontal plane. The resulting intersections for different values of the horizontal plane Z are demonstrated in the figure below, representing 5 layers of the same fractal image that I want to render.
Besides, I want to work with values in a range between 0 and 1, so I can use the weigthed arithmetic means to represent the shadow intensity, once clouds are more opaque in its centers and less opaque at their boundaries. In this way, I'm using the code below to write new S matrices that represents each layers. For the sake of brevity, I'm just showing how I'm obtaining the first layer. Other four layers are obtained in a similar manner, but they result in matrices with more 1's than the first one. Calculating the the weigthed arithmetic means between the five matrices, the result is one matrix with values in a range between 0 and 1.
for cc = 1 : ((N+1) * F) % counting columns
for cl = 1 : (N+1) % counting lines
if P(cl,cc) < plane_z1 % if value of P are lower than the value of the plane Z return 1
S1(cl,cc) = 1;
cont_S1 = cont_S1 + 1;
else % if value of P are larger than the value of the plane Z return 0
S1(cl,cc) = 0;
end
end
end
Now, I want to plot this new matrix, that is an average of the five layers I showed, and represent the scale of each pixel. I just want a scale that represents how dark each pixel is. Moreover, I want to represent values equal to 0 with the white color. In this way, I also want to establish an increasingly dark colormap for values larger than 0 and lower than 1, until values equal to 1 are represented with the black color.
The two problems are:
1) How do I plot the upper view of the resulting averaged matrix in a 2D dimension (like the layers I showed above), once I will not use the contourf function anymore?
2) How do I set the colormap I described above?
I appreciate any help!

Accepted Answer

Joseph Cheng
Joseph Cheng on 5 Jun 2021
Edited: Joseph Cheng on 5 Jun 2021
Here is an example of what i'm thinking you're doing. Not sure what you're doing with the weighted average but i think i did something that you could adapt? It wasn't too clear what you mean about plot this new matrix from the layers but here you go.
%%Matlab Answers Test script
%% matlab functions noiseonf.m retrieved from <https://www.mathworks.com/matlabcentral/answers/217417-how-fourier-synthesis-is-performed-using-matlab>
% Uses the substance of noiseonf routine of
% Peter Kovesi to generate dummy cloud image:
% Copyright (c) 1996-2005 Peter Kovesi
% School of Computer Science & Software Engineering
% The University of Western Australia
% http://www.csse.uwa.edu.au/
% download link appears to be broken so visit matlab answers above
%generated here is some dummy image data, just for demonstration since we
%can now run code!
rows = 258;
columns = 2570;
factor = 1.7;
im = randn(rows, columns);
imfft = fft2(im); % Take fft of image.
imfft = fftshift(imfft); % Shift 0 frequency to the middle.
mag = abs(imfft); % Get magnitude
phase = imfft./mag; % and phase
x = ones(rows,1) * (-columns/2 : (columns/2 - 1));
y = (-rows/2 : (rows/2 - 1))' * ones(1,columns);
radius = sqrt(x.^2 + y.^2); % Matrix values contain radius from centre.
radius(rows/2+1,columns/2+1) = 1; % .. avoid division by zero.
filter = 1./(radius.^factor); % Construct the filter.
newfft = filter .* phase;
grayImage = real(ifft2(fftshift(newfft))); % Invert to obtain final noise image
grayImage = grayImage-min(grayImage(:));
grayImageN = grayImage./max(grayImage(:));
% now that dummy image is set as values between 0 and 1
%lets show how to change the color map to be dark = 1 and light = 0
figure(1),clf,
colormap gray %not sure if it's needed here but the web implementation needs it to make it grayscale
ax(1)=subplot(211); % save the subplot axes handle for colormap to assign the color map
imagesc(grayImageN),colormap(ax(1),'gray'), %original colormap of 0 dark: 1 lit
title('original map')
colorbar
cmap = colormap;
ax(2)=subplot(212); ;
imagesc(grayImageN),title('flipped color map'),colorbar
colormap(ax(2),flipud(cmap)) %flip the colormap desination upsidedown
%layers no need for loop to check each pixel
zPlaneVals = [.1 .3 .5 .7 .9]; %list of layers to use
Splane = zeros(rows,columns,numel(zPlaneVals)); %pre-allocate memory for layer stack
figure(2),
for Lind = 1:numel(zPlaneVals)%layer index
TempBinImg = zeros(size(grayImageN)); %temporary zero image
%since in your example only set to 1 when values of original image is
% < threshold value otherwise we've already set the rest to 0.
TempBinImg(grayImageN<zPlaneVals(Lind))=1; %replace the Temp image for index values <threshold
Splane(:,:,Lind) = TempBinImg; %store this into stack
Sax(Lind)=subplot(numel(zPlaneVals),1,Lind); %create subplot
imagesc(Splane(:,:,Lind)),colormap(Sax(Lind),flipud(cmap)); %display image and the flipped colorbar
title(['Zplane thresh <' num2str(zPlaneVals(Lind))])
WeightSplanes(:,:,Lind) = zPlaneVals(Lind)*Splane(:,:,Lind); %save some dummy weight applied layer
end
WeightedPlane = sum(WeightSplanes,3)/sum(zPlaneVals); %divide the sum of the layered weight applied images by the sum of weights
figure,imagesc(WeightedPlane),colormap gray, colormap(flipud(cmap)),colorbar
  1 Comment
Eric Bernard Dilger
Eric Bernard Dilger on 5 Jun 2021
Hey Joseph, thanks for your support. It was really helpful!
I didn't know about the imagesc function. It seems to be working! The figure below represents the average layer of the five layers I was talking about. Thank you!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!