2016-07-18 14 views
2

我想通過指針執行平均平滑。使用圖像平滑過程圖像指針 - 需要幫助來糾正我的代碼

我的目標是通過指針處理圖像。

這就是我編碼。

代碼:

int main() { 

    Mat img = imread("best-testimg.jpg"); 

    Mat src = img.clone(); 

    int const templeteWindowSize = 3; 

    int tr = templeteWindowSize >> 1; //templateRegion 

    int bb = tr;     //backgroundBorder 

    //create large size image for bounding box; 
    Mat im; 
    copyMakeBorder(src, im, bb, bb, bb, bb, cv::BORDER_DEFAULT); //left= right= bottom= top = border = bb 

    //========================================================= 

    Mat dest = Mat::zeros(src.size(), src.type()); 

    int T = templeteWindowSize * templeteWindowSize; 

    int count = T - 1; 

    //step of template to image 
    const int cstep = im.step - templeteWindowSize; 

    for (int j = 0; j < src.rows; j++) { 

     //src ptr 
     uchar* d = dest.ptr(j); 

     for (int i = 0; i < src.cols; i++) { 

      uchar* tprt = im.data + im.step*(tr + j) + (tr + i); //pointer of image data amd step at 3x3 

      double tweight = 0.0; 
      for (int n = templeteWindowSize; n--;) 
      { 
       for (int m = templeteWindowSize; m--;) 
       { 
        tweight += *tprt++; 
       } 
       tprt += cstep; 
      } 
      tweight /= 9; 
      *(d++) = (saturate_cast<uchar>(tweight)); 
     }//i 

    }//j  
    imshow("dest", dest); 
    waitKey(0); 
    return 0; 
} 

輸入圖像:

enter image description here

輸出圖像

enter image description here

問題:指針處理不好我的圖像。任何人都可以糾正我的代碼錯誤。謝謝

任何人都可以請幫我糾正我的代碼。謝謝 。

+0

嘗試應用不同的線性濾波器來平滑使用OpenCV函數如圖像: 模糊 高斯模糊 medianBlur 雙邊濾波器 http://docs.opencv.org/2.4/doc/tutorials/imgproc/gausian_median_blur_bilateral_filter/gausian_median_blur_bilateral_filter .html –

+0

請讓我有興趣使用指針。僅平均平滑。 – Abc

回答

1

嘗試

Mat src = imread("best-testimg.jpg", IMREAD_GRAYSCALE); 

看來只有圖像三分之一的處理,因爲你不考慮通道數。

+0

我只能使用灰度圖像。 – Abc

+1

是的,你知道圖像是灰度的,但你必須給予imread()一個提示,否則它會返回一個帶有三個相同通道的圖像。 – bgp2000

+0

哦,你說得對。我忘了添加IMREAD_GRAYSCALE .. 謝謝:) – Abc

1

首先,你必須檢查你的代碼。這似乎需要檢修,例如:

  • 爲什麼intern_count
  • 爲什麼DEST不包括同一TR步驟,以避免德邊境

在任何情況下,看看到最後線。你先用saturate_cast先除以9.先分開,然後投。

+0

謝謝,是的,我已經糾正它。我也會在這裏編輯代碼。 – Abc

+0

好的,我已經刪除了像intern_count這樣的無用變量。 } 而且我編輯邊界'IM'即時圖像。並獲得平均值並將值賦給'dest'圖像,該圖像沒有邊框。 。 因此,我不能在dest圖像指針中添加相同的tr。 – Abc