4

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.

Niki
  • 15,662
  • 5
  • 48
  • 74
tsong99
  • 85
  • 1
  • 2
  • 8

3 Answers3

6

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.

Community
  • 1
  • 1
mpenkov
  • 21,621
  • 10
  • 84
  • 126
  • Wow, thanks a lot! I guess I haven't examine that code thoroughly enough. Although I might be able to proceed with my project without using OpenCV based on the code, since I'm not so familiar with OpenCV, would it's Java wrapper be compatible with this specific task? – tsong99 Feb 12 '11 at 17:52
  • Yeah, in theory you should be able to re-use most of that code in Java without too much effort. But I've never used OpenCV with Java so I wouldn't know how it would be in practice. – mpenkov Feb 13 '11 at 00:45
  • Hi, I'm trying to create the same effect but in Android. Could you explain to me how the above methods would work. Atm i have an app where i place a circle on the bitmap, i have an algorithm that targets only the pixels in the circle. i know obviously the radius of the circle, the centre-point of the circle and i can loop through the bitmap pixel by pixel. this is as far as i've got with image processing as i'm new to it. would you mind talking me through how i could apply this effect? any help would be appreciated. i can start a thread with my source if you like – turtleboy May 20 '11 at 17:34
  • @turtleboy: unfortunately, I don't have any Android experience, so I can't help you. If you ask a question and tag with "android", you're likely to find someone who can help you, though. – mpenkov May 21 '11 at 15:55
  • @mishsa ok thanks but the code above isn't particular to Android, i was just wondering how a radial tranform works and how to make that transform in the bitmap. eg i think usually you have a src image and a destination image, then you'll plot the new points in the dest. others things i'm not clear on are, what variables do i need eg radius, x y co-ords of pixels etc – turtleboy May 22 '11 at 13:43
  • Once you have RadialX and RadialY, how would you apply these to make an image again? I'm using R and not sure how to apply this solution to get an image back out at the end. – SqueakyBeak Mar 15 '19 at 19:01
3

I wrote an article about it in http://www.helviojunior.com.br/fotografia/barrel-and-pincushion-distortion/

Helvio
  • 59
  • 1
  • 4
    Welcome to Stack Overflow! Please note that bare links to your own website/product are not encouraged here for two reasons; First, an answer should be posted as a self-contained answer, not a mere link to an external site. Second, self-promotion tends to be frowned upon here, and often is flagged as spam (especially if there is no disclosure that you are linking to your own site/product). – Andrew Barber Feb 16 '13 at 01:27
  • 2
    As Andrew said, it looks like you've got a quite detailed post there. Could you summarize the relevant information for this question and place it in your answer? That would protect against your link going away at some point in the future. – Brad Larson Feb 16 '13 at 01:55
0

https://android.googlesource.com/platform/frameworks/base/+/ac39c604d6df8631922c2295b3341cd561f172a5/media/mca/filterpacks/java/android/filterpacks/imageproc/FisheyeFilter.java

this should be what you want.

look at the shader mFishEyeShader and updateProgramParams()

afpro
  • 1,103
  • 7
  • 12