Line fitting using RANSAC - matlab

I am doing a project in image processing, basically to Vectorise hand drawn images using image processing techniques.
I am using RANSAC in my project. The challenge that I am facing is that the algorithm does not perform the best fit as required but
it uses any two random points and draws a line that joins them as shown in the image below.
RANSAC results
In my algorithm to Vectorise hand drawn images, I also did Grey-scaling, Image thresholding (Image Binarization),
and Skeletonization using Morphological Operators.
I am using MATLAB for my project.
The following is the code I have done so far
% Line fitting using RANSAC
[x, y] =size(skeleton_image);
point =[];
count =1;
% figure; imshow(~data); hold on
for n =1:x
for m =1:y
if skeleton_image(n,m)==1
point(count,1)=m;
point(count,2)=n;
count= count+1;
end
end
end
data = point';
number = size(data,2); % Total number of points
X = 1:number;
iter=100; num=2; thresh = 1000;count_inlines=103; best_count=0; best_line=[];
for i=1:iter
% Randomly select 2 points
ind = randi(number,num); % randperm(number,num);
rnd_points= data(:,ind);
% Fitting line
Gradient = (rnd_points(2,2)-rnd_points(2,1))/(rnd_points(1,2)-rnd_points(1,1));
Constant = rnd_points(2,1)-Gradient*rnd_points(1,1);
Line = Gradient*X+Constant; [j,k]=size(Line);
% How many pixels are in the line?
for i=1:number
Distance = sqrt((Line(:,i)-data(1,i)).^2)+(Line(:,i)-data(2,i)).^2);
if Distance<=thresh
inlines = data(:,i);
count_inlines=countinlines+1;
best_line=Line;
end

I think your issue might be in the way you are counting the distance and/or the threshold that is currently 1000. It might choose all the points in any case and just pick the first or the last ransac line.
% Line fitting using RANSAC
%create skeleton_image objects
skeleton_image = zeros(50,50);
% draw a circle
circle_center = [15,15];
radius = 6;
for i=1:50
for j = 1:50
if abs( radius - sqrt( (i-circle_center(1))^2 + (j-circle_center(2))^2 ) ) <0.5 % < controls the thickness of the circle
skeleton_image(i,j) = 1;
endif
end
end
% draw a line
grad=0.5;
dy = 20;
for i=10:50
skeleton_image(ceil(dy + grad*i),i)=1;
if (i < 50)
skeleton_image(ceil(dy + grad*i)+1,i)=1;
endif
end
% a handful of random points to make it more realistic
skeleton_image(20,22)=1;
skeleton_image(30,7)=1;
skeleton_image(18,45)=1;
skeleton_image(10,10)=1;
skeleton_image(20,23)=1;
skeleton_image(31,6)=1;
skeleton_image(19,45)=1;
skeleton_image(9,13)=1;
skeleton_image(20,24)=1;
skeleton_image(31,5)=1;
skeleton_image(18,46)=1;
% [x, y] =size(skeleton_image);
x = 50;
y = 50;
points =[];
count =1;
for n =1:x
for m =1:y
if skeleton_image(n,m)==1
points(count,1)=m;
points(count,2)=n;
count= count+1;
end
end
end
best_line = [];
best_count = 0;
line_point_list = [];
% how close the pixel has to be to the line to be accepted
threshold = 1;
% how many samples are taken
steps = 10;
for i=1:steps
% pick two points
ind1 = randi(number,1);
ind2 = randi(number,1);
point1 = points(ind1,:);
point2 = points(ind2,:);
%auxiliaries
line = [point1;point2];
lpl = []; %line_point_list
count_i = 0;
if point1 != point2
vector1 = point2-point1;
% unit vector
vector1_normalized = vector1 ./ norm(vector1);
% normal direction of the line
normal_of_vector1 = [vector1_normalized(2), -vector1_normalized(1)];
% loop over points
for j = 1:size(points)
% calculate distance
normal_of_vector1;
vector2 = points(j,:) - point1;
distance = abs(dot(vector2, normal_of_vector1));
if ( distance < threshold )
count_i +=1;
lpl(count_i,:) = points(j,:);
endif
end
endif
if ( count_i > best_count)
best_count = count_i;
best_line = line;
line_point_list = lpl;
endif
end
%best_c
%best_l
%line_point_list
% draw found points
for i=1:size(line_point_list)
skeleton_image(line_point_list(i,2),line_point_list(i,1) ) = 0.25;
end
%visualize
figure(1)
imshow(skeleton_image)

