Finding and building a parabola in Matlab

I am trying to find a parabola in the image. For starters, I took an image with a black parabola on a white background. Then I found black pixels in the image using the find command

[yi xi] = find(im<10); % im is the image with black parabola and white background 

After that, I accidentally took 3 points from the collection and solved the equation for the parabola using the symbolic toolbar using

 syms xy; %solve them for the parabola equation A = [ x^2 xy 1 ;x0^2 x0 y0 1; x1^2 x1 y1 1; x2^2 x2 y2 1]; 

Where

% (x0, y0) = (104,137)

% (x1, y1) = (244,161)

% (x2, y2) = (300,229)

 S = solve(det(A),y); 

Then I get the coefficients a, b, c as

a = 0.0100

b = -1.6800

c = 254.1900

where a, b and c are

 a*x^2 + b*x + c = y; 

Since I got eqn, I have built a parabola by setting the coefficient values ​​and taking

  xx = 1:300; yy = a*xx.^2 + b*xx +c ; 

then i draw a parabola on the image like

  plot(xx,yy,'-'); 

To confirm that I took the correct points, I also draw the selected points on the image, and they lie exactly on the parabola in the image. So this is not a problem.

Problems:

  • The parabola that I draw (blue) does not lie on the image parabola (black).
  • When I put the x coordinate value in the above equation. The y value does not match the y coordinate.

    e.g.: (104,137)

    0.0100 * 104 * 104 -1.68 * 104 + 254.19 = 108.16 - 174.72 + 254.19 = 187.63 whereas it should be 137

My parabola is wrong. Any help would be appreciated. Images parabola from the equation (blue) plotted on the image (black)

+4
source share
2 answers

I think you should round somewhere in your calculations a, b and c.

I had three questions that you mentioned using the fit function (with the appropriate type poly2), and my a, b, c were 0.0053, -1.6802, and 254.1895 (or added more decimals when using the format for a long time).

Similarly, using the same code that you give, the output of the solution:

 S = (73*x^2)/13720 - (5763*x)/3430 + 87187/343 S2 = double(coeffs(S)) S2 = 254.1895 -1.6802 0.0053 

This gives me the same a, b and c as for fit / poly2 (just looking at it, the output 73/13720 cannot be 0.01). Also, if I draw this curve on top of the source points using the same code that you give, it works fine. Thus, the only remaining source of error that I see is a kind of rounding in any code that you use to extract the values ​​of a, b, and c from the result of the solution.

+2
source

The following work. The habit of changing the intuitive meaning of ordinates and abscissas used in a plot can sometimes create overlays. Your problem may arise due to the way you determine your coordinate system when building using another function (imshow or related image display procedures depending on the graph), in particular, the position of the beginning of the image. Here is my code:

 % create image of parabola k =[-0.3 10 10]; x = [1:0.1:50]; y = round(k(1)*x.^2 + k(2)*x + k(3)); crd = unique(round([x(:) y(:)]),'rows'); Nx = 100; Ny = 100; img = ones(Nx,Ny)*255; iok = find((crd(:,1)>0) & (crd(:,1)<=Nx) & (crd(:,2)>0) & (crd(:,2)<=Ny)); indx = sub2ind([Nx Ny],crd(iok,1),crd(iok,2)); img(indx) = 0; imshow(img) 

Then we come to the parabola

 % pick three points: x0 = crd(1,1); y0 = crd(1,2); x1 = crd(80,1); y1 = crd(80,2); x2 = crd(160,1); y2 = crd(160,2); % plot these on the original image hold on plot(y0,x0,y1,x1,y2,x2,'Markersize',20,'Linestyle','none','Marker','x','Color','r') 

By solving the equation exactly as you showed, the result

S = -1094/3627 * x ^ 2 + 12073/1209 * x + 37415/3627

Overlay of the established equation

 a= -1094/3627; b = 12073/1209; c = 37415/3627; xx = 1:100; yy = a*xx.^2 + b*xx +c ; % then I plot the parabola on the image as plot(yy,xx,'g--'); 

Not very beautiful plot (green = landing, red crosses = selected points, blue = parabola, applied without changing the order of x and y):

enter image description here

0
source

Source: https://habr.com/ru/post/1495461/


All Articles