Search code examples
javaimage-processingfisheyedistortion

FishEye Picture Effect (Barrel Distortion) Algorithm (with Java)?


I'm looking for the algorithm (bitmap pixel manipulation) to simulate the fisheye lens (Barrel Distortion) from normal pictures. So far I've found implementation involving external libraries like OpenCV, OpenGL or jhlabs. Since I'm taking a class in Digital Image Processing and I'm making a course assessment project, I'm not sure whether using any external lib will earn me a good grade. So would anyone give me the reference to such algorithm?

Ps. I'm asked to implement it in Java, but example from any language would do.


Solution

  • It's good that you've been able to find examples that do what you want. It's helpful to include them in your question -- it makes sure that people that read it are on the same page as you are. So here's a link.

    It's also good that you want to do things by yourself, without relying on some library to do the hard work for you. But that doesn't mean you have to ignore such solutions. Here's why.

    Look at what OpenCV is actually being used for in that link. These are functions that start with cv:

    $ grep -o "cv\\w*" barrel.cpp | sort | uniq
    cv
    cvCreateImage
    cvGet2D
    cvGetSize
    cvLoadImage
    cvNamedWindow
    cvSaveImage
    cvSet2D
    cvShowImage
    cvWaitKey
    

    If you look at the OpenCV API, all of these functions just handle mundane tasks like image creation, deletion, display, setting of pixels, etc. None of these tasks are particular to barrel distortion. As far as barrel distortion goes, that solution is not OpenCV specific.

    Indeed, the heart of the program is here:

    float getRadialX(float x,float y,float cx,float cy,float k){
      x = (x*xscale+xshift);
      y = (y*yscale+yshift);
      float res = x+((x-cx)*k*((x-cx)*(x-cx)+(y-cy)*(y-cy)));
      return res;
    }
    
    float getRadialY(float x,float y,float cx,float cy,float k){
      x = (x*xscale+xshift);
      y = (y*yscale+yshift);
      float res = y+((y-cy)*k*((x-cx)*(x-cx)+(y-cy)*(y-cy)));
      return res;
    }
    

    Which is just the radial transform formula -- this is the bit that you need to understand. As you can see, there's no OpenCV calls in there.