A matlab program simulating ps liquefaction effect (tilting deformation)

Source: Internet
Author: User

A while ago, the teacher left an after-school question, which is roughly the effect of the question. It was implemented using MATLAB. The final result is not bad, but it may be a problem of MATLAB cycle efficiency, there are other reasons why your code has not been further optimized, which may lead to slow program running. Hope you can give me some suggestions. Thank you.

 

For the code, see:

Encapsulate the code into a subfunction change ()

The general idea is:

1) Based on the coordinates of the start point and end point given by the outside world, the unit vector A = [ax AY] in this direction is obtained;

Note: The iterative direction of the image is determined based on the positive and negative judgment of ax and ay of vector.

2) When the direction vector is solved, the parallel distance ax and vertical distance ay from the unit vector A are determined respectively, solve the deformation degree (of course, the point must be in the circle !);

 

Note: The iteration idea here can be roughly understood as follows:

If there is a straight line, under the Action of forward deformation, it will definitely become the following curve B _line.

In this case, the three points A, B, and C on the source image are mapped to three points A, B, and C on the transformed graph.

Now assume that the coordinates of A, B, and C are (ax, ay) (BX, by) (CX, CY) (also ay = by = cy, that is, the original line is a horizontal line)

Then, the coordinates of the three ABC points are (ax, Ay + a) (BX, by + B) (CX, Cy + C, the gray level or other RGB components of the points on the curve can be obtained based on the gray level or RGB component of the previous point (in this example, the previous vertex is calculated by the increment of the current vertex minus A/B/C)

That is to say, if there is A bit between A A', then the value of A can be indirectly obtained by A '', and the value of A'' is obtained by. You may think that this is unnecessary, but you may think that if A is too far away from A, this will happen: the a' point calculated based on Vertex A is no longer within the image's ie range, and therefore cannot obtain accurate values. Reasonable control of iteration distance will make the image deformation better.

For how to create mappings in non-special cases (horizontal and vertical), see the following program code.

3) Also, the deformation degree of the arc needs to be manually controlled (in fact, the straightforward point is to set the influence weights of ax and ay ).

4) after multiple tests, it is found that the iteration distance is 5, and the calculated a' point is not necessarily an integer point, interpolation is also used here (bilinear interpolation is used in this experiment ).

 

Code attached:

Change. m

% I is the original rgb matrix, sp is the starting coordinate, fp is the end coordinate of the matrix, rm is the radius, and s is the deformation degree (0 <s <1) function J = change (I, sp, fp, rm, s); J = I; [ym xm] = size (rgb2gray (I )); % sp = [35 480]; % start point % fp = [1 40]; % end point % rm = 100; % radius % s = 0.75; p = sp; zz = sp-fp; z2 = sqrt (zz (1) ^ 2 + zz (2) ^ 2); danwei = zz/z2; % unit vector x1 = 1; x2 = 1; % indicates the iteration direction, and negative value indicates the decreasing iteration if danwei (1) <0x1 =-1; endif danwei (2) <0x2 =-1; endfor k = % The number of iterations depends on the start and end points. yma = floor (min (p (1) + rm, ym); ymi = floor (max (1, p (1)-rm); xma = floor (min (p (2) + rm, xm); xmi = floor (max (1, p (2) -rm); % Four boundary values: sm1 = ymi; sm2 = yma; sn1 = xmi; sn2 = xma; ii = 1; jj = 1; if x1 <0 sm1 = yma; sm2 = ymi; ii =-1; end if x2 <0 sn1 = xma; sn2 = xmi; jj =-1; end for I = sm1: ii: sm2 % need for j = sn1: jj: sn2 % need d2 = (p (1)-I) ^ 2 + (p (2) -j) ^ 2; if d2 <= rm ^ 2 AB = [p (1)-I, p (2)-j]; py = abs (AB * danwei '); % parallel to the moving direction px = sqrt (d2-py ^ 2 ); % perpendicular to the moving direction fmn = 5 * (1-(1/rm) * sqrt (s * px ^ 2 + (1-s) * py ^ 2 )) * danwei + [I, j]; fm = fmn (1); fn = fmn (2 ); if (fm <1 | fm> ym | fn <1 | fn> xm) else km = fm-floor (fm); kn = fn-floor (fn ); J (I, j, :) = (1-kn) * (km * J (floor (fm) + 1, j, :) + (1-km) * J (floor (fm ), j, :)) + kn * (km * J (floor (fm) + 1, j + 1, :) + (1-km) * J (floor (fm), j + 1, :)); end endendp = p + 5 * danwei; % the result obtained from the test-5 is the best end.

Now open the matlab command window and write:

I =imread('tigger.jpg '); sp = [400,600]; % start coordinate fp = [200,300]; % end coordinate rm = 100; s = 0.75; J = change (I, sp, fp, rm, s); figure; imshow (I); figure; imshow (J); % display the transformed Image

  

The conversion is as follows:

 

Please correct me for any errors and better methods.

Reprinted please specify source http://www.cnblogs.com/blue-lg/archive/2011/12/09/2282631.html

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.