Applying a vector field to image in matlab - matlab

How do I apply a vector field obtained via quiver, to an image which causes the pixels to displace in the direction of the vectors (image is warped)?
Also, if the vector field I have is 3 dimensional, how would I do this? Think of it as laying down a flat 2 dimensional image over a 3 dimensional terrain. How would I go about viewing this in matlab?
Thank you for your time
EDIT: I have to warp the image not just in the Z axis, but along the X and Y axes as well.

Laying down a flat 2 dimensional image over a 3 dimensional terrain:
It's not very clear the way the axes are oriented but this is an image of a clown mapped on the peaks function. Exact steps are described in the documentation of surface in the example 'Display image along surface plot.'
load clown
C = flipud(X);
figure
surface(XD,YD,ZD,C,...
'FaceColor','texturemap',...
'EdgeColor','none',...
'CDataMapping','direct')
colormap(map)
view(-35,45)
Essentially, you create your surface with CData as the image you want to be displayed and set an appropriate colormap for the axes.

Use the imwarp function in the Image Processing Toolbox.

Related

Cell colony survival mapping in a particular spatial pattern

I am attempting to spatially map the cell survival in a given scanned image of a cell flask. Quick background: the cells have received a high dose of irradiation (protons/X-rays) delivered through a grid so that some regions are covered from the irradiation, whereas other regions are not. After scanning such cell colonies, the images are then fed into a segmentation algorithm (in which I have developed using Matlab), centroid coordinates (c_i = (x_i,y_i)) of each detected viable colony are provided.
I have done this type of assessment for grid ‘stripes’, where I have counted colonies within a band along a single dimension (x) and tested for different band widths Δx (as shown in the left figure below). However, my issue is for grid ‘holes’ (see right figure below) – how can I perform the same type of assessment for cell colony survival in two dimensions (x and y) given the centroid coordinates? Do I have to “think” radially?
Thank you in advance for any guidance or help to this problem.
You are in the right direction. In the left side image the variation is along x-axis and you are using a new axis for plating efficiency (y-axis).
Similarly, for grid - you will have to introduce a new axis : z axis. Suppose your image I is 500x500 and each grid-cell is 50x50. So you will create a 10x10 grid G where each cell of G is count of centroids in one 50x50 grid cell of I.
Since visualizing a 3-D chart is difficult, people use images, where the value of z-axis is the intensity in image or the grayscale value of a grayscale image. Make sure to normalize your z-axis values on [0,1] or [0,255] range for using images as your visualization tool.

plotting 3D edge in matlab

I have a 3D matrix of a MRI image and used matlab edge function and it gave me a 3D matrix as follow which some of the points are 1 (means edges).
I want to show this surface in matlab but I don't know that how I should do this. I know that I should use surf.
As #bdecaf said, you can use find to determine the height of the points, or in other words, in which of the 100 layers does the point lie. You can do that as follows:
z1=zeros(30,100);
temp=find(b);
[row,col,layer]=ind2sub(size(b),temp);
for i=1:size(x,1)
z1(row(i),col(i))=layer(i);
end
You can get an image as follows:

Surface plot on top of colour image (x-y plane)

I guess, I've got an colormap issue with Matlab2013. I plot a 3d surface plot with colormap hot and I would like to have in the same plot an bitmap (8bit colour image) on the x-y plane. Plotting both separately from each other works fine but as soon as I plot them in one figure the first surface plot is only black. I guess it is because the RGB image on the x-y plane uses a different colour map. Is there an option in Matlab to plot two different type of images in the same plot?
surf(X,Y,density,'FaceColor','texturemap','Edgecolor','none')
colormap hot
...
%// define the location of the bitmap
xImage = [miX maX; miX maX]; %// The y data for the image corners
yImage = [miY miY; maY maY]; %// The x data for the image corners
zImage = [zDist zDist; zDist zDist]; %// The z data for the image corners
surf(xImage,yImage,zImage,... %// Plot the surface
'CData',RGBImage,...
'FaceColor','texturemap');
Thanks!
Durin
I think this is an issue with the relative scaling of density and zImage. I can replicate this by doing the following:
1) Plot a surf where the third input is n x m which is scaled like some real data (-0.2 to +0.2, for example). This responds to changes in colormap as you'd expect.
2) After hold on, plot another surf where the third input is n x m x 3, like a RGB image, double values scaled between 0 and 1.
This causes the first image to go "dark" (or whatever the lowest color in that particular colormap is). The issue is that they share their CLim, being in the same axis, although the RGBImage doesn't, in fact, reference the colormap.
This is "fixable" by scaling/normalising the first plot (your density values) to be between 0 and 1 (in this case) - although this quick fix is going to give you issues if you then want to add a colorbar. Alternatively, first grab the "CLim" from your axis after plotting the first surf:
trueC = get(gca,'CLim');
Then set it back after you plot the image:
set(gca,'CLim',trueC)