Related

Buffon's needle in MATLAB

I am currently working on a project for my Chemical Engineering class called Buffon's needle. The purpose of this project is to use MATLAB to get an estimate for pi and then to make a "cartoon" which will show the needles on a 10x10 graph with lines every 1 unit apart, with needles crossing the line being one color, and needles not crossing being another. I have found the pi estimate and i have created the graph, but my lines are not the one unit in length like they should be, instead the needles are all different lengths. if anyone could help me with this problem it would be much appreciated. my two scripts are below
clear all;
close all;
clc;
format compact
% Script to illustrate the estimation of pi value by using Buffon's needle
% experiment
% set number of separate experiments
nExperiments = 1000;
% set number of separate trials
nTrials = 3;
% total number of dropped needles is directly based on number of trials and
% number of experiments
ndropped=nTrials.*nExperiments;
% spacing between the parallel lines
spacing = 1;
% length of the needle
L = spacing;
% the lower bound of x coordinate.
a = 10;
totalhits = 0;
for i = 1:nTrials
% keeps track of the number of hits
hits = 0;
% keeps track of the number of times the needle doesn't hit the
% any of the lines
nothits = 0;
for j = 1:nExperiments
[outcome,endpoints,angle] = needle_drop(spacing,L);
if outcome
hits = hits + 1;
endpointsHitList(:,:,hits) = endpoints;
else
nothits = nothits + 1;
endpointsNotHitList(:,:,nothits) = endpoints;
end
angleList(j) = angle;
end
scatter(1:nExperiments,angleList);
xlabel('Experiments');
ylabel('Angles');
piestimate(i) = (2*L/spacing)/(hits/nExperiments);
end
fprintf('The average value of pi is %f plus or minus %f after %d trials of %d experiments with %d total number of dropped needle.\n',mean(piestimate),std(piestimate),nTrials,nExperiments,ndropped);
figure
hold on
% plot the vertical separations
for i = 0:spacing:a
p1 = plot([i,i],[0 11],'k','LineWidth',2);
end
% plot the needles that hit the vertical separation
for i = 1:hits
p2 = plot(endpointsHitList(:,1,i),endpointsHitList(1,:,i),['-','b']);
end
% plot the needles that don't hit the vertical separation
for i = 1:nothits
p3 = plot(endpointsNotHitList(:,1,i),endpointsNotHitList(1,:,i),['-','r']);
end
axis([-2,12 -2 12]);
legend([p1 p2 p3],'Vertical Separations','Hits','Not Hits')
title('Buffon Needle Experiment');
xlabel('x-axis');
ylabel('y-axis');
figure
histogram(piestimate)
title('Histogram of pi estimate');
This below is my function needle_drop:
function [outcome,endpoints,theta] = needle_drop(spacing,L)
% spacing = spacing between the parallel lines spacing
% L = length of the needle
% spacing = 1;
% % In the special case where the length of the needle is equal to the grid spacing
% % between the parallel lines
% L = spacing;
% a is the lower bound of x coordinate. b is the upper bound.
% the needle position will be randomly between 0 and 10.
a = 0;
b = 10;
% generate random number r1 in [0,1]
r1 = rand;
% sample a value of the angle uniformly distributed over the interval
% from zero to ninety degrees
theta = (pi/2)*r1;
% the projection of half the length of the needle horizontally: S
S = (L/2)*cos(theta);
% Another random number r2 is generated
% this corresponds to x,y coordinate
r2 = a + (b-a).*rand(1,2);
% we need to take care of the offset.
% if the x coordinate is between 0 and d then offset is 0 if xcord is
% between d and 2d then offset is d and likewise for other values.
offset = floor(r2(1));
% The sampled position T of the center of the needle is next compared to the
% sampled projection of half the length of the needle
if r2(1)-S <=0+offset || r2(1)+S >=spacing+offset
outcome = 1;
else
outcome = 0;
end
% the projection of half the length of the needle vertically: V
V = L/2*sin(theta);
endpoints = [r2(1)-S,r2(2)+V;r2(1)+S,r2(2)-V];
You made an indexing mistake. Your function returns endpoints:
endpoints = [ r2(1)-S, r2(2)+V; ...
r2(1)+S, r2(2)-V ];
Simplified,
endpoints = [ start_x, start_y; ...
end_x, end_y ];
These are collected in a 3D matrix, which you then plot:
p2 = plot( endpointsHitList(:,1,i), endpointsHitList(1,:,i), ['-','b'] );
% ^ x-coordinates ^ y-coordinates
Thus, here you are plotting a line with x-coordinates [start_x,end_x], and y-coordinates [start_x,start_y]! This latter should have been [start_y,end_y].
This should have been:
p2 = plot( endpointsHitList(:,1,i), endpointsHitList(:,2,i), ['-','b'] );
% ^^^ get second column
The same mistake happens when plotting endpointsNotHitList.

