Question

I have some code which takes a fish eye images and converts it to a rectangular image in each RGB channels. I am having trouble with the fact the the output image is square instead of rectangular. (this means that the image is distorted, compressed horizontally.) I have tried changing the output matrix to a more suitable format, without success. Besides this i have also discovered that for the code to work the input image must be square like 500x500. Any idea how to solve this issue? This is the code:

The code is inspired by Prakash Manandhar "Polar To/From Rectangular Transform of Images" file exchange on mathworks.

EDIT. Code now works.

function imP = FISHCOLOR2(imR)

rMin=0.1; 
rMax=1;

[Mr, Nr, Dr] = size(imR); % size of rectangular image 
xRc = (Mr+1)/2; % co-ordinates of the center of the image 
yRc = (Nr+1)/2; 
sx = (Mr-1)/2; % scale factors 
sy = (Nr-1)/2;

reduced_dim = min(size(imR,1),size(imR,2));
imR = imresize(imR,[reduced_dim reduced_dim]);

M=size(imR,1);N=size(imR,2);


dr = (rMax - rMin)/(M-1); 
dth = 2*pi/N;

r=rMin:dr:rMin+(M-1)*dr; 
th=(0:dth:(N-1)*dth)'; 

[r,th]=meshgrid(r,th); 

x=r.*cos(th); 
y=r.*sin(th); 
xR = x*sx + xRc; 
yR = y*sy + yRc; 

 for k=1:Dr % colors
   imP(:,:,k) = interp2(imR(:,:,k), xR, yR);        % add k channel
end

 imP = imresize(imP,[size(imP,1), size(imP,2)/3]);

 imP = imrotate(imP,270);

SOLVED

Input image <- Image link

Output image <- Image link

Was it helpful?

Solution

PART A

To remove the requirement of a square input image, you may resize the input image into a square one with this -

%%// Resize the input image to make it square
reduced_dim = min(size(imR,1),size(imR,2));
imR = imresize(imR,[reduced_dim reduced_dim]);

Few points I would like to raise here though about this image-resizing to make it a square image. This was a quick and dirty approach and distorts the image for a non-square image, which you may not want if the image is not too "squarish". In many of those non-squarish images, you would find blackish borders across the boundaries of the image. If you can remove that using some sort of image processing algorithm or just manual photoshoping, then it would be ideal. After that even if the image is not square, imresize could be considered a safe option.

PART B

Now, after doing the main processing of flattening out the fisheye image, at the end of your code, it seemed like the image has to be rotated 90 degrees clockwise or counter-clockwise depending on if the fisheye image have objects inwardly or outwardly respectively.

%%// Rotating image 
imP = imrotate(imP,-90); %%// When projected inwardly
imP = imrotate(imP,-90); %%// When projected outwardly

Note that the flattened image must have the height equal to the half of the size of the input square image, that is the radius of the image.

Thus, the final output image must have number of rows as - size(imP,2)/2

Since you are flattening out a fisheye image, I assumed that the width of the flattened image must be 2*PI times the height of it. So, I tried this -

imP = imresize(imP,[size(imP,2)/2 pi*size(imP,2)]);

But the results looked too flattened out. So, the next logical experimental value looked like PI times the height, i.e. -

imP = imresize(imP,[size(imP,2)/2 pi*size(imP,2)/2]);

Results in this case looked good.

OTHER TIPS

I'm not very experienced in the finer points of image processing in MATLAB, but depending on the exact operation of the imP fill mechanism, you may get what you're looking for by doing the following. Change:

M = size(imR, 1);
N = size(imR, 2);

To:

verticalScaleFactor = 0.5;
M = size(imR, 1) * verticalScaleFactor;
N = size(imR, 2);

If my hunch is right, you should be able to tune that scale factor to get the image just right. It may, however, break your code. Let me know if it doesn't work, and edit your post to flesh out exactly what each section of code does. Then we should be able to give it another shot. Good luck!

This is the code which works.

function imP = FISHCOLOR2(imR)

rMin=0.1; 
rMax=1;

[Mr, Nr, Dr] = size(imR); % size of rectangular image 
xRc = (Mr+1)/2; % co-ordinates of the center of the image 
yRc = (Nr+1)/2; 
sx = (Mr-1)/2; % scale factors 
sy = (Nr-1)/2;

reduced_dim = min(size(imR,1),size(imR,2));
imR = imresize(imR,[reduced_dim reduced_dim]);

M=size(imR,1);N=size(imR,2);


dr = (rMax - rMin)/(M-1); 
dth = 2*pi/N;

r=rMin:dr:rMin+(M-1)*dr; 
th=(0:dth:(N-1)*dth)'; 

[r,th]=meshgrid(r,th); 

x=r.*cos(th); 
y=r.*sin(th); 
xR = x*sx + xRc; 
yR = y*sy + yRc; 

 for k=1:Dr % colors
   imP(:,:,k) = interp2(imR(:,:,k), xR, yR);        % add k channel
end

 imP = imresize(imP,[size(imP,1), size(imP,2)/3]);

 imP1 = imrotate(imP1,270);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top