Ask Your Question
1

Sphere distortion / barrel grid algorithm?

asked 2016-03-18 19:26:29 -0600

Papercut gravatar image

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?

edit retag flag offensive close merge delete

Comments

@Papercut have a look also to my answers here and here. The second one is based on the link that @Tetragramm has posted in his answer but ported to the new API. I have also this thread that lacks an answer since I couldn't understand really how he makes the shift of the pixels. So if you or someone else have some time to work on it I would be glad to hear your answers.

theodore gravatar imagetheodore ( 2016-03-19 08:25:07 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2016-03-18 23:16:29 -0600

Tetragramm gravatar image

It's called Barrel Distortion, and here's a link to SO that links to a paper that has the equation.

ru = rd * (1 + k*rd^2)

Ru is the radius undistorted, Rd is the radius distorted, and k is the camera distortion parameter.

edit flag offensive delete link more

Comments

Thanks! What kind of values should I put in k?

Papercut gravatar imagePapercut ( 2016-03-18 23:40:18 -0600 )edit

Negative infinity to positive infinity. Positive values are like what you show in your pictures, Negative values are the other way on the slider.

Tetragramm gravatar imageTetragramm ( 2016-03-19 00:44:07 -0600 )edit

There is a function called cv::undistort. I'm not 100% sure if this is what you need, but you should take a look at it.

matman gravatar imagematman ( 2016-03-19 09:20:20 -0600 )edit

Thanks! Much appreciated!

Papercut gravatar imagePapercut ( 2016-03-21 11:51:53 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-03-18 19:26:29 -0600

Seen: 1,980 times

Last updated: Mar 18 '16