How translate a point on a circle? - matlab

I am trying to create a circle from the given radius and translate the circle.
edia = 10; %diameter
theta=linspace(0,2*pi, 100); %100 evenly spaced points.
radius = edia./2;
x = radius.*cos(theta);
y = radius.*sin(theta);
plot(x,y, 'k')
axis equal
axis([-edia, edia, -edia, edia]);
After creating a circle using the code, I have to translate it but I have no idea how to do it.
This is the circle that I have
and this is what I am suppose to get after translating
Thank you.

for this simple case, just add the shift directly.
close all
edia = 10; %diameter
theta=linspace(0,2*pi, 100); %100 evenly spaced points.
radius = edia./2;
x = radius.*cos(theta);
y = radius.*sin(theta);
plot(x,y, 'k')
axis equal
axis([-edia, edia, -edia, edia]);
newX=3; newY=4;
hold on;
plot(x+newX,y+newY, '-.')

Related

Pupil detection with hough transform

so I am working for my disertation thesis and I have to detect the pupil from images using Hough Transform. So far I wrote a code that identifies 2 circles on my image, but right now I have to keep the black circle from the pupil.
When I run the code, it identifies me the pupil, but also a random circle on the cheek. My professor said that I should calculate the pixels mean and, considering the fact that the pupil is black, to keep the pixels from only that region. I don't know how to do this.
I will let my code here to have a look and if someone has an ideea on how should I write this and keep only the black pixels would be great. I also attached to this the final image to see what I obtained.
close all
clear all
path='C:\Users\Ioana PMEC\OneDrive\Ioana personal\Disertatie\test.jpg';
%Citire imagine initiala
xx = imread(path);
figure
imshow(xx)
title('Imagine initiala');% Binarizarea imaginii initiale
yy = rgb2gray(xx);
figure
imshow(yy);
title('Imagine binarizata');
e = edge(yy, 'canny');
imshow(e);
radii = 11:1:30;
h = circle_hough(e, radii, 'same', 'normalise');
peaks = circle_houghpeaks(h, radii, 'nhoodxy', 15, 'nhoodr', 21, 'npeaks', 2);
imshow(yy);
hold on;
for peak = peaks
[x, y]=circlepoints(peak(3));
plot(x+peak(1), y+peak(2), 'r-');
end
hold off
testimage
finalimage
I implemented something which should get the task done for you. The example is done with the image you provided.
Step 1: Read the file(s) and convert them to grayscale.
path = %user input;
RGB = imread(path);
lab = rgb2lab(RGB);
grayscale_image = rgb2gray(RGB);
Step 2: Do the Hough transformation with given parameters.
These, and also the sensitivity can be adapted according to your task. Hint: Play around in the Image Segmenter toolbox for fast parameter finding. Next, the inferred circles are converted to integer values, since these are required for indexing.
min_radius = 10;
max_radius = 50;
% Find circles
[centers,radii,~] = imfindcircles(RGB,[min_radius max_radius],'ObjectPolarity','dark','Sensitivity',0.95);
centers = uint16(centers);
radii = uint16(radii);
The annotated image appears as follows:
Step 3: Get the brightness values of the circles.
From the circle center and radius values, we infer their respective brightness. It is sufficient to only check for the x and y pixel values left/right and above/below the center. (-1 is just a safety margin to completely stay within the circles.)
brightness_checker = zeros(2, max(radii), 2);
for i=1:size(centers,1)
current_radii = radii(i)-1;
for j=1:current_radii
% X-center minus radius, step along x-axis
brightness_checker(i, j, 1) = grayscale_image((centers(i,2) - current_radii/2) + j,...
(centers(i,1) - radii(i)/2) + j);
% Y-center minus radius, step along y-axis
brightness_checker(i, j, 2) = grayscale_image((centers(i,2) - current_radii/2) + j,...
(centers(i,1) - current_radii/2) + j);
end
end
Step 4: Check which circle is a pupil.
The determined value of 30 could potentially be enhanced.
median_x = median(brightness_checker(:,:,1),2);
median_y = median(brightness_checker(:,:,2),2);
is_pupil = (median_x<30)&(median_y<30);
pupils_center = centers(is_pupil == true,:);
Step 5: Draw the pupils.
The marker can be changed. Refer to:
https://de.mathworks.com/help/matlab/ref/matlab.graphics.chart.primitive.line-properties.html
figure
imshow(grayscale_image);
hold on
plot(centers(:,1), centers(:,2), 'r+', 'MarkerSize', 20, 'LineWidth', 2);
hold on
plot(pupils_center(:,1), pupils_center(:,2), 'b+', 'MarkerSize', 20, 'LineWidth', 2);
This is the final output:

