Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Sphere distortion / barrel grid algorithm?

The below image is an example of sphere distortion in Photo Shop. Pleasesee below image.

image description

When I changed the parameter 0 to 50 to 100, the 2D grids are changing. So basically this is what I want to achieve.

Below is my current code:

    private static CvPoint GetShiftedPoint( CvPoint center, CvPoint point, double p = 1.5 )
    {       
                    // p == 1 it draws perfect sphere. As it grows, it should look like flat surface
                    // the bigger p, the longer radius
        double a = Math.Max( center.X, center.Y );          
        double r = a * p;
        double distance = center.DistanceTo( point );

                    // calculate height of sphere
        double theta = Math.Asin( a / r );
        double h = Math.Cos( theta ) * r;
        double hTop = r - h;

                    // calculate shift amount
        double y = r - ( hTop * distance / a );
        double rho = Math.Acos( y / r );
        double newDistance = Math.Sin( rho ) * r;

        double dx = point.X - center.X;
        double dy = point.Y - center.Y;
        double radian = Math.Atan2( dy, dx );

        double newDx = Math.Cos( radian ) * newDistance;
        double newDy = Math.Sin( radian ) * newDistance;

        int newX = ( int )Math.Round( center.X + newDx );
        int newY = ( int )Math.Round( center.Y + newDy );

        return new CvPoint( newX, newY );
    }

The theory of my code is that I draw virtual semi-sphere over my image and calculate distance shift amount based on the first distance from the center. Hope you can understand my function.

But the result from this method looks like this: image description

Mine does not look like a sphere :(

Does anyone knows how to draw sphere grids with a strength parameter?