Skip to content

Instantly share code, notes, and snippets.

@johnnyferreiradev
Created March 16, 2019 12:46
Show Gist options
  • Save johnnyferreiradev/92b6cb5764b7412d993d69f8c7788d23 to your computer and use it in GitHub Desktop.
Save johnnyferreiradev/92b6cb5764b7412d993d69f8c7788d23 to your computer and use it in GitHub Desktop.
Função para realizar uma convolução de forma genérica em uma imagem (OpenCV)
#include <opencv2/opencv.hpp>
Mat convoluirGenerica(Mat& img, float **masc, int M, int N){
float output=0;
Mat resp = img.clone();
int m=(M-1)/2;
int n=(N-1)/2;
for (int x=0; x<img.rows;x++){
for (int y=0; y<img.cols; y++){
resp.at<uchar>(x,y) = 0;
}
}
int i=0,j=0;
for (int x=m; x<img.rows-m;x++){
for (int y=n; y<img.cols-n; y++) {
output=0;
for (int ml=-m; ml<=m; ml++){
for (int mc=-n; mc<=n; mc++) {
output += img.at<uchar>(x-mc,y-ml) * masc[mc+ m][ml+n];
if(output>255)
output=255;
if(output<0)
output=0;
}
}
resp.at<uchar>(x,y) = (int)output;
}
}
return (resp);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment