图像扭曲之万花筒

源码:

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(h, w)[0] = src.at(nh, nw)[0];
            dst.at(h, w)[1] = src.at(nh, nw)[1];
            dst.at(h, w)[2] = src.at(nh, nw)[2];
        }
    }
}

效果:

图像扭曲之万花筒_第1张图片

你可能感兴趣的:(OpenCV,图像视频处理,C++,图像处理,opencv)