/********************************************************** * transform.cpp - Implement various affine transforms * as well as the perspective transform * * - rotate: counter-clockwise rotation * - scale: * - translate: * - shear: * - perspective * * Author: Hairong Qi (C) hqi@utk.edu * **********************************************************/ #include "Image.h" #include "Dip.h" #include #include using namespace std; /** * Affine transform - rotation. The homogeneous rotation matrix is * [cos(theta) -sin(theta) 0; sin(theta) cos(theta) 0; 0 0 1] * @param inimg The input image. * @param theta The rotation angle. * @return The rotated image. */ Image rotate(Image &inimg, float theta) { Image outimg; Image R(3,3), IR; // the rotation matrix int i, j, k; int nr, nc, nt, nchan; int x, y; nr = inimg.getRow(); nc = inimg.getCol(); nt = inimg.getType(); nchan = inimg.getChannel(); outimg.createImage(nr, nc, nt); // convert from degree to radian theta = theta * PI / 180.0; R(0,0) = cos(theta); R(0,1) = -sin(theta); R(0,2) = 0; R(1,0) = sin(theta); R(1,1) = cos(theta); R(1,2) = 0; R(2,0) = 0; R(2,1) = 0; R(2,2) = 1; IR = inverse(R); // calculate the inverse transform // for each (i,j) in the transformed image, find the corresponding point // from the original image (inverse transform) for (i=0; i 0 && y > 0) for (k=0; k 0 && y > 0) for (k=0; k 0 && y > 0) for (k=0; k 0 && y > 0) for (k=0; k* B; P(0,0) = C(0,0); P(0,1) = C(1,0); P(0,2) = C(2,0); P(1,0) = C(3,0); P(1,1) = C(4,0); P(1,2) = C(5,0); P(2,0) = C(6,0); P(2,1) = C(7,0); P(2,2) = 1; IP = inverse(P); // calculate the inverse transform // for each (i,j) in the transformed image, find the corresponding point // from the original image (inverse transform) for (i=0; i 0 && y > 0) for (k=0; k