Skip to content

Instantly share code, notes, and snippets.

@Kobzol
Created September 29, 2016 14:21
Show Gist options
  • Select an option

  • Save Kobzol/530b1df9ba5f4792fda4eb7817d13723 to your computer and use it in GitHub Desktop.

Select an option

Save Kobzol/530b1df9ba5f4792fda4eb7817d13723 to your computer and use it in GitHub Desktop.
template <typename T, unsigned int S>
void convolution(cv::Mat& original, cv::Mat& resultImg, T kernel[S][S])
{
original.copyTo(resultImg);
int offset = S / 2;
T scale = 0;
for (int i = 0; i < S; i++)
{
for (int j = 0; j < S; j++)
{
scale += kernel[i][j];
}
}
for (int y = offset; y < original.rows - offset; y++)
{
for (int x = offset; x < original.cols - offset; x++)
{
cv::Vec3i result = cv::Vec3i(0, 0, 0);
for (int i = -1; i < 2; i++)
{
for (int j = -1; j < 2; j++)
{
pixel_t pix = original.at<pixel_t>(y + i, x + j);
cv::Vec3i c = pix;
c *= kernel[i + 1][j + 1];
result += c;
}
}
if (scale == 0)
{
scale = 1;
}
result /= scale;
resultImg.at<pixel_t>(y, x) = result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment