Qt图像处理技术七:轮廓提取
效果图
原理
图像先二值化让rgb数值相同,只有(0,0,0)或者(255,255,255)
取每个点的周围8个点,如果周围8个点与该点rgb值相同,则需要将该点描黑为(255,255,255)
代码
QImage ContourExtraction(const QImage &img)
{
int width = img.width();
int height = img.height();
int pixel[8];
QImage binImg = Binaryzation(img);
QImage newImg = QImage(width, height, QImage::Format_RGB888);
newImg.fill(Qt::white);
uint8_t *rgb = newImg.bits();
uint8_t *binrgb = binImg.bits();
int nRowBytes = (width * 24 + 31) / 32 * 4;
int lineNum_24 = 0;
for (int y = 1; y < height - 1; y++) {
for (int x = 1; x < width - 1; x++) {
memset(pixel, 0, 8);
lineNum_24 = y * nRowBytes;
if (binrgb[lineNum_24 + x * 3] == 0) {
rgb[lineNum_24 + x * 3] = 0;
rgb[lineNum_24 + x * 3 + 1] = 0;
rgb[lineNum_24 + x * 3 + 2] = 0;
pixel[0] = binrgb[(y - 1) * nRowBytes + (x - 1) * 3];
pixel[1] = binrgb[(y) * nRowBytes + (x - 1) * 3];
pixel[2] = binrgb[(y + 1) * nRowBytes + (x - 1) * 3];
pixel[3] = binrgb[(y - 1) * nRowBytes + (x) * 3];
pixel[4] = binrgb[(y + 1) * nRowBytes + (x) * 3];
pixel[5] = binrgb[(y - 1) * nRowBytes + (x + 1) * 3];
pixel[6] = binrgb[(y) * nRowBytes + (x + 1) * 3];
pixel[7] = binrgb[(y + 1) * nRowBytes + (x + 1) * 3];
if (pixel[0] + pixel[1] + pixel[2] + pixel[3] + pixel[4] + pixel[5] + pixel[6] + pixel[7] == 0) {
rgb[lineNum_24 + x * 3] = 255;
rgb[lineNum_24 + x * 3 + 1] = 255;
rgb[lineNum_24 + x * 3 + 2] = 255;
}
}
}
}
return newImg;
}