图像扭曲之旋转

源码:

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(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)