Visualize a 3D image in Matlab - matlab

Think of a 3 dimensional image V stored, in matlab, as an array for red, green, and blue. For pixel (i,j,k) we would have
V(i,j,k,1)=0.1 (red)
V(i,j,k,2)=0.9 (green)
V(i,j,k,3)=0.2 (blue)
I would then like to visualize this 3D image as a moving 2D slice. I thought the "slice" command could do the trick but it is designed to visualize 3D functions only, not 3D images. Do you have any suggestion?
I am adding a script that could help but does not achieve what is requested:
clear all; clc; close all;
N=35;
w=zeros(N,N,N);
x=linspace(-5,5,N); y=linspace(-5,5,N); z=linspace(-5,5,N);
for i=1:N
for j=1:N
for k=1:N
w(i,j,k)=cos(k)/(1+i+j)^2;
end
end
end
for k = 0:.1:10
hsp = surf(linspace(-5,5,Nx),linspace(-5,5,Ny),zeros(Nz));
rotate(hsp,[1,0,0],18*k,[0 0 0]) %rotate a slice where the image is shown
xd = hsp.XData;
yd = hsp.YData;
zd = hsp.ZData;
delete(hsp)
slice(x,y,z,w,xd,yd,zd)
shading flat
hold off
view(-25,20)
axis([-5 5 -5 5 -5 5]);
drawnow
end
The above w array should now be a 3D image instead.

