Image to Polar Transformation - matlab

I need to transform a black/white image to polar coordinates. The original image is saved in a matrix. Right now, I'm iterating through each pixel of the original image and I calculate the polar transformation of each pixel like this:
originX = 0;
originY = 0;
for x = 1:columns
for y = 1:rows
r = sqrt((x-originX)^2 + (y-originY)^2);
a = atand((y-originY)/(x-originX));
polarTrans(r, a) = origImage(x,y);
end
end
The problem is, the calculated new positions for the pixels are NOT positive integer values, so I can't simply save them into another matrix polarTrans. Do you have any suggestions here? How else should I save the transformed image if not in a matrix?

Vectorize, my friend.
% compute all [x,y] pairs for the whole image
[x,y]=meshgrid(1:columns -Xorigin,1:rows -Yorigin);
% Ta-da!
[alpha,rho]=cart2pol(x,y)
Now pixel [i,j] is [x(i,j), y(i,j)] in cartesian coordinates and [rho(i,j),alpha(i,j)] in polar coordinates. You have no need of storing originImage in any other way. Whenever you want to know the polar coordinates of an specific pixel value, you just do [rho(i,j),alpha(i,j)], for originImage(i,j) pixel value.

Related

Find maximum intensity point from center in all directions in matlab matrix

