三角波形扭曲:
void sinwave(cv::Mat& src,cv::Mat& dst)
{
dst.create(src.rows, src.cols, CV_8UC3);
dst.setTo(0);
src.copyTo(dst);
int PI = 3.1415;
int RANGE = dst.cols/2;
for (int i = 0; i < dst.rows; i++) {
double temp = (dst.cols - RANGE) / 2 + (dst.cols - RANGE) * sin((2 * PI * i) / dst.rows + PI) / 2;
for (int j = int(temp + 0.5); j < RANGE + temp; j++) {
int m = (int)((j - temp) * (src.cols) / RANGE);
if (m >= src.cols)
m = src.cols - 1;
if (m < 0)
m = 0;
dst.at<Vec3b>(i, j)[0] = src.at<Vec3b>(i, m)[0];
dst.at<Vec3b>(i, j)[1] = src.at<Vec3b>(i, m)[1];
dst.at<Vec3b>(i, j)[2] = src.at<Vec3b>(i, m)[2];
}
}
}
效果:
鱼眼扭曲:
void fisheye(cv::Mat& src,cv::Mat& dst)
{
dst.create(src.rows, src.cols, CV_8UC3);
dst.setTo(0);
int PI = 3.1415;
/* fish eye*/
for (int id = 0; id < dst.rows; id++) {
for (int jd = 0; jd < dst.cols; jd++) {
double xd = jd * 2.0 / dst.cols - 1.0;
double yd = id * 2.0 / dst.cols - 1.0;
double rd = sqrt(xd * xd + yd * yd);
double phid = atan2(yd, xd);
double xs = asin(rd) * 2 / PI * cos(phid);
double ys = asin(rd) * 2 / PI * sin(phid);
int is = round((ys + 1.0) * dst.rows / 2.0);
int js = round((xs + 1.0) * dst.cols / 2.0);
if (is > dst.rows || is < 0 || js>dst.cols || js < 0){
is = id;
js = jd;
}
dst.at<Vec3b>(id, jd)[0] = src.at<Vec3b>(is, js)[0];
dst.at<Vec3b>(id, jd)[1] = src.at<Vec3b>(is, js)[1];
dst.at<Vec3b>(id, jd)[2] = src.at<Vec3b>(is, js)[2];
}
}
}
效果: