Correcting Image from Wide Lens Camera

Is there any way I can fix an image that got quite a bit of a barrel distortion. I got this image from a camera with wide lens and I’m looking to straighten up the image as much as possible.

I tried using Stoner, but it’s quite hard for me (maybe I need more practice). But, is there any other way/workaround? Maybe something like undistort() in Open CV?

Cheers :slight_smile:

if you want to try your hand at OpenCV, it now comes pre-installed with 099.

See 099 wiki on OpenCV how you can now easily invoke it from any python script.

Here are a lot of opencv 3.2 python examples, and there’s also one about lens barrel distortion - Have fun!

hmm, this looks like something. Anyway, I’m new to python in TouchDesigner. Do you know how can I read image from a TOP?

I tried

cv2.imread(op('videodevin1'))

but no good

read the first link to the wikipage which i posted above

yes, but that link showed how to read image from file in computer. now I wonder how can I use an image from a Video Device In TOP? Do you know how? I’m pretty lost here, sorry.

oops sorry totally misread your last question, thought you asked how to read files.
No, I don’t know how to get TOP → OpenCV, I have a feeling this functionality is not implemented yet.

ah alright then. but thanks a lot for pointing this one. I’ll keep on coming back to the opencv doc.

Hey,

we currently don’t have a direct way of reading in values from a TOP into python cv.
Most probably it would be useful to save out a single camera frame, read it in via cv2.imread, solve for the distortion ones and save those values out.

Cheers
Markus

Seems like you should be able to tackle this kind of barrel distortion with a shader - shouldn’t be too hard to port something from the web on this one.

Thank you for pointing this. So, how can I use the saved values? How to feed it into the video? Should I use shader? Or is there any other transformation/warping operator?

This is a good suggestion, as I wanted to learn shader as well.

So, I googled and founded several shaders I can use to fix the barrel distortion. One of the most interesting is this:

//////////////////////////////// vertex shader //////////////////////////////////
 
uniform float strength;           // s: 0 = perspective, 1 = stereographic
uniform float height;             // h: tan(verticalFOVInRadians / 2)
uniform float aspectRatio;        // a: screenWidth / screenHeight
uniform float cylindricalRatio;   // c: cylindrical distortion ratio. 1 = spherical
 
varying vec3 vUV;                 // output to interpolate over screen
varying vec2 vUVDot;              // output to interpolate over screen
 
void main() {
    gl_Position = projectionMatrix * (modelViewMatrix * vec4(position, 1.0));
 
    float scaledHeight = strength * height;
    float cylAspectRatio = aspectRatio * cylindricalRatio;
    float aspectDiagSq = aspectRatio * aspectRatio + 1.0;
    float diagSq = scaledHeight * scaledHeight * aspectDiagSq;
    vec2 signedUV = (2.0 * uv + vec2(-1.0, -1.0));
 
    float z = 0.5 * sqrt(diagSq + 1.0) + 0.5;
    float ny = (z - 1.0) / (cylAspectRatio * cylAspectRatio + 1.0);
 
    vUVDot = sqrt(ny) * vec2(cylAspectRatio, 1.0) * signedUV;
    vUV = vec3(0.5, 0.5, 1.0) * z + vec3(-0.5, -0.5, 0.0);
    vUV.xy += uv;
}
 
/////////////////////////////// fragment shader ////////////////////////////////
 
uniform sampler2D tDiffuse;     // sampler of rendered scene’s render target
varying vec3 vUV;               // interpolated vertex output data
varying vec2 vUVDot;            // interpolated vertex output data
 
void main() {
    vec3 uv = dot(vUVDot, vUVDot) * vec3(-0.5, -0.5, -1.0) + vUV;
    gl_FragColor = texture2DProj(tDiffuse, uv);
}

got that from decarpentier.nl/lens-distortion

Therefore, my question is, how can I use both vertex and fragment shader in GLSL TOP? The wiki said most of the time I don’t need to use vertex shader. I tried to use both shaders in one GLSL TOP, obviously that won’t work, because then I’d have 2 main(). So, how can I use both shaders?

Also, another question is, the code I got, uses vUVDot and vUV. Are these similar to the vUV available in GLSL TOP?

Sorry for the beginner level question, but I’d really want to learn more about shader in TouchDesigner

In the parameter window of the GLSL TOP you can specify which Text DAT you want to use as your vertex shader.

If you want to write a GLSL TOP, all the TD information you need is on the wikipage How to write a GLSL TOP.

You will see several errors when trying to run this shader as some of the code is deprecated (for instance, instead of ‘varying’ you now have to use ‘in/out’ to pass variables from the vertexshader to the pixelshader). When it can’t compile you’ll see a blue/red checkerboard pattern in the GLSL TOP. So place an INFO DAT and point it to the GLSL TOP. This will display all lines which give an error. Google them all, and you’ll soon learn a lot about GLSL.

And in this code vUV and vUVDot are local variables calculated by the vertex shader so you can leave them as is.

I found the this 2D GLSL tutorial on shadertoy really helpful:
shadertoy.com/view/Md23DV

In learning more about GL I ported those examples to touch here:
github.com/raganmd/learningGLSL

Each of the lessons has its own tox:
github.com/raganmd/learningGLSL … ls/lessons

Then it comes down to lots of practice.

Hi guys, thanks a lot for replying with very valuable resources. So, I had a quick read and practice, it was OK. Then I tried to implement the aforementioned shader code.I thought I get it, but then I got this error message

0(16) : error C1020: invalid operands to "+"

And the line that produces that error is

vec2 signedUV = ((2.0 * uv) + vec2(-1.0, -1.0));

Does anybody knows what make such error? I thought that operation was totally valid.
Also, uv variable in that vertex shader wasn’t declared or initiated. The only line I see uv made and assigned with value is the main() of the pixel shader.

Does pixxel shader able to transfer variable with vertex shader?

Also, I tried replacing uv with a float number. Now the shader complaints about the following error

0(10) : error C1008: undefined variable "projectionMatrix" 0(10) : error C1008: undefined variable "modelViewMatrix" 0(10) : error C1008: undefined variable "position"

question is how to resolve that? Aren’t those variables built in to Touch?