matlabimage-processingfisheye

Convert Fisheye Video into regular Video


I have a video stream coming from a 180 degree fisheye camera. I want to do some image-processing to convert the fisheye view into a normal view.

After some research and lots of read articles I found this paper.

They describe an algorithm (and some formulas) to solve this problem.

I used tried to implement this method in a Matlab. Unfortunately it doesn't work, and I failed to make it work. The "corrected" image looks exactly like the original photograph and there's no any removal of distortion and secondly I am just receiving top left side of the image, not the complete image but changing the value of 'K' to 1.9 gives mw the whole image, but its exactly the same image.

Input image:

Input Image

Result:

When the value of K is 1.15 as mentioned in the article

When the value of K is 1.9

Here is my code:

image = imread('image2.png');
[Cx, Cy, channel] = size(image);

k = 1.5;
f = (Cx * Cy)/3;
opw = fix(f * tan(asin(sin(atan((Cx/2)/f)) * k)));
oph = fix(f * tan(asin(sin(atan((Cy/2)/f)) * k)));
image_new  = zeros(opw, oph,channel);

for i = 1: opw    
    for j = 1: oph        
        [theta,rho] = cart2pol(i,j);        
        R = f * tan(asin(sin(atan(rho/f)) * k));        
        r = f * tan(asin(sin(atan(R/f))/k));        
        X = ceil(r * cos(theta));        
        Y = ceil(r * sin(theta));

        for k = 1: 3            
            image_new(i,j,k) = image(X,Y,k);            
        end
    end
end

image_new = uint8(image_new);
warning('off', 'Images:initSize:adjustingMag');
imshow(image_new);

Solution

  • This is what solved my problem.

    input: strength as floating point >= 0. 0 = no change, high numbers equal stronger correction. zoom as floating point >= 1. (1 = no change in zoom)

    algorithm:

    set halfWidth = imageWidth / 2
    set halfHeight = imageHeight / 2
    
    if strength = 0 then strength = 0.00001
    set correctionRadius = squareroot(imageWidth ^ 2 + imageHeight ^ 2) / strength
    
    for each pixel (x,y) in destinationImage
        set newX = x - halfWidth
        set newY = y - halfHeight
    
        set distance = squareroot(newX ^ 2 + newY ^ 2)
        set r = distance / correctionRadius
    
        if r = 0 then
            set theta = 1
        else
            set theta = arctangent(r) / r
    
        set sourceX = halfWidth + theta * newX * zoom
        set sourceY = halfHeight + theta * newY * zoom
    
        set color of pixel (x, y) to color of source image pixel at (sourceX, sourceY)