Qt图像处理技术七:轮廓提取

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;
}


//该方法供理解,因为setPixel更慢
QImage QImageAPI::ContourExtraction(const QImage &origin)
{
    int width = origin.width();
    int height = origin.height();
    int pixel[8];   // 当前像素周围的8个像素的像素值
//    int *pixel = new int[9];
    QImage binImg = Binaryzation(origin);
    QImage newImg = QImage(width, height, QImage::Format_RGB888);
    newImg.fill(Qt::white);

    for (int y = 1; y < height; y++) {
        for (int x = 1; x < width; x++) {
            memset(pixel, 0, 8);

            if (QColor(binImg.pixel(x, y)).red() == 0) {
                newImg.setPixel(x, y, qRgb(0, 0, 0));
                pixel[0] = QColor(binImg.pixel(x - 1, y - 1)).red();
                pixel[1] = QColor(binImg.pixel(x - 1, y)).red();
                pixel[2] = QColor(binImg.pixel(x - 1, y + 1)).red();
                pixel[3] = QColor(binImg.pixel(x, y - 1)).red();
                pixel[4] = QColor(binImg.pixel(x, y + 1)).red();
                pixel[5] = QColor(binImg.pixel(x + 1, y - 1)).red();
                pixel[6] = QColor(binImg.pixel(x + 1, y)).red();
                pixel[7] = QColor(binImg.pixel(x + 1, y + 1)).red();
                if (pixel[0] + pixel[1] + pixel[2] + pixel[3] + pixel[4] + pixel[5] + pixel[6] + pixel[7] == 0)
                    newImg.setPixel(x, y, qRgb(255, 255, 255));
            }
        }
    }

    return newImg;
}
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夏有凉风,冬有雪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值