How to get the x,y coordinates to extrude an image - matlab

I am trying to detect the edges of an image (2D image) and extrude the edges of an image (3D image). I can simply extrude a random x,y coordinates using
x = randi(100,1,5);
y = randi(100,1,5);
x = x([1:end 1]);
y = y([1:end 1]);
bw = poly2mask(x,y,100,100);
figure;
subplot(121);
plot(x,y)
[xg,yg] = meshgrid(1:100);
zg = bw * 2;
subplot(122);
surfl(xg,yg,zg)
I can also get the x,y values of the coordinates from the pixel image using the following coordinates
A=double(imread('F:\01.jpg'));
imshow(A)
[height, width] = size(A)
[x, y] = meshgrid(1:width, 1:height);
But I am unable to use this x,y values as the input x,y values for extrusion. Please help me. Thanks in advance.

Related

putting an image in the XY plane of a matlab 3D scatter graph

I am trying to put an image on the XY plane of a 3D scattergraph. The current version that I have is giving some results but the image's colours is not showing properly.
Currently, I read the image as a grayscale in, but ideally I would like to have my points above a colour image.
x = [1,2,3,4,5,6,7,8,9,10,11,1,2,3,4,5,6,7,8,9,10,11];
y = [0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1];
z = [0,0,0,1,2,3,2,1,0,0,0,0,0,1,2,3,2,1,0,0,0,0];
min_x = min(min(x));
min_y = min(min(y));
max_x = max(max(x));
max_y = max(max(y));
planeimg = rgb2gray(imread('1.jpg'));
figure; hold on;
scatter3(x,y,z);
colormap(gray);
minplaneimg = min(min(planeimg)); % find the minimum
scaledimg = (floor(((planeimg - minplaneimg) ./ ...
(max(max(planeimg)) - minplaneimg)) * 255)); % perform scaling
imgzposition = -10;
colorimg = ind2rgb(scaledimg,jet(256));
surf([min_x max_x],[min_y max_y],repmat(imgzposition, [2 2]),...
colorimg,'facecolor','texture')
view(45,30);
xlabel('x');
ylabel('y');
zlabel('z');

How to transform different shapes to circles in Matlab

