图像处理算法,去噪处理

在图像处理中,经常处理需要过滤处理一些噪声,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;
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_29441995/article/details/81189139