源码:
void kaleidoscope(cv::Mat& src,cv::Mat& dst,double angle,double radius)
{
dst.create(src.rows, src.cols, CV_8UC3);
dst.setTo(0);
int cx = src.cols / 2;
int cy = src.rows / 2;
//angle = PI / 4;
double angle2 = PI / 4;
double sides= radius / 3;
double k = angle / 10;
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;
int r = sqrt(dx * dx + dy * dy);
double theta = atan2(dy, dx)- angle - angle2;
double temp_theta= theta / PI * sides *0.5 ;
theta = 0.5 - abs(k * temp_theta - 0.5);
double c=cos(theta);
double radius_c = radius / c;
r = radius_c * (0.5 - abs(r/radius_c - 0.5));
theta = theta+angle;
nw = r * cos(theta) + cx;
nh = r * sin(theta) + cy;
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];
}
}
}
效果: