Qt图像处理技术六:拉普拉斯锐化
效果图
源码
由该公式得到下方卷积核
使用到的卷积核:
//都把QImage转化为rgb888更好运算
QImage LaplaceSharpen(const QImage &origin)
{
QImage imgCopy;
int width = img.width();
int height = img.height();
int window[3][3] = {0, -1, 0, -1, 4, -1, 0, -1, 0};
if (img.format() != QImage::Format_RGB888) {
imgCopy = QImage(width, height, QImage::Format_RGB888);
} else {
imgCopy = QImage(img);
}
QImage imgCopyrgbImg = QImage(img).convertToFormat(QImage::Format_RGB888);
uint8_t *rgbImg = imgCopyrgbImg.bits();
uint8_t *rgb = imgCopy.bits();
int nRowBytes = (width * 24 + 31) / 32 * 4;
int lineNum_24 = 0;
for (int x = 1; x < img.width(); x++) {
for (int y = 1; y < img.height(); y++) {
int sumR = 0;
int sumG = 0;
int sumB = 0;
for (int m = x - 1; m <= x + 1; m++)
for (int n = y - 1; n <= y + 1; n++) {
if (m >= 0 && m < width && n < height) {
lineNum_24 = n * nRowBytes;
sumR += rgbImg[lineNum_24 + m * 3] * window[n - y + 1][m - x + 1];
sumG += rgbImg[lineNum_24 + m * 3 + 1] * window[n - y + 1][m - x + 1];
sumB += rgbImg[lineNum_24 + m * 3 + 2] * window[n - y + 1][m - x + 1];
}
}
int old_r = rgbImg[lineNum_24 + x * 3];
sumR += old_r;
sumR = qBound(0, sumR, 255);
int old_g = rgbImg[lineNum_24 + x * 3 + 1];
sumG += old_g;
sumG = qBound(0, sumG, 255);
int old_b = rgbImg[lineNum_24 + x * 3 + 2];
sumB += old_b;
sumB = qBound(0, sumB, 255);
lineNum_24 = y * nRowBytes;
rgb[lineNum_24 + x * 3] = sumR;
rgb[lineNum_24 + x * 3 + 1] = sumG;
rgb[lineNum_24 + x * 3 + 2] = sumB;
}
}
return imgCopy;
}