Estimating an ellipse with a multi-variate Gaussian

In MATLAB, say I have the parameters for an ellipse:
(x,y) center
Minor axis radius
Major axis radius
Angle of rotation
Now, I want to generate random points that lie within that ellipse, approximated from a 2D gaussian.
My attempt thus far is this:
num_samps = 100;
data = [randn(num_samps, 1)+x_center randn(num_samps, 1)+y_center];
This gives me a cluster of data that's approximately centered at the center, however if I draw the ellipse over the top some of the points might still be outside.
How do I enforce the axis rules and the rotation?
Thanks.
my assumptions
x_center = h
y_center = k
Minor Axis Radius = b
Major Axis Raduis = a
rotation angle = alpha
h=0;
k=0;
b=5;
a=10;
alpha=30;
num_samps = 100;
data = [randn(num_samps, 1)+h randn(num_samps, 1)+k];
chk=(((((data(:,1)-h).*cos(alpha)+(data(:,2)-k).*sin(alpha))./a).^2) +...
(((data(:,1)-h).*sin(alpha)+(data(:,2)-k).*cos(alpha))./b).^2)<=1;
idx=find(chk==0);
if ~isempty(idx)
data(idx,:)=data(idx,:)-.5*ones(length(idx),2);
end

Drawing circles around points in a plot

I have two matrices
timeline = [0.0008 0.0012 0.0016 0.0020 0.0024 0.0028];
Origdata =
79.8400 69.9390 50.0410 55.5082 34.5200 37.4486 31.4237 27.3532 23.2860 19.3039
79.7600 69.8193 49.8822 55.3115 34.2800 37.1730 31.1044 26.9942 22.8876 18.9061
79.6800 69.6996 49.7233 55.1148 34.0400 36.8975 30.7850 26.6352 22.4891 18.5084
79.6000 69.5799 49.5645 54.9181 33.8000 36.6221 30.4657 26.2762 22.0907 18.1108
79.5200 69.4602 49.4057 54.7215 33.5600 36.3467 30.1464 25.9173 21.6924 17.7133
79.4400 69.3405 49.2469 54.5249 33.3200 36.0714 29.8271 25.5584 21.2941 17.3159
When I plot them, I get a graph like below.
plot(timeline, Origdata, '.');
How can I draw a circle of radius 0.3524 value around each point? This radius should be relative to the y-axis only.
You can do this easily using viscircles (which requires the Image Processing Toolbox) however I don't think that the output is actually what you're expecting.
radius = 0.3524;
dots = plot(timeline, Origdata, '.');
hold on
for k = 1:numel(dots)
plotdata = get(dots(k));
centers = [plotdata.XData(:), plotdata.YData(:)];
% Ensure the the colors match the original plot
color = get(dots(k), 'Color');
viscircles(centers, radius * ones(size(centers(:,1))), 'Color', color);
end
The reason that it looks like this is because your X data is very close together relative to your y data and for circles to appear as circles, I have forced the x and y scaling of the axes to be equal (axis equal)
Edit
If you only want the radius to be relative to the y axis (distance) then we actually need to draw ellipses with an x and y radius. We want to scale the "x-radius" to make it appear as a circle regardless of your true axes aspect ratio, something like this can actually do that.
The trick to the code below is setting the data and plot aspect ratios (pbaspect and daspect) to manual. This ensures that the aspect ratio of the axes doesn't change during zoom, resizing, etc and makes sure that our "circles" remain circular-looking.
dots = plot(timeline, Origdata, '.');
drawnow
% Force the aspect ratio to not change (keep the circles, as circles)
pbaspect('manual')
daspect('manual')
hold on
aspectRatio = daspect;
t = linspace(0, 2*pi, 100);
t(end+1) = NaN;
radius = 4.3524;
% Scale the radii for each axis
yradius = radius;
xradius = radius * aspectRatio(1)/aspectRatio(2);
% Create a circle "template" with a trailing NaN to disconnect consecutive circles
t = linspace(0, 2*pi, 100);
t(end+1) = NaN;
circle = [xradius*cos(t(:)), yradius*sin(t(:))];
for k = 1:numel(dots)
x = get(dots(k), 'XData');
y = get(dots(k), 'YData');
color = get(dots(k), 'Color');
% Center circle template at all points
circles = arrayfun(#(x,y)bsxfun(#plus, [x,y], circle), x, y, 'uni', 0);
circles = cat(1, circles{:});
plot(circles(:,1), circles(:,2), 'Color', color)
end
Just to demonstrate, if we increase the circle radius to 4.3524 we can see the circles better.
And this works with all resizing etc.
To draw circles in MATLAB, you obviously have to use the rectangle function ;)
As mentioned in my comment, the size of 0.3524 does not match your axis, so I chose different sizes to have the circles actually visible, These are rx and ry
timeline = [0.0008 0.0012 0.0016 0.0020 0.0024 0.0028];
Orgidata =[79.8400 69.9390 50.0410 55.5082 34.5200 37.4486 31.4237 27.3532 23.2860 19.3039
79.7600 69.8193 49.8822 55.3115 34.2800 37.1730 31.1044 26.9942 22.8876 18.9061
79.6800 69.6996 49.7233 55.1148 34.0400 36.8975 30.7850 26.6352 22.4891 18.5084
79.6000 69.5799 49.5645 54.9181 33.8000 36.6221 30.4657 26.2762 22.0907 18.1108
79.5200 69.4602 49.4057 54.7215 33.5600 36.3467 30.1464 25.9173 21.6924 17.7133
79.4400 69.3405 49.2469 54.5249 33.3200 36.0714 29.8271 25.5584 21.2941 17.3159];
ry=1;
rx=0.0001;
dots=plot(timeline, Orgidata , '.');
hold on
for ix=1:size(Orgidata ,1)
for jx=1:size(Orgidata ,2)
rectangle('Position',[timeline(ix)-(rx/2),Orgidata(ix,jx)-(ry/2),rx,ry],'Curvature',[1,1],'EdgeColor',get(dots(jx), 'Color'));
end
end

