opencv算法學習


1.改變圖像的亮度和對比度:

  g(i,j) = \alpha \cdot f(i,j) + \beta

  算法介紹:對每一點像素值的r,g,b,值進行乘法和加法的運算。

  代碼使用:

for( int y = 0; y < image.rows; y++ )
    {
        for( int x = 0; x < image.cols; x++ )
        {
            for( int c = 0; c < 3; c++ )
            {
                new_image.at<Vec3b>(y,x)[c] = saturate_cast<uchar>( alpha*( image.at<Vec3b>(y,x)[c] ) + beta );
            }
        }
    }

2.opencv中的傅里葉變換:

  主要用途:識別圖片中物體的方向。

  例如:矯正圖片的中文字的排列方向。

  計算公式:

  

  算法介紹:f為空間值,F為頻域值

  代碼使用:

dft(complexI, complexI);

  由於dft變換后是有實數和復數部分,所有要進一步進行處理。

3.圖片的平滑處理:即模糊處理

  算法介紹:通過濾波器濾掉圖像中的噪點,opencv主要提供高斯,中值,雙邊濾波

  代碼使用:

  /// 使用高斯濾波
    if( display_caption( "Gaussian Blur" ) != 0 ) { return 0; }
   for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 )
        { GaussianBlur( src, dst, Size( i, i ), 0, 0 );
          if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } 
     /// 使用中值濾波
     if( display_caption( "Median Blur" ) != 0 ) { return 0; }
     for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 )
         { medianBlur ( src, dst, i );
           if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } }
     /// 使用雙邊濾波
     if( display_caption( "Bilateral Blur" ) != 0 ) { return 0; }
     for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 )
         { bilateralFilter ( src, dst, i, i*2, i/2 );
           if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } }

4.圖像的腐蝕與膨脹

  腐蝕:處理部分被消減了。

  膨脹:處理部分被擴大了。

  算法介紹:與操作的內核或者說矩(圖像)進行卷積操作。

  核說白了就是一個固定大小的數值數組。該數組帶有一個 錨點 ,一般位於數組中央。

  卷積高度概括地說,卷積是在每一個圖像塊與某個算子(核)之間進行的運算。

  腐蝕:

erode( src, erosion_dst, element );

element: 腐蝕操作的內核。 如果不指定,默認為一個簡單的 3x3 矩陣。

  膨脹:
dilate( src, dilation_dst, element );

可以使用函數 getStructuringElement();指定內核的形狀

 

5.圖像的放大與縮小

  算法介紹:對圖像與高斯內核卷積。縮小即是對圖像的4個點像素,處理為一個點的像素。一個點的像素分解為四個點的像素。

pyrUp( tmp, dst, Size( tmp.cols*2, tmp.rows*2 );//放大
pyrDown( tmp, dst, Size( tmp.cols/2, tmp.rows/2 );//縮小

但是opencv中最常用的圖像縮放函數是relize();

6.圖像的閾值操作:

  算法介紹:通過對圖像進行灰度值變換后,進行的色度分離操作,可應用與顏色識別。

 threshold( src_gray, dst, threshold_value, max_BINARY_value,threshold_type );

7.圖像的邊緣檢測:sobel算法

  算法介紹:因為在邊緣處,像素值明顯改變了,我們可以通過求導的辦法,將像素值的改變體現為斜率的變化。

/// 求 X方向梯度
  //Scharr( src_gray, grad_x, ddepth, 1, 0, scale, delta, BORDER_DEFAULT );
  Sobel( src_gray, grad_x, ddepth, 1, 0, 3, scale, delta, BORDER_DEFAULT );
  convertScaleAbs( grad_x, abs_grad_x );

  /// 求Y方向梯度
  //Scharr( src_gray, grad_y, ddepth, 0, 1, scale, delta, BORDER_DEFAULT );
  Sobel( src_gray, grad_y, ddepth, 0, 1, 3, scale, delta, BORDER_DEFAULT );
  convertScaleAbs( grad_y, abs_grad_y );

  /// 合並梯度(近似)
  addWeighted( abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad );

8。laplace算法優化了sobel算法,加入了二階求導

Laplacian( src_gray, dst, ddepth, kernel_size, scale, delta, BORDER_DEFAULT );

9.canny最優的邊緣檢測算法

Canny( detected_edges, detected_edges, lowThreshold, lowThreshold*ratio, kernel_size );

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM