QRgb convolute
(const QList<int>
&kernel,
const QImage &image,
int x,
int y
){ // assuming kernel is 3x3
int total = 0;
int red = qRed(image.pixel(x,y));
int green = qGreen(image.pixel(x,y));
int blue = qBlue(image.pixel(x,y));
for(int r = -1 ; r<=1; ++r){
for(int c = -1; c<=1; ++c){
int kerVal = kernel.at((1+r)*3+(1+c));
total+=kerVal;
red += qRed(image.pixel(x+c, y+r));
green += qGreen(image.pixel(x+c, y+r));
blue += qBlue(image.pixel(x+c, y+r));
}
}
return QRgb(qBound(0, red/total, 255), qBound(0, green/total, 255), qBound(0, blue/total, 255));
}
QRgb convolute(const QList<int> &kernel, const QImage &image, int x, int y){
// assuming kernel is 3x3
int total = 0;
int red = qRed(image.pixel(x,y));
int green = qGreen(image.pixel(x,y));
int blue = qBlue(image.pixel(x,y));
for(int r = -1 ; r<=1; ++r){
for(int c = -1; c<=1; ++c){
int kerVal = kernel.at((1+r)*3+(1+c));
total+=kerVal;
red += qRed(image.pixel(x+c, y+r));
green += qGreen(image.pixel(x+c, y+r));
blue += qBlue(image.pixel(x+c, y+r));
}
}
return QRgb(qBound(0, red/total, 255), qBound(0, green/total, 255), qBound(0, blue/total, 255));
}
To copy to clipboard, switch view to plain text mode
#include <QtGui>
QRgb convolute
(const QList<int>
&kernel,
const QImage &image,
int x,
int y
){ int kernelsize = sqrt(kernel.size());
qreal total = 0;
qreal red = 0;
qreal green = 0;
qreal blue = 0;
for(int r = -kernelsize/2 ; r<=kernelsize/2; ++r){
for(int c = -kernelsize/2; c<=kernelsize/2; ++c){
int kerVal = kernel.at((kernelsize/2+r)*kernelsize+(kernelsize/2+c));
total+=kerVal;
red += qRed(image.pixel(x+c, y+r))*kerVal;
green += qGreen(image.pixel(x+c, y+r))*kerVal;
blue += qBlue(image.pixel(x+c, y+r))*kerVal;
}
}
if(total==0)
return qRgb(qBound(0, qRound(red), 255), qBound(0, qRound(green), 255), qBound(0, qRound(blue), 255));
return qRgb(qBound(0, qRound(red/total), 255), qBound(0, qRound(green/total), 255), qBound(0, qRound(blue/total), 255));
}
int main(int argc, char **argv){
QList<int> sharpen, blur, laplace;
sharpen << 0 << 0 << 0 << 0 << 0
<< 0 << 0 << -1 << 0 << 0
<< 0 << -1 << 5 << -1 << 0
<< 0 << 0 << -1 << 0 << 0
<< 0 << 0 << 0 << 0 << 0;
blur << 0 << 0 << 1 << 0 << 0
<< 0 << 1 << 3 << 1 << 0
<< 1 << 3 << 7 << 3 << 1
<< 0 << 1 << 3 << 1 << 0
<< 0 << 0 << 1 << 0 << 0;
laplace << -1 << -1 << -1 << -1 << -1
<< -1 << -1 << -1 << -1 << -1
<< -1 << -1 << 24 << -1 << -1
<< -1 << -1 << -1 << -1 << -1
<< -1 << -1 << -1 << -1 << -1;
for(int r=2;r<img.height()-2;r++){
for(int c=2;c<img.width()-2;c++){
dest.setPixel(c, r, convolute(sharpen, img, c, r));
}
}
label.
setPixmap(QPixmap::fromImage(dest
));
label.show();
lab2.
setPixmap(QPixmap::fromImage(img
));
lab2.show();
return app.exec();
}
#include <QtGui>
QRgb convolute(const QList<int> &kernel, const QImage &image, int x, int y){
int kernelsize = sqrt(kernel.size());
qreal total = 0;
qreal red = 0;
qreal green = 0;
qreal blue = 0;
for(int r = -kernelsize/2 ; r<=kernelsize/2; ++r){
for(int c = -kernelsize/2; c<=kernelsize/2; ++c){
int kerVal = kernel.at((kernelsize/2+r)*kernelsize+(kernelsize/2+c));
total+=kerVal;
red += qRed(image.pixel(x+c, y+r))*kerVal;
green += qGreen(image.pixel(x+c, y+r))*kerVal;
blue += qBlue(image.pixel(x+c, y+r))*kerVal;
}
}
if(total==0)
return qRgb(qBound(0, qRound(red), 255), qBound(0, qRound(green), 255), qBound(0, qRound(blue), 255));
return qRgb(qBound(0, qRound(red/total), 255), qBound(0, qRound(green/total), 255), qBound(0, qRound(blue/total), 255));
}
int main(int argc, char **argv){
QApplication app(argc, argv);
QImage img(argv[1]);
QImage dest = img;
QList<int> sharpen, blur, laplace;
sharpen << 0 << 0 << 0 << 0 << 0
<< 0 << 0 << -1 << 0 << 0
<< 0 << -1 << 5 << -1 << 0
<< 0 << 0 << -1 << 0 << 0
<< 0 << 0 << 0 << 0 << 0;
blur << 0 << 0 << 1 << 0 << 0
<< 0 << 1 << 3 << 1 << 0
<< 1 << 3 << 7 << 3 << 1
<< 0 << 1 << 3 << 1 << 0
<< 0 << 0 << 1 << 0 << 0;
laplace << -1 << -1 << -1 << -1 << -1
<< -1 << -1 << -1 << -1 << -1
<< -1 << -1 << 24 << -1 << -1
<< -1 << -1 << -1 << -1 << -1
<< -1 << -1 << -1 << -1 << -1;
for(int r=2;r<img.height()-2;r++){
for(int c=2;c<img.width()-2;c++){
dest.setPixel(c, r, convolute(sharpen, img, c, r));
}
}
QLabel label;
label.setPixmap(QPixmap::fromImage(dest));
label.show();
QLabel lab2;
lab2.setPixmap(QPixmap::fromImage(img));
lab2.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks