MATLAB exercise program (rectangle transformed into circle)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.