Matlab - plot image gradients using vector representation

I have computed the gradients from every pixel location of a grayscale image, both on X and Y axis and this can result in a vector representation for each pixel location. I want to obtain a plot figure similar to the one illustrated bellow:
My image has 1000 x 1002 dimensions and I have computed the gradients for each pixel on X and Y directions so I have 2 matrices, each one having 1000 x 1002 dimensions.
I am interested in obtaining a plot similar to the one illustrated in the image above, where I show basically the direction of each vector obtained from the computed gradients. I do not care about the magnitude of the vector, so basically each arrow can have the same length.
Do you know how can I obtain something similar to this?
It works in my case:
[DX,DY] = imgradient(imageIn);
%show gradient
figure;
[x,y]=meshgrid(1:1:500);
figure
quiver(x,y,DX,DY)
hold off

Matlab 3d plot of indexed data

I am trying to plot a 3d view of a very large CT dataset. My data is in a 3d matrix of 2000x2000x1000 dimension. The object is surrounded by air, which is set to NaN in my matrix.
I would like to be able to see the greyscale value of the surface of the object (no isosurface) but I cannot quite work out how to do that in Matlab. Can anyone help me please?
Given that I a dealing with a huge matrix and I am only interested in the surface of the object, does anyone know a good trick how to reduce the size of my dataset?
The function surf(X,Y,Z) allows you to plot 3d data, where (X,Y) gives the coordinates in the x-y-plane while Z gives the z-coordinate and the surface color.
By default the function does not plot anything for the NaN entries, so you should be good to go with the surf function.
To set the surf-function to use a grayscale plotting use:
surf(matrix3d);
colormap(gray);
This plots the matrix in a surface plot and sets the colormap to grayscale.
In addition, as I understand your data, you might be able to eliminate entire plane-segments in your matrix. If for instance the plane A(1,1:2000,1:1000) is NaN in all entries you could eliminate all those entries (thus the entire Y,Z-plane in entry X=1). This will however require some heavy for loops, which might be over the top. This depends on how many data matrices you have compared to how many different plot you want for each matrix.
I will try to give you some ideas. I assume lack of a direct 3D "surface detector".
Since you have a 3D matrix where XY-planes are CT scan slices and each slice is an image, I would try to find edges of each slice say with edge. This would require some preprocessing like first thresholding each slice image. Then I can either use scatter3 to display the edge data as a 3D point cloud or delaunay3 to display the edge data as a surface.
I hope this will help you achieve what you are asking for.
I managed to get it working:
function [X,Y,Z,C] = extract_surface(file_name,slice_number,voxel_size)
LT = imread(file_name);%..READ THE 2D MAP
BW = im2bw(LT,1);%..THRESHOLD TO BINARY
B = bwboundaries(BW,8,'noholes');%..FIND THE OUTLINE OF THE IMAGE
X = B{1}(:,1);%..EXTRACT X AND Y COORDINATES
Y = B{1}(:,2);
indices = sub2ind(size(LT),X,Y);%..FIND THE CORRESPONDING LINEAR INDICES
C = LT(indices);%..NOW READ THE VALUES AT THE OUTLINE POSITION
Z = ones(size(X))*slice_number;
I can then plot this with
figure
scatter3(X,Y,Z,2,C)
Now the only thing I could improve is to have all these points in the scatter plot connected with a surface. #upperBound you suggested delaunay3 for this purpose - I cannot quite figure out how to do this. Do you have a tip?