how to scale bounding box coordinates?

I'm extracting the outline of blob the following way:
bw = im2bw(image, threshold);
boundaries = bwboundaries(bw);
plot(boundaries(:, 2), boundaries(:, 1), 'k', 'LineWidth', 2);
what I would like to do now, is to scale boundaries so that I can plot a smaller version of the boundaries inside the original boundaries. Is there an easy way to do this?
Here's an example on what the result should look like: black is the original bounding box, and red is the same bounding box, just scaled (but with same center as black box).
EDIT:
I guess I can scale each point individually, but then I still have to recenter the coordinates. Is there a better way of doing this?
scale = 0.7
nbr_points = size(b, 1);
b_min = nan(nbr_points, 2);
for k = 1 : nbr_points
b_min(k, :) = ([scale 0; 0 scale] * b(k, 1:2)')';
end
Just creating a function which does this should be easy.
function scaledB = scaleBoundaries(B,scaleFactor)
% B is a cell array of boundaries. The output is a cell array
% containing the scaled boundaries, with the same center of mass
% as the input boundaries.
%%
for k = 1:length(B)
scaledB{k} = B{k} .* scaleFactor;
com = mean(B{k}); % Take the center of mass of each boundary
sCom = mean(scaledB{k}); % Take the center of mass of each scaled boundary
difference = com-sCom; % Difference between the centers of mass
% Recenter the scaled boundaries, by adding the difference in the
% centers of mass to the scaled boundaries:
scaledB{k}(:,1) = scaledB{k}(:,1) + difference(1);
scaledB{k}(:,2) = scaledB{k}(:,2) + difference(2);
end
end
Or did you want to avoid something unoptimized for speed purposes?

Generating a 3D plot by revolution of a curve

I'm trying to revolve a 2D curve to generate a 3D surface plot.
I've tried using
[X,Z,Y] = cylinder(u);
surf(X,Y,Z), axis square
this, however, revolves my curve around the wrong axis. How do I go about changing the axis?
Thanks alot.
To rotate the axis of the cylinder, you can simply change the order of X, Y, and Z.
[X,Y,Z] = cylinder(u);
surf(X,Y,Z) %# rotation around Z
surf(Z,X,Y) %# rotation around X
surf(Y,Z,X) %# rotation around Y
EDIT
To change the axis of rotation of your curve, you have to calculate the surface. For example, to rotate y = sin(alpha) with alpha = 0:0.1:pi around the y-axis, you can write
r = 0:0.1:pi;
z = sin(r);
theta = 0:pi/20:2*pi;
xx = bsxfun(#times,r',cos(theta));
yy = bsxfun(#times,r',sin(theta));
zz = repmat(z',1,length(theta));
dfig,surf(xx,yy,zz)
axis equal