图像处理算法,去噪处理

在图像处理中,经常处理需要过滤处理一些噪声,opencv自带算法不一定能满足我们工程的要求,自己写了一个调暗处理的简单函数,记录下来:

//取ksize大小矩形区域内的最小值替代整个区域的像素值
void Ave2(Mat mat, Mat&ave,int ksize)
{
    Size size(ksize, ksize);
    ave = mat.clone();
    for (int i = 0; i < mat.rows; i++)
    {
        for (int j = 0; j < mat.cols; j++)
        {
            int min = 256;
            for (int m = 0; m < size.width; m++)
            {
                for (int n = 0; n < size.height; n++)
                {
                    int x = m - size.width / 2 + j;
                    int y = n - size.height / 2 + i;
                    if (x < 0 || x >= mat.cols || y < 0 || y >= mat.rows)
                        continue;
                    int data = mat.at<unsigned char>(y, x);
                    if (data < min)
                        min = data;
                }
            }
            ave.at<unsigned char>(i, j) = min;
        }
    }
}

//p点的size范围内如果非0值的数量小于threshold,则置为0
void myBlur(Mat mat,Mat& blur,Size size,int threshold)
{
    blur = mat.clone();
    for (int i = 0; i < mat.rows; i++)
    {
        for (int j = 0; j < mat.cols; j++)
        {
            int count = 0;
            for (int m = 0; m < size.width; m++)
            {
                for (int n = 0; n < size.height; n++)
                {
                    int x = m - size.width / 2 + j;
                    int y = n - size.height / 2 + i;
                    if (x < 0 || x >= mat.cols || y < 0 || y >= mat.rows)
                        continue;
                    int data = mat.at<unsigned char>(y, x);
                    if (data != 0)
                        count++;
                }
            }
            if (count <= threshold)
            {
                blur.at<unsigned char>(i, j)=0;
            }
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值