10-图像均值滤波,底层实现

void OpenCVStudy::slot_ProcessImage()
{
    Mat ResImg;
    //转灰度图
    //cvtColor(srcimg, ResImg, COLOR_BGR2GRAY);

    int h = srcimg.rows;
    int w = srcimg.cols;

    // 3x3 均值滤波
    Mat dst = srcimg.clone();
    for (int row = 1; row < h - 1; row++) {
        for (int col = 1; col < w - 1; col++) {
            Vec3b pixel1 = srcimg.at<Vec3b>(row - 1, col - 1);
            Vec3b pixel2 = srcimg.at<Vec3b>(row - 1, col);
            Vec3b pixel3 = srcimg.at<Vec3b>(row - 1, col + 1);
            Vec3b pixel4 = srcimg.at<Vec3b>(row, col - 1);
            Vec3b pixel5 = srcimg.at<Vec3b>(row, col);
            Vec3b pixel6 = srcimg.at<Vec3b>(row, col + 1);
            Vec3b pixel7 = srcimg.at<Vec3b>(row + 1, col - 1);
            Vec3b pixel8 = srcimg.at<Vec3b>(row + 1, col);
            Vec3b pixel9 = srcimg.at<Vec3b>(row + 1, col + 1);

            int channelR = pixel1[0] + pixel2[0] + pixel3[0] + pixel4[0] + pixel5[0] + pixel6[0] + pixel7[0] + pixel8[0] + pixel9[0];
            int channelG = pixel1[1] + pixel2[1] + pixel3[1] + pixel4[1] + pixel5[1] + pixel6[1] + pixel7[1] + pixel8[1] + pixel9[1];
            int channelB = pixel1[2] + pixel2[2] + pixel3[2] + pixel4[2] + pixel5[2] + pixel6[2] + pixel7[2] + pixel8[2] + pixel9[2];

            dst.at<Vec3b>(row, col)[0] = saturate_cast<uchar>(channelR / 9);
            dst.at<Vec3b>(row, col)[1] = saturate_cast<uchar>(channelG / 9);
            dst.at<Vec3b>(row, col)[2] = saturate_cast<uchar>(channelB / 9);
        }
    }

    QImage disImage;
    matToQImage(dst, disImage);
    ui.labelResult->setPixmap(QPixmap::fromImage(disImage));
    ui.labelResult->setScaledContents(true);
}
貌似转换通道除了问题!

关注公众号,更多知识内容

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值