Design a circle on a square?

I have a retina photo stored as an array. I will present this image using this two-dimensional array (my actual array is much larger, has 3 color channels and the values ​​are floating point numbers, not 0or 1):

array([[0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 1, 0, 0, 0, 0],
       [0, 0, 1, 1, 1, 1, 1, 0, 0],
       [0, 0, 1, 1, 1, 1, 1, 0, 0],
       [0, 1, 1, 1, 1, 1, 1, 1, 0],
       [0, 0, 1, 1, 1, 1, 1, 0, 0],
       [0, 0, 1, 1, 1, 1, 1, 0, 0],
       [0, 0, 0, 0, 1, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0]])

How can I project a circle (somehow stretch the edges?) So that it becomes square without cropping? So it looks like this:

array([[0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 1, 1, 1, 1, 1, 1, 1, 0],
       [0, 1, 1, 1, 1, 1, 1, 1, 0],
       [0, 1, 1, 1, 1, 1, 1, 1, 0],
       [0, 1, 1, 1, 1, 1, 1, 1, 0],
       [0, 1, 1, 1, 1, 1, 1, 1, 0],
       [0, 1, 1, 1, 1, 1, 1, 1, 0],
       [0, 1, 1, 1, 1, 1, 1, 1, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0]])

Basically, I am looking for a Python library that can do conformal mapping in this image

conformal mapping from circle to square

+5
source share
2 answers

, 1, :

>>> a=np.array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
...        [ 0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.],
...        [ 0.,  0.,  1.,  1.,  1.,  1.,  1.,  0.,  0.],
...        [ 0.,  0.,  1.,  1.,  1.,  1.,  1.,  0.,  0.],
...        [ 0.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  0.],
...        [ 0.,  0.,  1.,  1.,  1.,  1.,  1.,  0.,  0.],
...        [ 0.,  0.,  1.,  1.,  1.,  1.,  1.,  0.,  0.],
...        [ 0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.],
...        [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]])
>>> 
>>> (min_row,max_row),(min_col,max_col)=map(lambda x :(np.min(x),np.max(x)),np.nonzero(a))
>>> a[min_row:max_row+1,min_col:max_col+1]=1
>>> a
array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  0.],
       [ 0.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  0.],
       [ 0.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  0.],
       [ 0.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  0.],
       [ 0.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  0.],
       [ 0.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  0.],
       [ 0.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]])
0

squircle ( : )

import squircle
import PIL
import numpy as np

square = np.asarray(Image.open('some-square-image.jpg'))  # or create the array some other way

circle = squircle.to_circle(square, method="fgs")
and_back_to_square = squircle.to_square(circle, method="fgs")

3 /: "fgs", "stretch" "elliptical"

angular, , , , angular.

pip install squircle
0

All Articles