Skip to main content

Intersecting parabolas in an image


I have a friend who is a math teacher (like me) and while out cycling took this picture.


parabolas


The idea would be to create the equations of the quadratics that model the bridges and super-impose them on the picture. (assuming the bridge supports ARE quadratics, but they should be "close enough" for high school...)


I have no idea what is possible with "image processing", so here is possibly a stupid question. The picture is taken from an angle, not sure what it is, but lets say about 30 degrees. I know it won't make much difference to the answer, but is there some image processing command that would skew this image to simulate the picture being taken from "the middle of the river"?


After that, I know I would use some kind of "image compose" to put together some plots of parabolas and the photo.



This question deals with that , but I'd be happy to have further input on that process.


As always, appreciate any input, I learn the most when the problem is something I am actively working on



Answer



The easiest way to do this is probably using FindGeometricTransform: For that, you'd need a number of points in a plane in the image, and their corresponding coordinates in some 2d "world" coordinate system. To illustrate, I'll pick 4 points in the image that are (more or less) the corners of a rectangle:


img = Import["http://i.stack.imgur.com/1VDdh.png"];
pts = {{419, 157}, {933, 140}, {937, 276}, {420, 259}};
LocatorPane[Dynamic[pts],
Dynamic[Show[img,
Graphics[{
{EdgeForm[Red], Transparent, Polygon[pts]},

{
Red,
MapIndexed[Text[Style[#2[[1]], 14], #1, {1, 1}] &, pts]
}
}]]]]

enter image description here


(It's hard to estimate the vanishing point in this image, so I'm not sure how accurate these points are...)


The I'll say that these points correspond to the corners of this rectangle:


width = 100;

height = 20;
ptsFlat = {{0, 0}, {width, 0}, {width, height}, {0, height}};

and let Mathematica do the real work:


{err, transform} = 
FindGeometricTransform[pts, ptsFlat,
TransformationClass -> "Perspective"]

Now transform is a function that maps points from my ptsFlat-coordinate system to image coordinates:


grid = Table[transform[{x, y}], {x, 0, width, 5}, {y, 0, height, 5}];

Show[img,
Graphics[
{
Red, Line /@ grid, Line /@ Transpose[grid]
}]]

enter image description here


You could just pass this function to ImageTransformation and "un-distort" the image:


ImageTransformation[img, transform, 800, PlotRange -> All, 
DataRange -> Full]


enter image description here


But the result isn't that spectacular: The projection is correct (at least ignoring radial distortion) for points in the same plane as the bridge, but stuff before/behind the bridge is distorted and in the wrong place. I wouldn't say that it's completely impossible to get the rest of the image projected the right way; But it'll definitely be quicker to rent a boat and take a picture from the middle of the river.


However, it's now simple enough to project our coordinate system into the image. I'll again choose 3 points on one of the bridge arcs:


ptsBridge = {{429, 163}, {646, 258}, {859, 245}};
LocatorPane[Dynamic[ptsBridge],
Dynamic[Show[img,
Graphics[{
{
Red, Point[ptsBridge],

MapIndexed[Text[Style[#2[[1]], 14], #1, {1, 1}] &, ptsBridge]
}
}]]]]

...map these to our 2d "world" coordinate system:


ptsParabola = InverseFunction[transform] /@ ptsBridge;

fit a parabola to these 3 points:


parabola = Fit[ptsParabola, {1, x, x^2}, x];


and draw that parabola into the image


parabolaPts = 
transform /@
Table[{x, parabola}, {x, Min[ptsParabola[[All, 1]]],
Max[ptsParabola[[All, 1]]], 0.1}];

Show[img,
Graphics[
{
Gray, Line /@ grid, Line /@ Transpose[grid],

Red, Point[ptsBridge],
Line[transform /@ {{#[[1]], 0}, #}] & /@ ptsParabola,
Dashed, Line[parabolaPts]
}]]

enter image description here


Comments

Popular posts from this blog

plotting - How to draw lines between specified dots on ListPlot?

I would like to create a plot where I have unconnected dots and some connected. So far, I have figured out how to draw the dots. My code is the following: ListPlot[{{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4,13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full] I have thought using ListLinePlot command, but I don't know how to specify to the command to draw only selected lines between the dots. Do have any suggestions/hints on how to do that? Thank you. Answer One possibility would be to use Epilog with Line : ListPlot[ {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4, 13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full, Epilog -> { Line[ ...

equation solving - Invert and fit implicitly defined curve

I need to fit an implicitly defined curve. I thought I could get some data out of Solve , and then using FindFit . Therefore, I would like to find the relation the parametric curve defined by $F(x,y)=0$: Solve[-(1/2) + 1/2 (0.41202 BesselK[0, 0.1 Sqrt[x^2 + y^2]] + (0.101483 x BesselK[1, 0.1 Sqrt[x^2 + y^2]])/Sqrt[x^2 + y^2]) == 0, y] But I can't get an output: Solve was unable to solve the system with inexact coefficients or the system obtained by direct rationalization of inexact numbers present in the system. Since many of the methods used by Solve require exact input, providing Solve with an exact version of the system may help. >> Edit: In particular, I would like to fit the data coming from the curve with the expression of another curve, and not with a function $f(x)$. In particular, since this clearly looks like a cardioid , I would like it to fit to something like it. What other strategies could I try?

dynamic - How can I make a clickable ArrayPlot that returns input?

I would like to create a dynamic ArrayPlot so that the rectangles, when clicked, provide the input. Can I use ArrayPlot for this? Or is there something else I should have to use? Answer ArrayPlot is much more than just a simple array like Grid : it represents a ranged 2D dataset, and its visualization can be finetuned by options like DataReversed and DataRange . These features make it quite complicated to reproduce the same layout and order with Grid . Here I offer AnnotatedArrayPlot which comes in handy when your dataset is more than just a flat 2D array. The dynamic interface allows highlighting individual cells and possibly interacting with them. AnnotatedArrayPlot works the same way as ArrayPlot and accepts the same options plus Enabled , HighlightCoordinates , HighlightStyle and HighlightElementFunction . data = {{Missing["HasSomeMoreData"], GrayLevel[ 1], {RGBColor[0, 1, 1], RGBColor[0, 0, 1], GrayLevel[1]}, RGBColor[0, 1, 0]}, {GrayLevel[0], GrayLevel...