Facebook
From Harmless Treeshrew, 6 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 223
  1. cv::Vec3b determinePixelValueByConv(cv::Mat& neigbourhood){
  2.         int coeffs[3][3] = { { -1, -1, -1 }, { -1, 8, -1 }, { -1, -1, -1 } };
  3.  
  4.         cv::Mat_<cv::Vec3b> _neigbourhood = neigbourhood;
  5.         cv::Vec3b centralPixel = _neigbourhood(1, 1);
  6.         cv::Vec3b outputPixel = cv::Vec3b();
  7.  
  8.         for (int k = 0; k < 3; k++){
  9.                 int product_sum = 0;
  10.                 for (int i = 0; i < _neigbourhood.rows; ++i){
  11.                         for (int j = 0; j < _neigbourhood.cols; ++j){
  12.                                 product_sum += (_neigbourhood(i, j)[k] * coeffs[i][j]);
  13.                         }
  14.                 }
  15.                 outputPixel[k] = cutToRange((int)(product_sum));
  16.         }
  17.        
  18.         return outputPixel;
  19.  
  20. }
  21.  
  22.  
  23.  
  24. cv::Mat edgingConvolution(cv::Mat& I){
  25.         cv::Mat imageCopy = shallowCopy(I);
  26.         cv::Mat_<cv::Vec3b> _I = I;
  27.  
  28.         for (int i = 1; i < I.rows - 1; i++){
  29.                 for (int j = 1; j < I.cols - 1; j++){
  30.                         cv::Mat neigbourhood = imageCopy(cv::Rect(j - 1, i - 1, 3, 3));
  31.                         cv::Vec3b outputPixel = determinePixelValueByConv(neigbourhood);
  32.                         _I(i, j)[0] = outputPixel[0];
  33.                         _I(i, j)[1] = outputPixel[1];
  34.                         _I(i, j)[2] = outputPixel[2];
  35.                 }
  36.         }
  37.         I = _I;
  38.         return I;
  39.  
  40. }