Last active
December 15, 2015 14:29
-
-
Save EricCrosson/80e1670ab5f01fa8fdc7 to your computer and use it in GitHub Desktop.
A convolution function that takes a kernel and an image.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = &quot;&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