Recently, I am very interested in the transformation of image coordinates. This is to convert an image into a circle.
The conversion principle is to scale down the distance between the pixels before and after the transformation to the center of the circle proportionally.
Changing the coefficient in the direction of X and Y should also be able to be transformed to an elliptic, but I have not tried it yet.
Note that I want to narrow down the image here, so we use forward interpolation. If we want to generate a large circle, we still need reverse interpolation.
The source image is as follows:
Results After processing:
The Matlab code is as follows:
Clear all; close all1_clc1_img1_imread('lena.jpg '); [H w] = size (IMG); imshow (IMG); imgn = zeros (H, W); cenx = W/2; ceny = H/2; newr = W/2; % circle radius for y = 1: H for x = 1: w r = SQRT (X-cenx) ^ 2 + (Y-ceny) ^ 2); % the distance from the current pixel to the image center ang = atan2 (Y-ceny, X-cenx ); if ang> = PI/4 & ang <= 3 * PI/4 | ang <=-PI/4 & ang> =-3 * PI/4 r = SQRT (ceny ^ 2 + (X-cenx) ^ 2); % the distance between the current pixel and the image center is else r = SQRT (cenx ^ 2 + (Y-ceny) ^ 2 ); end Ss = r/R; % proportional coefficient xx = round (newr * ss * Cos (ANG) + cenx )); YY = round (newr * ss * sin (ANG) + ceny )); if XX> = 1 & XX <= W & YY> = 1 & YY <= H imgn (YY, XX) = IMG (Y, x); End endfigure; imshow (imgn, [])
MATLAB exercise program (rectangle transformed into circle)