I have images (matrix) in matlab and I look for the maximum intensity point from the center of the matrix in every direction to obtain edges. (I use the gradient of the image and I'm looking for a quadrilateral).
For M (n,m)
My first try was to consider one vector M(1:n/2, m/2), look for the maximum and rotate the image to find all maximums in all other directions.
But : the imrotate function causes many errors (crop or loose) and the reconstructed image doesn't correspond to the shape of the original one.
I tried also to consider vectors directly in the original image from center to all points in the perimeter... but it's not easy!
Do you have an idea to solve this ? Any subtlety in Matlab I don't know?
Thanks guy;
My actual code is
s_im = size(ima, 2)/2;
ima_max = zeros(size(ima));
ima_new = zeros(size(ima));
for a=0:359
im_r = imrotate(ima, a, 'crop');
c= floor(size(im_r,1)/2);
vect_h1 = im_r(c, 1:c);
l = length(vect_h1);
[~, id_h1] = max(vect_h1(:));
[x,y] = rotatePoint([id_h1, c], [c,c], deg2rad(a-180));
ima_max(floor(y), floor(x))= 1;
ima_new(floor(y), floor(x)) = 1;
An error is also that the center computed is not the same in all images...
I suppose you can use improfile to get the intensity along rays emitting from the center pixel:
sz = size(ima);
X = sz(2);
Y = sz(1);
all_end_points = cat(1, [ones(1,Y); 1:Y]', ...
[1:X; Y*ones(1,X)]', ...
[X*ones(1,Y); Y:-1:1]', ...
[X:-1:1; ones(1,X)]' );
cent = repmat( [X/2 Y/2], [size(all_end_points,1), 1]);
all_profs = improfile(ima, all_end_points(:,1), all_end_points(:,2));
Now you have all the profiles from the center, you can look for the max intensity along each.

how to converting pixel coordinates x and y of an image to an image in matlab?

I have a set of vectors x and y which contains the pixel coordinates of an image. And i need to convert those values to an image using Matlab. How can I use these values? Αnd which function is best suitable for it? The code i used is:
I = imread('D:\majorproject\image\characters\ee.jpg');
imshow(I)
BW =im2bw(I);
BW=imcomplement(BW);
imshow(BW)
dim = size(BW);
col = round(dim(2)/2)-90;
row = find(BW(:,col), 1 );
boundary = bwtraceboundary(BW,[row, col],'N');
imshow(I)
hold on;
plot(boundary(:,2),boundary(:,1),'0','LineWidth',3);
BW_filled = imfill(BW,'holes');
boundaries = bwboundaries(BW_filled);
for k=1:10
b = boundaries{k};
plot(b(:,2),b(:,1),'g','LineWidth',3);
end
from which i got the coordinate values.
Thank you.
Avoid using loops and consider using sub2ind to index into your output image instead. sub2ind will convert (x,y) coordinates into linear indices so that you can index what you want using a single command:
img = false(size(I));
img(sub2ind(size(I), y, x)) = true;
imshow(img);
Here x and y denote the column and row coordinates, assuming they start at 1. If x and y are row and column coordinates, simply swap the input parameters:
img = false(size(I));
img(sub2ind(size(I), x, y)) = true;
imshow(img);
Also, I is your image read in with imread. As you want to have an image that is the same dimensions as I, we can certainly make use of that fact to create your output image.
Alternatively, you can use sparse to directly index into the matrix and set the locations to 1, then convert back to a full logical matrix:
img = sparse(y, x, true, size(I,1), size(I,2));
%img = sparse(x, y, true, size(I,1), size(I,2)); %// Use this if x is row and y is column
img = full(img);
imshow(img);
Since, you have not provided the intensities of the pixels, I assume you want to create a binary image which you can do easily as-
img=[];
for k=1:length(x) //assuming the length of x and y are same
img(x(k),y(k))=1;
end
imshow(img);

MATLAB Boundingbox around object detected with Eigen Value Algorithm

I have an image of an object that I want to crop using the Eigen Value Algorithm, everything is fine until I want to draw a Bounding Box around the detected features to use as the area of significance.
original = imread('1.jpg');
img = rgb2gray(original);
corners = detectMinEigenFeatures(img);
figure;
imshow(original); hold on;
plot(corners.selectStrongest(4000));
%st = regionprops( corners.selectStrongest(4000), 'BoundingBox' );
%rect = st.BoundingBox;
crop = imcrop(original,rect);
figure
imshow(crop);
My problem is that the variable corners is (n x 1) and I don't know how that relates to coordinates in my original image.
your output corner is an object for storing corner points, use corner.Location to get an M-by-2 array of [x y] point coordinate.

Matlab - Trying to use vectors with grid coordinates and value at each point for a color plot

I'm trying to make a color plot in matlab using output data from another program. What I have are 3 vectors indicating the x-position, y-yposition (both in milliarcseconds, since this represents an image of the surroundings of a black hole), and value (which will be assigned a color) of every point in the desired image. I apparently can't use pcolor, because the values which indicate the color of each "pixel" are not in a matrix, and I don't know a way other than meshgrid to create a matrix out of the vectors, which didn't work due to the size of the vectors.
Thanks in advance for any help, I may not be able to reply immediately.
If we make no assumptions about the arrangement of the x,y coordinates (i.e. non-monotonic) and the sparsity of the data samples, the best way to get a nice image out of your vectors is to use TriScatteredInterp. Here is an example:
% samplesToGrid.m
function [vi,xi,yi] = samplesToGrid(x,y,v)
F = TriScatteredInterp(x,y,v);
[yi,xi] = ndgrid(min(y(:)):max(y(:)), min(x(:)):max(x(:)));
vi = F(xi,yi);
Here's an example of taking 500 "pixel" samples on a 100x100 grid and building a full image:
% exampleSparsePeakSamples.m
x = randi(100,[500 1]); y = randi(100,[500 1]);
v = exp(-(x-50).^2/50) .* exp(-(y-50).^2/50) + 1e-2*randn(size(x));
vi = samplesToGrid(x,y,v);
imagesc(vi); axis image
Gordon's answer will work if the coordinates are integer-valued, but the image will be spare.
You can assign your values to a matrix based on the x and y coordinates and then use imagesc (or a similar function).
% Assuming the X and Y coords start at 1
max_x = max(Xcoords);
max_y = max(Ycoords);
data = nan(max_y, max_x); % Note the order of y and x
indexes = sub2ind(size(data), max_y, max_x);
data(indexes) = Values;
imagesc(data); % note that NaN values will be colored with the minimum colormap value

How to assign values to particular point in 3D rectangle in matlab

I want to animate the varying temperature gradient in 3D rectangle. I have temperature values at specified points in a real container. I am not been able to figure out how to pass the temperature values as specified point in 3D container in Matlab.Lets say I have 10 points on one side of rectangle and same as on other remaining five sides.
any suggestions
Let's assume that your rectangular container is oriented in space with one vertex at (0,0,0) and sides along x, y and z axis. And you have set of points each with 3-point coordinate (x,y,z). In MATLAB it's probably represented by 3 vectors X, Y and Z. You also have a vector of temperature values (say T) for each points.
Then you can use SCATTER3 function to plot the points:
scatter3(X,Y,Z,[],T,'.')
You can change the size of points substituting the empty parameter with a value.
If you have point only on the faces of the container, it means one of the coordinate is either 0 or the size of corresponding side.
the colors are controlled by current color map. You can change it with COLORMAP function. For temperature the good one is 'hot' or 'cool'. Show the color scale with COLORBAR.
Here is an example with random data:
%# random coordinates
X = rand(60,1,1);
Y = rand(60,1,1);
Z = rand(60,1,1);
%# put the points into faces
X(1:10) = 0;
X(10:20) = 1;
Y(20:30) = 0;
Y(30:40) = 1;
Z(40:50) = 0;
Z(50:60) = 1;
%# temperature vector
T = rand(60,1,1) * 100;
%# plot
scatter3(X,Y,Z,[],T,'.')
grid off
box on
colormap hot
colorbar
Temp=zeros(10,10,10);
Temp(5,2,4)=25;