opencv3/C++图像滤波实现

图像滤波在opencv中可以有多种实现形式

自定义滤波

如使用3×3的掩模:
opencv3/C++图像滤波实现_第1张图片
对图像进行处理.

使用函数filter2D()实现

#include
using namespace cv;

int main()
{
    //函数调用filter2D功能
    Mat src,dst;
    src = imread("E:/image/image/daibola.jpg");
    if(!src.data)
    {
        printf("can not load image \n");
        return -1;
    }
    namedWindow("input", CV_WINDOW_AUTOSIZE);
    imshow("input", src);
    src.copyTo(dst);
    Mat kernel = (Mat_<int>(3,3)<<1,1,1,1,1,-1,-1,-1,-1);
    double t = (double)getTickCount();
    filter2D(src, dst, src.depth(), kernel);
    std::cout<<((double)getTickCount()-t)/getTickFrequency()<<std::endl;
    namedWindow("output", CV_WINDOW_AUTOSIZE);
    imshow("output", dst);
    printf("%d",src.channels());
    waitKey();
    return 0;
}

opencv3/C++图像滤波实现_第2张图片
opencv3/C++图像滤波实现_第3张图片

通过像素点操作实现

#include
using namespace cv;
int main()
{
    Mat src, dst;
    src = imread("E:/image/image/daibola.jpg");
    CV_Assert(src.depth() == CV_8U);
    if(!src.data)
    {
        printf("can not load image \n");
        return -1;
    }
    namedWindow("input", CV_WINDOW_AUTOSIZE);
    imshow("input",src);
    src.copyTo(dst);
    for(int row = 1; row<(src.rows - 1); row++)
    {
        const uchar* previous = src.ptr(row - 1);
        const uchar* current = src.ptr(row);
        const uchar* next = src.ptr(row + 1);
        uchar* output = dst.ptr(row);
        for(int col = src.channels(); col < (src.cols - 1)*src.channels(); col++)
        {
            *output = saturate_cast(1 * current[col] + previous[col] - next[col] + current[col - src.channels()] - current[col + src.channels()]);
            output++;
        }
    }
    namedWindow("output", CV_WINDOW_AUTOSIZE);
    imshow("output",dst);
    waitKey();
    return 0;
}

opencv3/C++图像滤波实现_第4张图片
opencv3/C++图像滤波实现_第5张图片

特定形式滤波

常用的有:
blur(src,dst,Size(5,5));均值滤波
GaussianBlur(src,dst,Size(5,5),11,11);高斯滤波
medianBlur(src,dst,5);中值滤波(应对椒盐噪声)
bilateralFilter(src,dst,2,0.5,2,4);双边滤波(保留边缘)

#include
using namespace cv;

int main()
{
    Mat src, dst;
    src = imread("E:/image/image/daibola.jpg");
    CV_Assert(src.depth() == CV_8U);
    if(!src.data)
    {
        printf("can not load image \n");
        return -1;
    }
    namedWindow("input", CV_WINDOW_AUTOSIZE);
    imshow("input",src);
    src.copyTo(dst);
    //均值滤波
    blur(src,dst,Size(5,5));
    //中值滤波
    //medianBlur(src,dst,5);

    namedWindow("output", CV_WINDOW_AUTOSIZE);
    imshow("output",dst);

    waitKey();
    return 0;
}

opencv3/C++图像滤波实现_第6张图片
opencv3/C++图像滤波实现_第7张图片

你可能感兴趣的:(OpenCV)