I have this image:
that has different shapes, and I want to transform each shape in a circle. And each circle must have different radius, depending on the size of the shape. How can I do that? With Morphology Operations or there are any function on Matlab that does that?
I used the function Regionprops to detect every individual shape, then I can do operations on each region separately.
I would use bwlabel to first label all of the components. Then I would use regionprops to find the bounding box of each component. You can then use the rectangle with a Curvature value of [1 1] to plot an ellipse at each bounding box.
%// Load the image and convert to 0's and 1's
img = imread('http://i.stack.imgur.com/9wRYK.png');
img = double(img(:,:,1) > 0);
%// Label the image
L = bwlabel(img);
%// Compute region properties
P = regionprops(L);
imshow(img)
for k = 1:numel(P)
%// Get the bounding box
bb = P(k).BoundingBox;
%// Plot an ellipse around each object
hold on
rectangle('Position', bb, ...
'Curvature', [1 1], ...
'EdgeColor', 'r', ...
'LineWidth', 2);
end
If you actually want circles, you will need to decide how exactly you define a circle from a rectangle. For this, I just used the maximum of the width and height for the diameter.
t = linspace(0, 2*pi, 100);
cost = cos(t);
sint = sin(t);
for k = 1:numel(P)
bb = P(k).BoundingBox;
%// Compute the radius and center of the circle
center = [bb(1)+0.5*bb(3), bb(2)+0.5*bb(4)];
radius = max(bb(3:4)) / 2;
%// Plot each circle
plot(center(1) + radius * cost, ...
center(2) + radius * sint, ...
'Color', 'r');
end
Now if you actually want to modify the image data itself rather than simply displaying it, you can use a meshgrid of all of the pixel centers to test whether a given pixel is within a circle or not.
%// Create a new image the size of the old one
newImage = zeros(size(img));
%// Determine the x/y coordinates for each pixel
[xx,yy] = meshgrid(1:size(newImage, 2), 1:size(newImage, 1));
xy = [xx(:), yy(:)];
for k = 1:numel(P)
bb = P(k).BoundingBox;
%// Compute the circle that fits each bounding box
center = [bb(1)+0.5*bb(3), bb(2)+0.5*bb(4)];
radius = max(bb(3:4)) / 2;
%// Now check if each pixel is within this circle
incircle = sum(bsxfun(#minus, xy, center).^2, 2) <= radius^2;
%// Change the values of newImage
newImage(incircle) = k;
end
%// Create a binary mask of all points that were within any circle
mask = newImage > 0;

Selecting an ellipse as a region of interest (ROI)

I used imellipse to select an ellipse as my region of interest (ROI). The issue is that the ellipse I want to select is of around 45 degrees, and, when I use imellipse, it seems it is 90 degrees either horizontally or vertically.
How can I change the orientation of the ellipse?
Thanks.
You need to rotate the coordinates of an ellipse. Like this:
npts = 1e4;
t = linspace(0,2*pi,npts);
theta = pi/4;
aspect = [5 1]; % [x y]
x = aspect(1)*sin(t+theta);
y = aspect(2)*cos(t);
plot(x, y);
If you want to use imellipse to draw the ellipse on an image, you can extract the vertices and transform them:
figure, imshow('pout.tif');
h = imellipse;
exy = h.getVertices
theta = pi/12;
M = [cos(theta), sin(theta); -sin(theta), cos(theta)]
exy_centered = bsxfun(#minus,exy,mean(exy))
exyRot = bsxfun(#plus,exy_centered*M,mean(exy));
hold on
plot(exyRot(:,1),exyRot(:,2),'r') % orig: plot(exy(:,1),exy(:,2),'r')
To fill in the ellipse, creating a mask, use roifill or roipoly:
w=getfield(imfinfo('pout.tif'),'Width');
h=getfield(imfinfo('pout.tif'),'Height');
bw = roipoly(zeros(h,w),exyRot(:,1),exyRot(:,2));

on matlab, how can i plot centroid points on top of an rgb image?

i have a list of about 300 centroid points. these points are the centroids of the conncomps of a BW image that i have. Is there a way to plot the points of the centroids over the original rgb image?
No problem. Have this short script:
img = imread('rice.png');
bg = imopen(img,strel('disk',15));
img2 = img - bg;
mask = im2bw(img2, 0.19);
mask = bwareaopen(mask, 40);
cc = bwconncomp(mask, 4);
positionArray = regionprops(cc, {'Centroid'});
positionArray = struct2cell(positionArray);
positionArray = cellfun(#transpose, positionArray, 'UniformOutput',false);
positionArray = cell2mat(positionArray);
imshow(img);
hold on;
scatter(positionArray(1, :), positionArray(2, :), 200, 'g+');
You can vary the markersize and shape as you wish. The points in this case are stored as a 2 by n matrix with x coordinates in the first row ans y in the second.
First, the image itself is plotted using imshow. Then, scatter() is called. To put both items on the same set of axes, you have to call hold on.

How to change an image from Cartesian to Polar coordinates in Matlab?

I am trying to convert the pixels of an image from x-y coordinate to polar coordinate and I have problem with it, as I want to code the function by myself.
Here is the code I did so far:
function [ newImage ] = PolarCartRot
% read and show the image
image= imread('1.jpg');
%%imshow(image);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%change to polar coordinate
[x y z]= size(image);
r = sqrt(x*x+y*y);
theta = atan2(y,x);
for i =0:r
for j= 0:theta
newpixel = [i; j];
newImage(newpixel(1), newpixel(2),:) = image(i,j,:);
end
end
figure;
imshow (newImage);
It is not quite clear what you are trying to do, which is why I am making my own example...
So given an image, I am converting the pixel x/y coordinates from Cartesian to polar with CART2POL.
In the first figure, I am showing the locations of the points, and in the second, I plot both the original image and the one with polar coordinates.
Note that I am using the WARP function from the Image Processing Toolbox. Under the hood, it uses the SURF/SURFACE function to display a texture-mapped image.
% load image
load clown;
img = ind2rgb(X,map);
%img = imread(...); % or use any other image
% convert pixel coordinates from cartesian to polar
[h,w,~] = size(img);
[X,Y] = meshgrid(1:w,1:h);
[theta,rho] = cart2pol(X, Y);
Z = zeros(size(theta));
% show pixel locations (subsample to get less dense points)
XX = X(1:8:end,1:4:end);
YY = Y(1:8:end,1:4:end);
tt = theta(1:8:end,1:4:end);
rr = rho(1:8:end,1:4:end);
subplot(121), scatter(XX(:),YY(:),3,'filled'), axis ij image
subplot(122), scatter(tt(:),rr(:),3,'filled'), axis ij square tight
% show images
figure
subplot(121), imshow(img), axis on
subplot(122), warp(theta, rho, Z, img), view(2), axis square
EDIT
As I originally stated, the question is not clear. You have to describe the mapping you want in a well defined manner...
For one you need to think about where the origin is located before converting to polar coordinates. The previous example assume the origin to be the axes base at (0,0). Suppose you want to take the center of the image (w/2,h/2) as origin, then you would do this instead:
[X,Y] = meshgrid((1:w)-floor(w/2), (1:h)-floor(h/2));
with the rest of the code unchanged. To better illustrate the effect, consider a source image with concentric circles drawn in Cartesian coordinates, and notice how they map to straight lines in polar coordinates when using the center of the circles as origin:
EDIT
Here is another example of how to display an image in polar coordinates as requested in the comments. Note that we perform the mapping in the inverse direction pol2cart:
[h,w,~] = size(img);
s = min(h,w)/2;
[rho,theta] = meshgrid(linspace(0,s-1,s), linspace(0,2*pi));
[x,y] = pol2cart(theta, rho);
z = zeros(size(x));
subplot(121), imshow(img)
subplot(122), warp(x, y, z, img), view(2), axis square tight off
Again the effect is better show if you feed it an input image with straight lines, and see how they map in polar coordinates (vertical lines become circles, and horizontal lines become rays emanating from the origin):