Looping my algorithm to plot for a different parameter value on the same graph(MATLAB)

I've implemented an algorithm for my physics project which does exactly what I want. The problem that I'm stuck which is not the Physics content itself hence I think it might be somewhat trivial to explain what my code does. I'm mainly stuck with the way MATLAB's plotting works if I was to loop over the same algorithm to produce similar graphs with a slight change of a value of my parameter. Here's my code below:
clear; clc; close all;
% Parameters:
z_nn = 4; % Number of nearest-neighbour in lattice (square = 4).
z_nnn = 4; % Number of next-nearest-neighbours in lattice (square = 4).
Lx = 40; % Number of sites along x-axis.
Ly = 40; % Number of sites along y-axis.
sigma = 1; % Size of a site (defines our units of length).
beta = 1.2; % Inverse temperature beta*epsilon.
mu = -2.53; % Chemical potential mu/epsilon.
mu_2 = -2.67; % Chemical potential mu/epsilon for 2nd line.
J = linspace(1, 11, 11);%J points for the line graph plot
potential = zeros(Ly);
attract = 1.6; %wall attraction constant
k = 1; %wall depth
rho_0 = 0.4; % Initial density.
tol = 1e-12; % Convergence tolerance.
count = 30000; % Upper limit for iterations.
alpha = 0.01; % Mixing parameter.
conv = 1; cnt = 1; % Convergence value and counter.
rho = rho_0*ones(Ly); % Initialise rho to the starting guess(i-th rho_old) in Eq(47)
rho_rhs = zeros(Ly); % Initialise rho_new to zeros.
% Solve equations iteratively:
while conv>=tol && cnt<count
cnt = cnt + 1; % Increment counter.
% Loop over all lattice sites:
for j=1:Ly
%Defining the Lennard-Jones potential
if j<k
potential(j) = 1000000000;
else
potential(j) = -attract*(j-k)^(-3);
end
% Handle the periodic boundaries for x and y:
%left = mod((i-1)-1,Lx) + 1; % i-1, maps 0 to Lx.
%right = mod((i+1)-1,Lx) + 1; % i+1, maps Lx+1 to 1.
if j<k+1 %depth of wall
rho_rhs(j) = 0;
rho(j) = 0;
elseif j<(20+k)
rho_rhs(j) = (1 - rho(j))*exp((beta*((3/2)*rho(j-1) + (3/2)*rho(j+1) + 2*rho(j) + mu) - potential(j)));
else
rho_rhs(j) = rho_rhs(j-1);
end
end
conv = sum(sum((rho - rho_rhs).^2)); % Convergence value is the sum of the differences between new and current solution.
rho = alpha*rho_rhs + (1 - alpha)*rho; % Mix the new and current solutions for next iteration.
end
% disp(['conv = ' num2str(conv_2) ' cnt = ' num2str(cnt)]); % Display final answer.
% figure(2);
% pcolor(rho_2);
figure(1);
plot(J, rho(1:11));
hold on;
% plot(J, rho_2(1,1:11));
hold off;
disp(['conv = ' num2str(conv) ' cnt = ' num2str(cnt)]); % Display final answer.
figure(3);
pcolor(rho);
Running this code should give you a graph like this
Now I want to produce a similar graph but with one of the variable's value changed and plotted on the same graph. My approach that I've tried is below:
clear; clc; close all;
% Parameters:
z_nn = 4; % Number of nearest-neighbour in lattice (square = 4).
z_nnn = 4; % Number of next-nearest-neighbours in lattice (square = 4).
Lx = 40; % Number of sites along x-axis.
Ly = 40; % Number of sites along y-axis.
sigma = 1; % Size of a site (defines our units of length).
beta = 1.2; % Inverse temperature beta*epsilon.
mu = [-2.53,-2.67]; % Chemical potential mu/epsilon.
mu_2 = -2.67; % Chemical potential mu/epsilon for 2nd line.
J = linspace(1, 10, 10);%J points for the line graph plot
potential = zeros(Ly, length(mu));
gamma = zeros(Ly, length(mu));
attract = 1.6; %wall attraction constant
k = 1; %wall depth
rho_0 = 0.4; % Initial density.
tol = 1e-12; % Convergence tolerance.
count = 30000; % Upper limit for iterations.
alpha = 0.01; % Mixing parameter.
conv = 1; cnt = 1; % Convergence value and counter.
rho = rho_0*[Ly,length(mu)]; % Initialise rho to the starting guess(i-th rho_old) in Eq(47)
rho_rhs = zeros(Ly,length(mu)); % Initialise rho_new to zeros.
figure(3);
hold on;
% Solve equations iteratively:
while conv>=tol && cnt<count
cnt = cnt + 1; % Increment counter.
% Loop over all lattice sites:
for j=1:Ly
for i=1:length(mu)
y = 1:Ly;
MU = mu(i).*ones(Ly)
%Defining the Lennard-Jones potential
if j<k
potential(j) = 1000000000;
else
potential(j) = -attract*(j-k).^(-3);
end
% Handle the periodic boundaries for x and y:
%left = mod((i-1)-1,Lx) + 1; % i-1, maps 0 to Lx.
%right = mod((i+1)-1,Lx) + 1; % i+1, maps Lx+1 to 1.
if j<k+1 %depth of wall
rho_rhs(j) = 0;
rho(j) = 0;
elseif j<(20+k)
rho_rhs(j) = (1 - rho(j))*exp((beta*((3/2)*rho(j-1) + (3/2)*rho(j+1) + 2*rho(j) + MU - potential(j)));
else
rho_rhs(j) = rho_rhs(j-1);
end
end
end
conv = sum(sum((rho - rho_rhs).^2)); % Convergence value is the sum of the differences between new and current solution.
rho = alpha*rho_rhs + (1 - alpha)*rho; % Mix the new and current solutions for next iteration.
disp(['conv = ' num2str(conv) ' cnt = ' num2str(cnt)]); % Display final answer.
figure(1);
pcolor(rho);
plot(J, rho(1:10));
end
hold off;
The only variable that I'm changing here is mu. I would like to loop my first code so that I can enter an arbitrary amount of different values of mu and plot them on the same graph. Naturally I had to change all of the lists dimension from (1 to size of Ly) to (#of mu(s) to size of Ly), such that when the first code is being looped, the i-th mu value in that loop is being turned into lists with dimension as long as Ly. So I thought I would do the plotting within the loop and use "hold on" encapsulating the whole loop so that every plot that was generated in each loop won't be erased. But I've been spending hours on trying to figure out the semantics of MATLAB but ultimately I can't figure out what to do. So hopefully I can get some help on this!
hold on only applies to the active figure, it is not a generic property shared among all figures. What is does is changing the value of the current figure NextPlot property, which governs the behavior when adding plots to a figure.
hold on is equivalent to set(gcf,'NextPlot','add');
hold off is equivalent to set(gcf,'NextPlot','replace');
In your code you have:
figure(3); % Makes figure 3 the active figure
hold on; % Sets figure 3 'NextPlot' property to 'add'
% Do some things %
while conv>=tol && cnt<count
% Do many things %
figure(1); % Makes figure 1 the active figure; 'hold on' was not applied to that figure
plot(J, rho(1:10)); % plots rho while erasing the previous plot
end
You should try to add another hold on statement after figure(1)
figure(1);
hold on
plot(J, rho(1:10));

Insert random noise in a V slope DEM

With the following code I am generating a V plane with 2 different slopes, 10° and 20° respectively.
% /*
% Assumptions
% */
% resolution [m]
res = 1;
% inclination [deg]
i1 = 10; i2 = 20;
% /*
% DEM -> V shape
% */
% pre-allocate output
testDEM = zeros(513);
% required elevation step [m]
hstep = res*tan(i1*(pi/180));
% elevation start right [m]
k = 513*(2/3)*tan(i1*(pi/180));
% coordinates
q = length(1:513*(2/3));
% initialize
nStep = 0;
for jj = 1:q
testDEM(:,jj) = k-nStep;
nStep = nStep + hstep;
end
% change elevation step
step = res*tan(i2*(pi/180));
% update nStep
nStep = step;
% elevation start left [m]
start = testDEM(end,q);
for jj = q+1:513
testDEM(:,jj) = start + nStep;
nStep = nStep + step;
end
testDEM = testDEM(1:507,1:507);
%//Plot test DEM
f_tSlope = figure();
set(gca,'PlotBoxAspectRatio',[1 1 1]);
surf(testDEM, 'EdgeColor', 'none')
colormap jet;
hb = colorbar('location','eastoutside');
hb.Label.String = '[m]';
hb.Label.Rotation = 0;
hb.Label.HorizontalAlignment = 'Left';
With the following I'm adding noise in every location
sigma = 1;
testDEM = testDEM + sigma*randn(size(testDEM));
But what I'd like instead is to add random noise in random location, not everywhere. How can I do it?
Thanks in advance
How about this:
N_locations = 100; % no. of locations to add random noise
% randomize 'N_locations' linear indecies in 'testDEM':
noise_location = randi(numel(testDEM),N_locations,1);
% add the noise:
testDEM(noise_location) = testDEM(noise_location)+sigma*randn(N_locations,1);
This will randomize N_locations random locations on the map, and apply different random noise to each of them.
If you prefer to add the same noise to all random locations, just write sigma*randn, without the parenthesis after it.
For small N_locations this should suffice. However, if you want to make sure you don't pick the same location twice, or N_locations is large, you can set noise_location like this:
noise_location = randperm(numel(testDEM),N_locations);
so you'll have only non-repeating values of indices in testDEM.
This code adds noise with 0.5 probability
testDEM = testDEM + sigma*randn(size(testDEM)) .* (rand(size(testDEM)) > 0.5);

finding peak start and end points

I have a series of peaks Peaks image, I used matlab findpeaks to find the peak points .
I need to find peak widths and the peak start and end points ? I have started
with this code , but it is not giving me the right width calculation :
% If the peak is at index 150
% Scan to the right.
for k = 151:length(signal)
if signal(k) < signal(k-1)
% Signal is starting to fall.
rightIndex = k-1;
break;
end
end
% Scan to the left.
for k = 149: -1 : 1
if signal(k) < signal(k+1)
% Signal is starting to fall.
leftIndex = k+1;
break;
end
end
peakWidth = rightIndex - leftIndex;
here there are two kinds of widths, on computed by findpeaks and one computed naively by comparing derivative to zero:
% generate sinal
x = 0:0.1:10;
y = x.*sin(x/5).*sin(5*x.^2);
% get derivatives
dy = diff(y);
dx = diff(x);
dy_dx = [0 dy./dx];
% plot default annotated peaks
subplot(211);
findpeaks(y,x,'Annotate','extents');
% get peaks with width computed by 'findpeaks'
[pks,locs,peakWidth1,p] = findpeaks(y,x);
subplot(212);
plot(x,y);
hold on
plot(locs,pks,'*m')
% compute starting and ending points
startpoint = zeros(size(pks));
endpoint = zeros(size(pks));
for ii = 1:length(pks)
plot(locs(ii) + peakWidth1(ii)*[-.5 .5],pks(ii) - p(ii)/2 + [0 0],'y')
sp = find((x < locs(ii)) & (dy_dx <= 0),1,'last');
if isempty(sp)
sp = 1;
end
startpoint(ii) = sp;
ep = find((x > locs(ii)) & (dy_dx >= 0),1,'first') - 1;
if isempty(ep)
ep = length(x);
end
endpoint(ii) = ep;
plot(x(startpoint(ii)),y(startpoint(ii)),'og')
plot(x(endpoint(ii)),y(endpoint(ii)),'sr')
end
% compute second type of width using ending and starting points
peakWidth2 = x(endpoint) - x(startpoint);

K-Means Clustering of random numbers in Matlab

I have a program that generates 10 fixed points and 3 random points when run. I would like the 10 fixed points to use K-means clustering but don't know where to begin. My code is below
function TESTING (re_point)
%***********************NOTE*************************************
% if re_point = 0 [default]
% points generated for xtemp and y_temp remain fixed
% if re_point = 1
% new points are generated for x_temp and y_temp
% Variable definitions for tags and figure window
persistent xtemp ytemp hFig
% Initialisiation of re_point
if nargin<1
re_point = 0; % If 0, the points are fixed, if 1 they move
end
A1 = 30; % area defined as 30 X 30 grid
N = 10;
R = 3; % 3 readers
s = rng; % fixed tags does not change position when simulated repatedly
rng(s)
if (isempty(xtemp) && isempty(xtemp)) || re_point == 1
% Generate x and y position of tags
xtemp = A1*rand(1,N);
ytemp = A1*rand(1,N);
end
if isempty(hFig)
hFig = figure;
end
% Generate x and y position of red points
xtemp_2 = A1*rand(1,R);
ytemp_2 = A1*rand(1,R);
% plot data
plot(xtemp,ytemp,'.',xtemp_2,ytemp_2,'rs','LineWidth',1,'MarkerEdgeColor','k','MarkerFaceColor','r','MarkerSize',14);
% Labelling of the red markers
for iter = 1:numel(xtemp_2)
text(xtemp_2(iter),ytemp_2(iter), num2str(iter),...
'FontSize',8,'HorizontalAlignment','center',...
'Color','White','FontWeight','bold');
end
grid on
hold off
axis([0 A1 0 A1])
% Tag formatting
xoffset = 0;
yoffset = -1;
fsize = 8;
temp_str = mat2cell(num2str([xtemp(:) ytemp(:)], '(%.2f,%.2f)'), ones(1,N));
text(xtemp+xoffset, ytemp+yoffset, temp_str,'fontsize', fsize)
% distance function calculator
cDistance = distanceCalc()
function S = distanceCalc
S = size(numel(xtemp),numel(xtemp_2));
for ri = 1:numel(xtemp)
for fi = 1:numel(xtemp_2)
S(ri,fi) = pdist([xtemp(ri),ytemp(ri);...
xtemp_2(fi),ytemp_2(fi)],...
'euclidean');
end
end
end
end
This particular snippet from the block above generates the 10 fixed points that need to be clustered
if (isempty(xtemp) && isempty(xtemp)) || re_point == 1
% Generate x and y position of tags
xtemp = A1*rand(1,N);
ytemp = A1*rand(1,N);
end
It's not clear at all from your question what you want to do with kmeans, for instance how many clusters are you looking for? I recommend looking at the first example in the MATLAB ref guide
For your data you can try e.g.
X = [xtemp(:) ytemp(:)];
Nclusters = 3;
[idx,C] = kmeans(X,Nclusters);
For plotting something like the following should work:
figure, hold on
plot(X(idx==1,1),X(idx==1,2),'b*')
plot(X(idx==2,1),X(idx==2,2),'g*')
plot(X(idx==3,1),X(idx==3,2),'r*')
to get started. This will attempt to classify your random points into 3 clusters. The cluster into which each point has been classified is defined in idx.