8
$\begingroup$

I found something interesting on this page (Generic contour smoothing with shading) that could help me improve my images. Unfortunately, they can only be used with graphics generated by the software itself:

ClearAll[smoothCP]
smoothCP = Module[{pr = PlotRange @ #, 
  nF =  Nearest[Join @@ Cases[Normal @ #, Line[a_] :> Transpose[#2 /@ Transpose@a], ∞]]},
  # /. GraphicsComplex[a_, b__] :> GraphicsComplex[ 
    If[Or @@ MemberQ @@@ Thread @ {pr, #}, #, First @ nF @ #] & /@ a, b] ] &;
Row[{p1, smoothCP[p1, GaussianFilter[#, {25, 25}] &]}]

I tried to use a PNG image to improve the curves looking for a smooth spline or b-spline.

enter image description here

img = Import["https://i.stack.imgur.com/Yy0uN.png"]
Row[{img, smoothCP[img, GaussianFilter[#, {25, 25}] &]}]

I looked for something on the site, but I didn't find what I wanted.

Is it possible to obtain a smooth spline curve from PNG images?

$\endgroup$
9
$\begingroup$

Extracting a spline from the image is easiest with ImageMesh. I've used the midpoints of the lines on the mesh boundary for the BSplineCurve control points.

img = Import["https://i.stack.imgur.com/Yy0uN.png"];
mesh = ImageMesh@img;
midpoints = Midpoint /@ MeshPrimitives[mesh, 1];
Graphics[{Thick, Darker[Red], 
  BSplineCurve[midpoints, SplineClosed -> True]}]

bspline from image

You can also filter the curve to smooth it out, by replacing each point on the spline by the midpoint of its neighbours:

(* also try `Partition[points, 3, 1]` here too for a different effect *)
filter[points_] := Midpoint[#[[{1, 3}]]] & /@ Partition[points, 3, 2]
Manipulate[
 Graphics[
  BSplineCurve[Nest[filter, midpoints, k], SplineClosed -> True]]
 , {k, 0, 7, 1}]

midpoint filtering

| improve this answer | |
$\endgroup$
  • $\begingroup$ I think I will have to create a Midpoint function, because I have version only Mathematica 11 software $\endgroup$ – LCarvalho Jun 15 at 0:03
  • 1
    $\begingroup$ Midpoint[{x1_, y1_}, {x2_, y2_}] := {(x1 + x2)/2, (y1 + y2)/2} $\endgroup$ – LCarvalho Jun 15 at 0:29

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.