源码:
void twirl(cv::Mat& src,cv::Mat& dst,double angle,double radius)
{
dst.create(src.rows, src.cols, CV_8UC3);
dst.setTo(0);
int radius2=radius*radius;
int cx = src.cols / 2;
int cy = src.rows / 2;
int distance,distance2 = 0;
for (int h = 0; h < dst.rows; h ++) {
for (int w = 0; w < dst.cols; w ++) {
int nh,nw;
int dx = w - cx;
int dy = h - cy;
distance2 = dx * dx + dy * dy;
if (distance2 > radius2)
{
nw = w;
nh = h;
}
else
{
distance = sqrt(distance2);
double a = atan2(dy, dx) + angle * (radius - distance) / radius;
nw = cx + distance*cos(a);
nh = cy + distance*sin(a);
}
if (nw < 0 || nw > dst.cols - 1)
nw = w;
if (nh < 0 || nh > dst.rows - 1)
nh = h;
dst.at<Vec3b>(h, w)[0] = src.at<Vec3b>(nh, nw)[0];
dst.at<Vec3b>(h, w)[1] = src.at<Vec3b>(nh, nw)[1];
dst.at<Vec3b>(h, w)[2] = src.at<Vec3b>(nh, nw)[2];
}
}
}
原图及效果: