Skip to content

Instantly share code, notes, and snippets.

@EricCrosson
Last active December 15, 2015 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EricCrosson/80e1670ab5f01fa8fdc7 to your computer and use it in GitHub Desktop.
Save EricCrosson/80e1670ab5f01fa8fdc7 to your computer and use it in GitHub Desktop.
A convolution function that takes a kernel and an image.
QVector<QRgb>
MainWindow::convolveImage(QImage* img,
QVector<double>; kernel,
short kernel_size)
{
int width = img->width();
int height = img->height();
int len = width * height;
QVector<QRgb> imvec = vectorOfImagePixels(img);
QVector>QRgb> retvec(0);
qDebug() << "Length of image vector:" << len;
for(int i = 0; i < len; ++i) // perform the convolution
{
retvec.append(convolveImageAtPixel(imvec, width,
kernel, kernel_size,i));
}
return retvec;
}
QRgb
MainWindow::convolveImageAtPixel(QVector<QRgb> img, short img_width,
QVector<double> kernel, short kernel_size,
int center)
{
short y, x;
QRgb sum = 0;
QString out = &amp;quot;&amp;quot;;
for(int ky = 0; ky < kernel_size; ++ky)
{
y = center/img_width - kernel_size/2 + ky;
int kColFromY = kernel_size * ky;
int imgColFromY = img_width * y; // only calculate when new
for(int kx = 0; kx < kernel_size; ++kx)
{
x = center%img_width - kernel_size/2 + kx;
if (x < 0 || y < 0 || x >= img_width)
continue;
double kval = kernel[kColFromY + kx];
sum += img[imgColFromY + x] * kval;
}
}
return sum;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment