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

C++Builder 影像濾鏡統一框架:模糊、銳化、浮雕、邊緣偵測 3x3 卷積核實作

     編輯:VC 教學

3x3 卷積核通用框架

空域線性濾波:輸出像素 = Σ (鄰域像素 × 核權重) / 除數 + 偏移,再鉗制 [0,255]。

核心代碼

void __fastcall ApplyConvolution3x3(Graphics::TBitmap* Src, Graphics::TBitmap* Dst,
    const int Kernel[3][3], int Divisor, int Offset = 0)
{
    if (!Src || !Dst || Src->PixelFormat != pf24bit) return;
    Dst->Assign(Src);  // 複製尺寸/格式

    int W = Src->Width, H = Src->Height;
    for (int y = 1; y < H - 1; ++y) {
        BYTE* dstRow = (BYTE*)Dst->ScanLine[y];
        BYTE* srcRow = (BYTE*)Src->ScanLine[y];
        BYTE* upRow    = (BYTE*)Src->ScanLine[y-1];
        BYTE* downRow  = (BYTE*)Src->ScanLine[y+1];

        for (int x = 3; x < (W - 1) * 3; x += 3) {  // 跳過邊緣 1 像素
            int sumB = 0, sumG = 0, sumR = 0;

            // 3x3 鄰域加權
            for (int ky = -1; ky <= 1; ++ky) {
                BYTE* row = (ky == -1) ? upRow : (ky == 0 ? srcRow : downRow);
                for (int kx = -1; kx <= 1; ++kx) {
                    int w = Kernel[ky+1][kx+1];
                    int idx = x + kx * 3;
                    sumB += row[idx + 0] * w;
                    sumG += row[idx + 1] * w;
                    sumR += row[idx + 2] * w;
                }
            }

            int b = sumB / Divisor + Offset;
            int g = sumG / Divisor + Offset;
            int r = sumR / Divisor + Offset;

            // 鉗制
            b = (b < 0) ? 0 : (b > 255 ? 255 : b);
            g = (g < 0) ? 0 : (g > 255 ? 255 : g);
            r = (r < 0) ? 0 : (r > 255 ? 255 : r);

            dstRow[x + 0] = (BYTE)b;
            dstRow[x + 1] = (BYTE)g;
            dstRow[x + 2] = (BYTE)r;
        }
    }
}

預設核矩陣定義

效果Kernel[3][3]DivisorOffset
模糊{{1,1,1},{1,2,1},{1,1,1}}100
銳化{{0,-1,0},{-1,5,-1},{0,-1,0}}10
浮雕{{-2,-1,0},{-1,1,1},{0,1,2}}1128
邊緣偵測{{-1,-1,-1},{-1,8,-1},{-1,-1,-1}}10
高斯模糊近似{{1,2,1},{2,4,2},{1,2,1}}160
拉普拉斯銳化{{0,-1,0},{-1,4,-1},{0,-1,0}}10

按鈕事件範例


void __fastcall TForm1::btnBlurClick(TObject *Sender) {
    int K[3][3] = {{1,1,1},{1,2,1},{1,1,1}};
    ApplyConvolution3x3(Image1->Picture->Bitmap, bmpWork, K, 10);
    Image1->Picture->Bitmap->Assign(bmpWork);
}

void __fastcall TForm1::btnSharpenClick(TObject *Sender) {
    int K[3][3] = {{0,-1,0},{-1,5,-1},{0,-1,0}};
    ApplyConvolution3x3(Image1->Picture->Bitmap, bmpWork, K, 1);
    Image1->Picture->Bitmap->Assign(bmpWork);
}

void __fastcall TForm1::btnEmbossClick(TObject *Sender) {
    int K[3][3] = {{-2,-1,0},{-1,1,1},{0,1,2}};
    ApplyConvolution3x3(Image1->Picture->Bitmap, bmpWork, K, 1, 128);
    Image1->Picture->Bitmap->Assign(bmpWork);
}

優化方向

  • 分離式濾波:高斯核可分解為水平 1D + 垂直 1D,複雜度 O(2×3×N) → O(2×N)。
  • 整數定點運算:將權重擴大 256 倍 (Q8.8),避免浮點除法,用位移代替除以 256。
  • SIMD (SSE2/AVX2):並行處理 8/16 個像素,使用 _mm_madd_epi16 等指令。
  • 多執行緒:將圖片按行分塊,交給執行緒池並行卷積。
  • 邊緣處理:鏡像延伸、複製邊緣、常數填充,避免邊框黑線。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved