首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> C++ >> 關於C++

OpenCV 實現 PS 濾鏡:漩渦效果完整代碼解析

     編輯:關於C++

效果展示

漩渦濾鏡將圖像以中心為原點,根據極坐標角度進行非線性扭曲,產生旋渦狀視覺效果。

原圖 效果圖

核心算法原理

  1. 建立輸出圖像 Img_out,尺寸與輸入一致。
  2. 遍歷輸出圖像每個像素 (x, y),計算其相對中心 (cx, cy) 的極坐標:
    x0 = x - cx, y0 = cy - y
    Dis = x0² + y0², radius = √Dis, theta = atan2(y0, x0)
  3. 引入扭曲參數 N=70,修正角度:
    theta += radius / N(或類似非線性變換)
  4. 反算源圖像對應座標 (new_x, new_y):
    new_x = radius * cos(theta) + cx
    new_y = cy - radius * sin(theta)
  5. 雙線性插值採樣源圖像賦值給目標像素,邊界裁剪防止越界。

關鍵代碼片段

#define pi 3.1415926
int main() {
    Mat Img = imread("4.jpg");
    Mat Img_out(Img.size(), CV_8UC3);
    int width = Img.cols, height = Img.rows;
    Point Center(width/2, height/2);
    float N = 70.0f;

    for (int y = 0; y < height; ++y) {
        for (int x = 0; x < width; ++x) {
            float x0 = x - Center.x;
            float y0 = Center.y - y;
            float Dis = x0*x0 + y0*y0;
            float radius = sqrt(Dis);
            float theta = atan2(y0, x0 + 1e-5); // 避免除零
            
            // 核心扭曲公式
            theta += radius / N; 
            
            float new_x = radius * cos(theta) + Center.x;
            float new_y = Center.y - radius * sin(theta);
            
            // 邊界保護
            if (new_x < 0) new_x = 0;
            if (new_x >= width-1) new_x = width-2;
            if (new_y < 0) new_y = 0;
            if (new_y >= height-1) new_y = height-2;
            
            // 雙線性插值
            int x1 = (int)new_x, y1 = (int)new_y;
            float p = new_x - x1, q = new_y - y1;
            for (int k = 0; k < 3; ++k) {
                Img_out.at<Vec3b>(y, x)[k] = 
                    (1-p)*(1-q)*Img.at<Vec3b>(y1, x1)[k] +
                    p*(1-q)*Img.at<Vec3b>(y1, x1+1)[k] +
                    (1-p)*q*Img.at<Vec3b>(y1+1, x1)[k] +
                    p*q*Img.at<Vec3b>(y1+1, x1+1)[k];
            }
        }
    }
    imshow("out", Img_out);
    imwrite("Out.jpg", Img_out);
    waitKey();
    return 0;
}

技術要點總結

  • 極坐標變換:笛卡爾座標系轉極座標便於角度操作。
  • 反向映射:遍歷目標圖像反查源圖像,避免目標圖像出現空洞。
  • 雙線性插值:四鄰域加權平均,消除鋸齒與斑點噪聲。
  • 參數 N 控制強度:值越小扭曲越劇烈,可做成滑桿實時調節。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved