中值滤波 Qt代码 内含OpenCV类

#include 
#include 
#include 
#include 

using namespace cv;
using namespace std;

int main()
{
    double duration;
    Mat src = imread("salt.bmp",0);
    Mat src_border(src.rows+2, src.cols+2, CV_8UC1, Scalar(0));
    Mat dst = imread("salt.bmp",0);
    uchar array[9] = {0};
duration = static_cast(getTickCount());

    for ( int i = 0; i < src.rows; i ++ )//边界扩充
    {
        uchar* src_ptr = src.ptr(i);
        uchar* src_border_ptr = src_border.ptr(i+1);
        for ( int j = 0; j < src.cols; j ++ )
        {
            src_border_ptr[j+1] = src_ptr[j];
        }
    }

    for ( int i = 1; i < src_border.rows-1; i ++ )
    {
        uchar* src_border_previous = src_border.ptr(i-1);
        uchar* src_border_current = src_border.ptr(i);
        uchar* src_border_next = src_border.ptr(i+1);
        uchar* dst_ptr = dst.ptr(i-1);
        for ( int j = 1; j < src_border.cols-1; j ++ )
        {
            if ( src_border_current[j] == 255 )
            {
                count ++;
                array[0] = src_border_previous[j-1];
                array[1] = src_border_previous[j];
                array[2] = src_border_previous[j+1];
                array[3] = src_border_current[j-1];
                array[4] = src_border_current[j];
                array[5] = src_border_current[j+1];
                array[6] = src_border_next[j-1];
                array[7] = src_border_next[j];
                array[8] = src_border_next[j+1];
                uchar temp = 0;
                for ( int n = 0; n < 8; n ++)//冒泡排序
                    for ( int m = 0; m < 8-n; m ++ )
                    {
                        if ( array[m] > array[m+1] )
                        {
                            temp = array[m];
                            array[m] = array[m+1];
                            array[m+1] = temp;
                        }
                    }
                dst_ptr[j-1] = array[4];
            }
        }
    }
duration = static_cast(getTickCount()) - duration;
duration /= getTickFrequency();
cout << duration << endl;
    namedWindow("src", 0);
    imshow("src", src);
    namedWindow("dst", 0);
    imshow("dst", dst);
    waitKey(0);
    return 0;
}


其中输入图像为加入椒盐噪声的 bmp 图片

你可能感兴趣的:(Image,algorithm)