"Squaring the Circle"

Not to discuss of an abstract mathematical problem, but from a practical image processing viewpoint:

  • is there a way to transform graphics created in a circle like the attached sample in squared graphics? (to be used for instance as frames around an image?). Thanks.
    Merry Christmas to everybody.

Yes, it is possible mathematically. I’m not sure yet how to do it though. But, I’m way too busy solving string-related problem in G’MIC for my next year continuation of refactor and improvement of my G’MIC filters.

Thanks for your reply.
If you’ll find a “hole” of time, maybe something new could happen.
Happy New Year (and success for your continuous activity).

Off the top of my head, using polar coordinates centered on the circle, I would say that a point at (ρ,θ) is moved to

(ρ/(max(abs(sin(θ)),abs(cos(θ)))),θ)

The problem is that you probably want the inverse transformation (ie, given a point in the target, where is the corresponding point in the source) but if you divide the image in 4 quadrants (delimited by diagonals), then a point at (ρ,θ) corresponds to the point at

  • (ρ*abs(sin(θ),θ) in the top & bottom quadrants
  • (ρ*abs(cos(θ),θ) in the side quadrants

Converting to Cartesian coordinates is left as an exercise to the reader :smiling_imp:

This perhaps could done with a displace map in Gimp (to quote a famous French movie line, I’m not sure enough to bet, but I would give my word of honor)

Probably a good start:

foo :
  sp colorful circle. 50%,50%,20%,1,255,128,0 circle. 50%,50%,10%,1,255,0,0
  +f. "x = x - w/2;
       y = y - h/2;
       t = atan2(y,x);
       r = norm(x,y);

       phi = (t + pi/4)%(pi/2) - pi/4;
       nr = r*cos(phi);
       nx = nr*cos(t);
       ny = nr*sin(t);

       i(w/2 + nx,h/2 + ny)"

transforms an image with centered circles into centered squares :

2 Likes

This is what it does on your image @dinasset :

1 Like

Wow! Fantastic!
David, could you store this process under G’MIC for future use by filters?
Thanks a lot.

P.S.
Just seen that you have already created this filter in G’MIC; my fault not to have checked (Square to Circle has also the choice Circle to Square).
Happy to have it already available. Thanks and sorry again.

You’re probably not considering doing this, but as you asked for filters before, I think you could consider trying to code in G’MIC and making solutions for others.

That being said, I think something like this is doable as a border filter tool as it is now, you just gotta make your target image fit.

When I will have practiced myself, if there is something of general use I will do it as an ancillary Gimp filter (using Gmic inside).

Here it is :

This is not the same as the previous Square to Circle filter, because the new one (Square to Circle [alt]) transforms each centered circle to square and vice-versa. The previous filter (without the alt) was doing this only for the image border.

The code of this new filter:

#@gui Square to Circle [alt] : fx_square_circle_alt,fx_square_circle_alt(1)
#@gui : Mode = choice("Square to Circle","Circle to Square")
#@gui : Center = point(50,50,0,1)
#@gui : Zoom (%) = float(0,-100,100)
#@gui : Interpolation = choice(1,"Nearest Neighbor","Linear")
#@gui : Boundary = choice("Transparent","Nearest","Periodic","Mirror")
#@gui : Adapt to Image Ratio = bool(1)
#@gui : sep = separator()
#@gui : note = note("<small>Author: <i>David Tschumperlé</i>.      Latest Update: <i>2023/12/24</i>.</small>")
fx_square_circle_alt :
  mode,centerx,centery,zoom,interpolation,boundary,ratio=${1-7}
  if !$boundary to_a fi
  f. "const interpolation = $interpolation;
      const boundary = $boundary;
      const zoom = 10^($zoom%);
      const cx = w*$centerx%;
      const cy = h*$centery%;
      const fx = $ratio?w:1;
      const fy = $ratio?h:1;
      x = (x - cx)/fx;
      y = (y - cy)/fy;
      t = atan2(y,x);
      r = norm(x,y);
      phi = (t + pi/4)%(pi/2) - pi/4;
      nr = ($mode?r*cos(phi):r/cos(phi))/zoom;
      nx = cx + fx*nr*cos(t);
      ny = cy + fy*nr*sin(t);
      i(nx,ny)"

Let’s say this is my Xmas present :slight_smile:

Some additional examples:



3 Likes

Double wow ! Really magic. Thanks a lot David :innocent: :innocent: :innocent:

1 Like

David le Magicien ! Formidable.

1 Like

My first use of the new filter to create a frame.
Thanks again David, and Merry Christmas.

1 Like

My second use of the new filter to create a frame.
Circle created before “squaring it” was twisted rays.


The created squared frame has been placed resized proportionally outside (all around) the image in this case.

1 Like