2
$\begingroup$

I know how to make a plot given a function, but I don't know how to get implicit function based on an image.

For example, I have the image shown below (thumbnail):

graph

Full size image import:

Import @ "https://i.stack.imgur.com/SLkeC.png"

I want to find the math function within it. It seems to be an ellipse, but what is a good way to get the function that plot as this ellipse?

$\endgroup$
  • 3
    $\begingroup$ Please show us the code you evaluated to get the image shown above. Add it to your question by making an edit to it. $\endgroup$ – m_goldberg Jan 15 '18 at 3:01
  • 1
    $\begingroup$ Simon says this is the answers to your question mathematica.stackexchange.com/a/17780/8070 $\endgroup$ – Sumit Jan 15 '18 at 9:01
  • $\begingroup$ @Sumit, I read this post,but that method cannot get implicit function.And I cannot open Michael Trott's 3 post. $\endgroup$ – kittygirl Jan 15 '18 at 9:34
  • $\begingroup$ Note that the is not a unique function that has the boundary of the shape as zero-set. $\endgroup$ – Henrik Schumacher Jan 15 '18 at 11:20
  • 1
    $\begingroup$ Perhaps these Q&A are closer to your question: (25589), (99578) $\endgroup$ – Michael E2 Jan 15 '18 at 20:47
5
$\begingroup$

Along the same line, I first binarize the image to obtain the ellipse, and rotate the image to get the correct origin of the pixel coordinates (we should recall that the pixel coordinates origin are at the left top corner):

enter image description here

imgbw = Binarize[ImageRotate[img, 270 Degree],Method -> {"BlackFraction", 0.99}];
elip = N@Position[ImageData@imgbw, 1];

Now, we fit the best ellipse by means of 51491:

lin = {#1^2, #1, #2, 2 #1 #2, #2^2} & @@@ elip;
lm = LinearModelFit[lin, {1, a, b, c, d}, {a, b, c, d}];
pa = lm["BestFitParameters"];
w[x_, y_] := pa.{1, x^2, x, y, 2 x y} - y^2;
ContourPlot[w[x, y] == 0, 
{x, 200, 400}, {y, 200, 500}, ContourStyle -> {Black, Dashed}, 
Epilog -> {Red, , PointSize[0.003], Point[elip]}]

enter image description here

$w[x,y]=-3.06274 x^2-y^2+1192.22 x+152.71 y+1.829648 xy-196494=0$

$\endgroup$
3
$\begingroup$

lhere is a function which returns an implicit function such as the equation of an ellipse.

functionThatReturnsImplicit[image_] := Module[
                                             {img,edgeCoordinates,lin},
              edgeCoordinates = PixelValuePositions[EdgeDetect[image],1];

              lin = {#1^2, #1, #2, 2 #1 #2, #2^2} & @@@edgeCoordinates;
              lm = LinearModelFit[lin, {1, a, b, c, d}, {a, b, c, d}];

              pa = lm["BestFitParameters"];
              w[xx_, yy_] := pa.{1, xx^2, xx, yy, 2 xx yy} - yy^2;
              w[x, y] == 0
              ]

for instance

     image = Import@"https://i.stack.imgur.com/SLkeC.png";

   functionThatReturnsImplicit[image]
   -197019. + 1190.93 x - 3.05173 x^2 + 157.116 y + 1.81471 x y - y^2 == 0        

enter image description here

$\endgroup$
  • 1
    $\begingroup$ Your function functionThatReturnsImplicit doesn't use its argument image... $\endgroup$ – Alexey Popkov Jan 16 '18 at 1:06
  • $\begingroup$ @AlexeyPopkov thanks I will edit it, my bad $\endgroup$ – Conor Cosnett Jan 16 '18 at 6:50
1
$\begingroup$

Yet another approach:

img = Import@"https://i.stack.imgur.com/SLkeC.png";
{center, sa, angle} = 
  1 /. ComponentMeasurements[AlphaChannel@img, {"Centroid", "SemiAxes", "Orientation"}];
Show[Binarize[img], Graphics[{Blue, Thick, Dashed, Rotate[Circle[center, sa], angle]}]]

output

m = TransformedRegion[Ellipsoid[center, sa], RotationTransform[angle]][[2]];
ellipseEquation = Expand[({x, y} - center).Inverse[m].({x, y} - center) - 1] == 0

$0.000349686 x^2-0.000207989 x y-0.136216 x+0.000114855 y^2-0.0181534 y+22.5165=0$

Show[ContourPlot[Evaluate@ellipseEquation, {x, 200, 400}, {y, 200, 500}], 
 Graphics[{Red, Thick, Dashed, Rotate[Circle[center, sa], angle]}]]

output

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.