Water Wave Filter 模擬水面波紋對影像的折射擾動。以同心圓波傳播模型,像素沿徑向位移,位移量隨距離呈正弦波動,形成動態水波視覺效果。
wavelength、振幅 amplitude、相位 phase、中心 (centreX, centreY)、影響半徑 radius。(x, y),計算相對中心距離 distance。distance > radius,保持原位。amount = amplitude * sin(distance / wavelength * 2π - phase) 基礎正弦波。amount *= (radius - distance) / radius 邊緣衰減包絡。amount *= wavelength / (distance + ε) 能量守恆修正(可選)。new_x = x + dx * amount,new_y = y + dy * amount(沿徑向位移)。#include <opencv2/opencv.hpp>
#include <cmath>
#include <algorithm>
using namespace cv;
using namespace std;
void showImage(const Mat& img, const string& name) {
namedWindow(name, WINDOW_AUTOSIZE);
imshow(name, img);
}
int main() {
Mat src = imread("4.jpg");
if (src.empty()) return -1;
Mat dst(src.size(), src.type());
src.copyTo(dst);
int w = src.cols, h = src.rows;
float cx = w * 0.25f, cy = h * 0.5f;
float radius = 0.0f;
if (radius == 0) radius = min(w, h) * 0.5f;
float radius2 = radius * radius;
int wavelength = 20;
int amplitude = 30;
float phase = CV_PI / 4;
for (int y = 0; y < h; ++y) {
for (int x = 0; x < w; ++x) {
float dx = x - cx, dy = y - cy;
float dist2 = dx*dx + dy*dy;
float nx = x, ny = y;
if (dist2 < radius2 && dist2 > 0) {
float distance = sqrt(dist2);
float amount = amplitude * sin(distance / wavelength * 2*CV_PI - phase);
amount *= (radius - distance) / radius; // 邊緣衰減
amount *= wavelength / (distance + 1e-4f); // 能量修正
nx = x + dx * amount;
ny = y + dy * amount;
}
// 邊界裁剪
nx = max(0.0f, min(nx, w - 2.0f));
ny = max(0.0f, min(ny, h - 2.0f));
int x1 = (int)nx, y1 = (int)ny;
float px = nx - x1, py = ny - y1;
for (int k = 0; k < 3; ++k) {
dst.at<Vec3b>(y, x)[k] = saturate_cast<uchar>(
(1-px)*(1-py)*src.at<Vec3b>(y1, x1)[k] +
px*(1-py)*src.at<Vec3b>(y1, x1+1)[k] +
(1-px)*py*src.at<Vec3b>(y1+1, x1)[k] +
px*py*src.at<Vec3b>(y1+1, x1+1)[k]
);
}
}
}
showImage(dst, "Water Wave");
waitKey(0);
return 0;
}
將 phase 隨時間線性遞增(phase += speed * dt),在迴圈中重複渲染即可產生動態水波動畫。配合 VideoWriter 可輸出影片。
| 參數 | 增加效果 | 減少效果 |
|---|---|---|
| wavelength | 波紋間距變大、波數減少 | 波紋密集、高頻擾動 |
| amplitude | 位移幅度大、扭曲強烈 | 微弱擾動、近似原圖 |
| phase | 波紋向外傳播(動畫) | 波紋向內收斂 |
| radius | 影響範圍擴大 | 局部水波效果 |