It sounds like you want the functionality of slice but you want it to operate on an [N x M x P x 3] array instead of an [N x M x P] array.
One way to do this is by setting up an interpolator for each channel:
red = griddedInterpolant({x,y,z},V(:,:,:,1));
green = griddedInterpolant({x,y,z},V(:,:,:,2));
blue = griddedInterpolant({x,y,z},V(:,:,:,3));
and then replacing
slice(x,y,z,w,xd,yd,zd)
with
cdata = cat(3, red(xd,yd,zd), green(xd,yd,zd), blue(xd,yd,zd));
surface(xd,yd,zd,cdata);
I tried this on your example and found that the colors in w were all very close to black. So I loaded a different image to work on:
% An orange-and-black skull for Halloween
load mri
V = permute(double(D), [1 2 4 3]);
V = V / max(V(:));
V = bsxfun(#times, V, reshape([1 .3 0], [1 1 1 3]));
% Define grid coordinates and interpolator
[Nx, Ny, Nz, nCh] = size(V);
x = linspace(-5,5,Nx);
y = linspace(-5,5,Ny);
z = linspace(-5,5,Nz);
red = griddedInterpolant({x,y,z},V(:,:,:,1));
green = griddedInterpolant({x,y,z},V(:,:,:,2));
blue = griddedInterpolant({x,y,z},V(:,:,:,3));
for k = 0:.1:10
hsp = surf(linspace(-5,5,Nx),linspace(-5,5,Ny),zeros(Ny,Nx));
rotate(hsp,[1,0,0],18*k,[0 0 0])
xd = hsp.XData;
yd = hsp.YData;
zd = hsp.ZData;
delete(hsp)
cdata = cat(3, red(xd,yd,zd), green(xd,yd,zd), blue(xd,yd,zd));
surface(xd,yd,zd,cdata)
shading flat
hold off
view(-25,20)
axis([-5 5 -5 5 -5 5]);
drawnow
end

Related

Gradient colors for faces of surface

I have the following code for MATLAB:
close all
clear all
clc
edges= linspace(0,1,10);
[X,Y] = meshgrid(edges);
Z=rand(10);
h= surf(X,Y,Z,'FaceColor','none')
I need to paint the faces on this surface. The face with coordinate (0,0) should be green and the face with coordinate(1,1) should be red. All faces on diagonal should be yellow.
Could You help me to perform this painting?
If you have a closer look at the surf command, you'll see, that you can set a custom "colormap", which is then used instead of the Z data as a color indicator.
So, you just have to set up a proper "colormap". That one must have the same dimensions as your X and Y data, and for each data point you must specify the [R, G, B] triplet of your choice, i.e. [0, 1, 0] for the [0, 0] coordinate, [1, 0, 0] for the [1, 1] coordinate, and some "diagonal" interpolation between those two.
Luckily, you already have that, have a look at your X and Y data! Adding both will give that kind of "diagonal" interpolation for the green channel. The inverse of that will give the proper red channel. (The scaling is a bit corrupt, since you have values larger than 1.0, but these will "clipped".)
Here's the enhanced code:
edges = linspace(0, 1, 10);
[X, Y] = meshgrid(edges);
Z = rand(10);
cm(:, :, 1) = (X + Y); % Red channel
cm(:, :, 2) = 2 - cm(:, :, 1); % Green channel
cm(:, :, 3) = zeros(size(X)); % Blue channel (empty)
h = surf(X, Y, Z, cm); % No need for the FaceColor property
The output looks like this:
Hope that helps!
One way would be the following:
edges= linspace(0,1,10);
[X,Y] = meshgrid(edges);
Z=rand(10);
% Fake some color data
cdata = zeros(size(Z));
for i = 1:size(cdata,1)
cdata(i,i) = i;
end
%make a suitable colormap
cm = ones(size(cdata,1), 3);
cm = cm .*[ 1 1 0 ] % everything yellow
cm(1, :) = [1 1 1] % except for 0
cm(2, :) = [1 0 0] % except for 1
cm(end, :) = [0 1 0] % except for 10
ax = axes();
h = surface(ax, X,Y,Z);
h.CData = cdata;
ax.Colormap = cm;

Re-scaling X axis in imagesc plot in Matlab

I have written the following code in Matlab to show different shades of color red. How can I re-scale all x-axis in sub-plots to 0-1?
% create a custome color map
map = zeros(11,3);
reds = 0:0.1:1;
map(:,1) = reds(:);
colormap(map);
figure(1)
depth = [1 2 3 4];
num_depth = length(depth);
for index = 1: num_depth
subplot(num_depth,1,index);
step = 1/(2^depth(index)-1);
A = 0:step:1;
imagesc(A);
axis equal
set(gca,'ytick',[])
end
However, imagesc(x,y,C) could specifies the image location. Use x and y to specify the locations of the corners corresponding to C(1,1) and C(m,n).
% create a custome color map
map = zeros(11,3);
reds = 0:0.1:1;
map(:,1) = reds(:);
colormap(map);
figure(1)
depth = [1 2 3 4];
num_depth = length(depth);
for index = 1: num_depth
subplot(num_depth,1,index);
step = 1/(2^depth(index)-1);
A = 0:step:1;
imagesc(0:1,0:1,A)
axis([0 1 0 1])
set(gca,'ytick',[])
end
the output is:

MATLAB: How do I shade plot area with increasing and decreasing shades of a color

I want to plot an area of a graph (x = 1:24, y = -1:1) with one color (black), which I then want to decrease/increase in shading in terms of the time of day. So, that I have a plot which is 'dark' in the background at night and 'light' during the day with x being hours of day and y being a data value. My sunrise would be at 6.8 and my sunset would be at 22. I would then overlay scatter plots with data on top.
I have tried messing around with patch and area but with no luck. Here is some code I got from elsewhere on the internet but I'm not sure how to proceed:
% Define x, f(x), and M(x)
x = linspace(6.8, 22)';
f = sin(x)+cos(x);
M = x.^2;
% Define the vertices: the points at (x, f(x)) and (x, 0)
N = length(x);
verts = [x(:), f(:), x(:) zeros(N,1)];
% Define the faces to connect each adjacent f(x) and the corresponding points at y = 0.
q = (1:N-1)';
faces = [q, q+1, q+N+1, q+N];
p = patch('Faces', faces, 'Vertices', verts, 'FaceVertexCData', [M(:); M(:)], 'FaceColor', 'interp', 'EdgeColor', 'none')
So I want the end result to be similar to the image attached below (note faint shading - I would like the gradient stronger), but with x = 1:24 and y = -1:1.
Since you ignored my request to include the results of your MATLAB code in your question, I had to look into my crystal ball to determine that your "messing around" resulted in the following error message:
Error using patch
While setting the 'Vertices' property of Patch:
Value must be a 1x2 or 1x3 vector of numeric type
Error in X (line 13)
p = patch('Faces', faces, 'Vertices', verts, 'FaceVertexCData', [M(:); M(:)], 'FaceColor', 'interp', 'EdgeColor', 'none')
This can be resolved by concatenating the vertices vertically instead of horizontally in line 8:
verts = [x(:), f(:); x(:), zeros(N,1)];
This yields the following plot:
This is of course not what you want.
Actual Solution
You can build up your background from 5 rectangles:
x = [0;
6; % 'First daylight'
6.8; % Full brightness
22; % Beginning of 'sunset'
22.8; % Complete darkness
24];
vertices = [repmat(x,2,1), [ones(size(x)); -1.*ones(size(x))]];
faces = [1:5; 2:6; 8:12; 7:11].';
color = repmat([0 0 0; % 'Night' color
0 0 0;
1 1 1; % 'Day' color
1 1 1;
0 0 0;
0 0 0],2,1);
patch('Faces',faces, ...
'Vertices',vertices, ...
'FaceVertexCData',color, ...
'FaceColor','interp', ...
'EdgeColor','red');
xlim([0 24]);
You make the vertices of the 'night' rectangles black and the vertices of the 'day' rectangle white by assigning the appropriate RGB values to the FaceVertexCData property.
The color of 'sunrise'/'sunset' rectangles is then interpolated between black and white by setting the FaceColor property to 'interp'.
The EdgeColor has only been set to 'red' to illustrate how the background is constructed. Set the property to 'interp' to make the lines disappear.

How to colorize individual bar in matlab? [duplicate]

I would like to draw a 3D histogram (with gnuplot or octave) in order to represent my data.
lets say that I have a data file in the following form:
2 3 4
8 4 10
5 6 7
I'd like to draw nine colored bars (the size of the matrix), in the set [1,3]x[1,3], such that the bar's color is proportional to the bar's height. How can I do this?
Below is a function I implemented that acts as a bar3 replacement (partially).
In my version, the bars are rendered by creating a patch graphics object: we build a matrix of vertex coordinates and a list of faces connecting those vertices.
The idea is to first build a single "3d cube" as a template, then replicate it for as many bars as we have. Each bar is shifted and scaled according to its position and height.
The vertices/faces matrices are constructed in a vectorized manner (look ma, no loops!), and the result is a single patch object drawn for all bars, as opposed to multiple patches one per bar (this is more efficient in terms of graphics performance).
The function could have been implemented by specifying coordinates of connected vertices that form polygons, by using the XData, YData, ZData and CData properties instead of the Vertices and Faces properties. In fact this is what bar3 internally does. Such approach usually requires larger data to define the patches (because we cant have shared points across patch faces, although I didn't care much about that in my implementation). Here is a related post where I tried to explain the structure of the data constructed by bar3.
my_bar3.m
function pp = my_bar3(M, width)
% MY_BAR3 3D bar graph.
%
% M - 2D matrix
% width - bar width (1 means no separation between bars)
%
% See also: bar3, hist3
%% construct patch
if nargin < 2, width = 0.8; end
assert(ismatrix(M), 'Matrix expected.')
% size of matrix
[ny,nx] = size(M);
% first we build a "template" column-bar (8 vertices and 6 faces)
% (bar is initially centered at position (1,1) with width=? and height=1)
hw = width / 2; % half width
[X,Y,Z] = ndgrid([1-hw 1+hw], [1-hw 1+hw], [0 1]);
v = [X(:) Y(:) Z(:)];
f = [
1 2 4 3 ; % bottom
5 6 8 7 ; % top
1 2 6 5 ; % front
3 4 8 7 ; % back
1 5 7 3 ; % left
2 6 8 4 % right
];
% replicate vertices of "template" to form nx*ny bars
[offsetX,offsetY] = meshgrid(0:nx-1,0:ny-1);
offset = [offsetX(:) offsetY(:)]; offset(:,3) = 0;
v = bsxfun(#plus, v, permute(offset,[3 2 1]));
v = reshape(permute(v,[2 1 3]), 3,[]).';
% adjust bar heights to be equal to matrix values
v(:,3) = v(:,3) .* kron(M(:), ones(8,1));
% replicate faces of "template" to form nx*ny bars
increments = 0:8:8*(nx*ny-1);
f = bsxfun(#plus, f, permute(increments,[1 3 2]));
f = reshape(permute(f,[2 1 3]), 4,[]).';
%% plot
% prepare plot
if exist('OCTAVE_VERSION','builtin') > 0
% If running Octave, select OpenGL backend, gnuplot wont work
graphics_toolkit('fltk');
hax = gca;
else
hax = newplot();
set(ancestor(hax,'figure'), 'Renderer','opengl')
end
% draw patch specified by faces/vertices
% (we use a solid color for all faces)
p = patch('Faces',f, 'Vertices',v, ...
'FaceColor',[0.75 0.85 0.95], 'EdgeColor','k', 'Parent',hax);
view(hax,3); grid(hax,'on');
set(hax, 'XTick',1:nx, 'YTick',1:ny, 'Box','off', 'YDir','reverse', ...
'PlotBoxAspectRatio',[1 1 (sqrt(5)-1)/2]) % 1/GR (GR: golden ratio)
% return handle to patch object if requested
if nargout > 0
pp = p;
end
end
Here is an example to compare it against the builtin bar3 function in MATLAB:
subplot(121), bar3(magic(7)), axis tight
subplot(122), my_bar3(magic(7)), axis tight
Note that I chose to color all the bars in a single solid color (similar to the output of the hist3 function), while MATLAB emphasizes the columns of the matrix with matching colors.
It is easy to customize the patch though; Here is an example to match bar3 coloring mode by using indexed color mapping (scaled):
M = membrane(1); M = M(1:3:end,1:3:end);
h = my_bar3(M, 1.0);
% 6 faces per bar
fvcd = kron((1:numel(M))', ones(6,1));
set(h, 'FaceVertexCData',fvcd, 'FaceColor','flat', 'CDataMapping','scaled')
colormap hsv; axis tight; view(50,25)
set(h, 'FaceAlpha',0.85) % semi-transparent bars
Or say you wanted to color the bars using gradient according to their heights:
M = 9^2 - spiral(9);
h = my_bar3(M, 0.8);
% use Z-coordinates as vertex colors (indexed color mapping)
v = get(h, 'Vertices');
fvcd = v(:,3);
set(h, 'FaceVertexCData',fvcd, 'FaceColor','interp')
axis tight vis3d; daspect([1 1 10]); view(-40,20)
set(h, 'EdgeColor','k', 'EdgeAlpha',0.1)
Note that in the last example, the "Renderer" property of the figure will affect the appearance of the gradients. In MATLAB, the 'OpenGL' renderer will interpolate colors along the RGB colorspace, whereas the other two renderers ('Painters' and 'ZBuffer') will interpolate across the colors of the current colormap used (so the histogram bars would look like mini colorbars going through the jet palette, as opposed to a gradient from blue at the base to whatever the color is at the defined height as shown above). See this post for more details.
I've tested the function in Octave 3.6.4 and 3.8.1 both running on Windows, and it worked fine. If you run the examples I showed above, you'll find that some of the advanced 3D features are not yet implemented correctly in Octave (this includes transparency, lighting, and such..). Also I've used functions not available in Octave like membrane and spiral to build sample matrices, but those are not essential to the code, just replace them with your own data :)
Solution using only functions available in OCTAVE, tested with octave-online
This solution generates a surface in a similar way to the internals of Matlabs hist3d function.
In brief:
creates a surface with 4 points with the "height" of each
value, which are plotted at each bin edge.
Each is surrounded by zeros, which are also plotted at each bin edge.
The colour is set to be based on the bin values and is applied to
the 4 points and the surrounding zeros. (so that the edges and tops of the 'bars' are coloured to match the "height".)
For data given as a matrix containing bin heights (bin_values in the code):
Code
bin_values=rand(5,4); %some random data
bin_edges_x=[0:size(bin_values,2)];
x=kron(bin_edges_x,ones(1,5));
x=x(4:end-2);
bin_edges_y=[0:size(bin_values,1)];
y=kron(bin_edges_y,ones(1,5));
y=y(4:end-2);
mask_z=[0,0,0,0,0;0,1,1,0,0;0,1,1,0,0;0,0,0,0,0;0,0,0,0,0];
mask_c=ones(5);
z=kron(bin_values,mask_z);
c=kron(bin_values,mask_c);
surf(x,y,z,c)
Output
I don't have access to Octave, butI believe this should do the trick:
Z = [2 3 4
8 4 10
5 6 7];
[H W] = size(Z);
h = zeros( 1, numel(Z) );
ih = 1;
for ix = 1:W
fx = ix-.45;
tx = ix+.45;
for iy = 1:W
fy = iy-.45;
ty = iy+.45;
vert = [ fx fy 0;...
fx ty 0;...
tx fy 0;...
tx ty 0;...
fx fy Z(iy,ix);...
fx ty Z(iy,ix);...
tx fy Z(iy,ix);...
tx ty Z(iy,ix)];
faces = [ 1 3 5;...
5 3 7;...
7 3 4;...
7 8 4;...
5 6 7;...
6 7 8;...
1 2 5;...
5 6 2;...
2 4 8;...
2 6 8];
h(ih) = patch( 'faces', faces, 'vertices', vert, 'FaceVertexCData', Z(iy,ix),...
'FaceColor', 'flat', 'EdgeColor','none' );
ih = ih+1;
end
end
view( 60, 45 );
colorbar;
I think the following should do the trick. I didn't use anything more sophisticated than colormap, surf and patch, which to my knowledge should all work as-is in Octave.
The code:
%# Your data
Z = [2 3 4
8 4 10
5 6 7];
%# the "nominal" bar (adjusted from cylinder())
n = 4;
r = [0.5; 0.5];
m = length(r);
theta = (0:n)/n*2*pi + pi/4;
sintheta = sin(theta); sintheta(end) = sqrt(2)/2;
x0 = r * cos(theta);
y0 = r * sintheta;
z0 = (0:m-1)'/(m-1) * ones(1,n+1);
%# get data for current colormap
map = colormap;
Mz = max(Z(:));
mz = min(Z(:));
% Each "bar" is 1 surf and 1 patch
for ii = 1:size(Z,1)
for jj = 1:size(Z,2)
% Get color (linear interpolation through current colormap)
cI = (Z(ii,jj)-mz)*(size(map,1)-1)/(Mz-mz) + 1;
fC = floor(cI);
cC = ceil(cI);
color = map(fC,:) + (map(cC,:) - map(fC,:)) * (cI-fC);
% Translate and rescale the nominal bar
x = x0+ii;
y = y0+jj;
z = z0*Z(ii,jj);
% Draw the bar
surf(x,y,z, 'Facecolor', color)
patch(x(end,:), y(end,:), z(end,:), color)
end
end
Result:
How I generate the "nominal bar" is based on code from MATLAB's cylinder(). One cool thing about that is you can very easily make much more funky-looking bars:
This was generated by changing
n = 4;
r = [0.5; 0.5];
into
n = 8;
r = [0.5; 0.45; 0.2; 0.1; 0.2; 0.45; 0.5];
Have you looked at this tutorial on bar3?
Adapting it slightly:
Z=[2 3 4
8 4 10
5 6 7]; % input data
figure;
h = bar3(Z); % get handle to graphics
for k=1:numel(h),
z=get(h(k),'ZData'); % old data - need for its NaN pattern
nn = isnan(z);
nz = kron( Z(:,k),ones(6,4) ); % map color to height 6 faces per data point
nz(nn) = NaN; % used saved NaN pattern for transparent faces
set(h(k),'CData', nz); % set the new colors
end
colorbar;
And here's what you get at the end:

Using Coordinate System in Matlab

I've been working on a project involving inverse source problem known within the electromagnetic wave field. The problem i have is that ; I have to define 3 points in a 2D space. These points should have a x,y coordinate of course and a value which will define its' current. Like this:
A1(2,3)=1
A2(2,-2)=2
and so on.
Also i have to define a circle around this and divide it into 200 points. Like the first point would be ; say R=2 ; B1(2,0) ;B50(0,2);B100(-2,0) and so on.
Now i really am having a hard time to define a space in MATLAB and circle it. So what i am asking is to help me define a 2D space and do it as the way i described. Thanks for any help guys!
This kind of code may be use. Look at grid in the Variable editor.
grid = zeros(50, 50);
R = 10;
angles = (1:200)/2/pi;
x = cos(angles)*R;
y = sin(angles)*R;
center = [25 20];
for n=1:length(angles)
grid(center(1)+1+round(x(n)), center(2)+1+round(y(n))) = 1;
end
You have to define a grid large enough for your need.
Here is a complete example that might be of help:
%# points
num = 3;
P = [2 3; 2 -2; -1 1]; %# 2D points coords
R = [2.5 3 3]; %# radii of circles around points
%# compute circle points
theta = linspace(0,2*pi,20)'; %'
unitCircle = [cos(theta) sin(theta)];
C = zeros(numel(theta),2,num);
for i=1:num
C(:,:,i) = bsxfun(#plus, R(i).*unitCircle, P(i,:));
end
%# prepare plot
xlims = [-6 6]; ylims = [-6 6];
line([xlims nan 0 0],[0 0 nan ylims], ...
'LineWidth',2, 'Color',[.2 .2 .2])
axis square, grid on
set(gca, 'XLim',xlims, 'YLim',ylims, ...
'XTick',xlims(1):xlims(2), 'YTick',xlims(1):xlims(2))
title('Cartesian Coordinate System')
xlabel('x-coords'), ylabel('y-coords')
hold on
%# plot centers
plot(P(:,1), P(:,2), ...
'LineStyle','none', 'Marker','o', 'Color','m')
str = num2str((1:num)','A%d'); %'
text(P(:,1), P(:,2), , str, ...
'HorizontalAlignment','left', 'VerticalAlignment','bottom')
%# plot circles
clr = lines(num);
h = zeros(num,1);
for i=1:num
h(i) = plot(C(:,1,i), C(:,2,i), ...
'LineStyle','-', 'Marker','.', 'Color',clr(i,:));
end
str = num2str((1:num)','Circle %d'); %'
legend(h, str, 'Location','SW')
hold off