图像模糊的处理方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011808673/article/details/83090326

1.对焦模糊

均值滤波

高斯滤波

2.运动模糊

运动模糊的数学原理

运动模糊,是在拍摄设备快门打开的时间内,物体在成像平面上的投影发生平移或旋转,使接受的影像彼此发生重叠。

        为了便于用数学语言描述图像及其变换,现作如下规定:图像的左上角为坐标原点(0,0),图像的长度方向为x轴,宽度方向为y轴,整个图像落在第一象限。

        假设无任何模糊和噪声的真实图像为f(x,y),模糊图像为g(x,y)。由于运动模糊是由图像彼此重叠造成的,所以成立:


        其中,Cx为图像在方向上的平移速度,Cy为在方向上的平移速度,T为快门打开时间即产生模糊图像的时间,n(x,y)为加性噪声。

计算机实现细节

        为了简化计算过程,我假设只有运动模糊而没有任何加性噪声,而且产生模糊的运动是沿x方向的。

        对于真实图像,模糊图像自然是原始图像的叠加,但对于数字图像,由于像素信息由数值表示,不能简单地将相应像素值相加,而是将像素信息缩小后相加,否则会使亮度成倍增加,使图像严重失真。
 

cv::Mat motionblur(cv::Mat in , unsigned int steps_x, unsigned int steps_y)
{
    // steps_x steps_y should >= 2.
    int rows = in.rows;
    int cols = in.cols;
    int channel = in.channels();
    cv::Mat out(rows, cols, CV_8UC(channel));
    int element = cols * channel;

    if (steps_x != 0) {
        for (size_t i = 0; i < rows; i++)
        {
            uchar * ptr_in = in.ptr<uchar>(i);
            uchar * ptr_out = out.ptr<uchar>(i);
            float sum_b = 0, sum_g = 0, sum_r = 0;
            for (size_t j = 0; j < element; j += channel)
            {
                if ((j / channel) < steps_x) {
                    if (channel == 3) {
                        sum_b += ptr_in[j];
                        sum_g += ptr_in[j + 1];
                        sum_r += ptr_in[j + 2];

                        ptr_out[j] = static_cast<uchar>(sum_b / ((j / channel) + 1));
                        ptr_out[j + 1] = static_cast<uchar>(sum_g / ((j / channel) + 1));
                        ptr_out[j + 2] = static_cast<uchar>(sum_r / ((j / channel) + 1));
                    }
                    else if (channel == 1) {
                        sum_b += ptr_in[j];
                        ptr_out[j] = static_cast<uchar>(sum_b / (j + 1));
                    }

                }
                else {
                    if (channel == 3) {
                        sum_b = sum_b + ptr_in[j] - ptr_in[j - steps_x * channel];
                        sum_g = sum_g + ptr_in[j + 1] - ptr_in[j - steps_x * channel + 1];
                        sum_r = sum_r + ptr_in[j + 2] - ptr_in[j - steps_x * channel + 2];

                        ptr_out[j] = static_cast<uchar>(sum_b / steps_x);
                        ptr_out[j + 1] = static_cast<uchar>(sum_g / steps_x);
                        ptr_out[j + 2] = static_cast<uchar>(sum_r / steps_x);
                    }
                    else if (channel == 1) {
                        sum_b = sum_b + ptr_in[j] - ptr_in[j - steps_x];
                        ptr_out[j] = static_cast<uchar>(sum_b / steps_x);
                    }

                }
            }
        }
    }
    
    if (steps_y != 0) {
        for (size_t i = 0; i < cols; i++)
        {
            float sum_b = 0, sum_g = 0, sum_r = 0;
            for (size_t j = 0; j < rows; j++)
            {
                if (j < steps_y) {
                    if (channel == 3) {
                        sum_b += in.at<cv::Vec3b>(j, i)[0];
                        sum_g += in.at<cv::Vec3b>(j, i)[1];
                        sum_r += in.at<cv::Vec3b>(j, i)[2];

                        out.at<cv::Vec3b>(j, i)[0] = static_cast<uchar>(sum_b / (j + 1));
                        out.at<cv::Vec3b>(j, i)[1] = static_cast<uchar>(sum_g / (j + 1));
                        out.at<cv::Vec3b>(j, i)[2] = static_cast<uchar>(sum_r / (j + 1));
                    }
                    else if(channel == 1){
                        sum_b += in.at<uchar>(j, i);
                        out.at<uchar>(j, i) = static_cast<uchar>(sum_b / (j + 1));
                    }
                }
                else {
                    if (channel == 3) {
                        sum_b = sum_b + in.at<cv::Vec3b>(j, i)[0] - in.at<cv::Vec3b>(j - steps_y, i)[0];
                        sum_g = sum_g + in.at<cv::Vec3b>(j, i)[1] - in.at<cv::Vec3b>(j - steps_y, i)[1];
                        sum_r = sum_r + in.at<cv::Vec3b>(j, i)[2] - in.at<cv::Vec3b>(j - steps_y, i)[2];

                        out.at<cv::Vec3b>(j, i)[0] = static_cast<uchar>(sum_b / steps_y);
                        out.at<cv::Vec3b>(j, i)[1] = static_cast<uchar>(sum_g / steps_y);
                        out.at<cv::Vec3b>(j, i)[2] = static_cast<uchar>(sum_r / steps_y);
                    } else if(channel == 1) {
                        sum_b = sum_b + in.at<uchar>(j, i) - in.at<uchar>(j-steps_y, i);
                        out.at<uchar>(j, i) = static_cast<uchar>(sum_b / steps_y);
                    }
                }
                
            }
        }
    }
    return out;
}

实现效果

原图

处理之后的图

3.径向模糊

(与运动模糊原理一致)

PS里称为 径向模糊->缩放, 还有一种 径向模糊->旋转。

缩放的原理:

确定一个中心点(如0.5, 0.5), 跟当前像素连一条线. 以当前像素为中心, 在线上的附近像素进行采样, 最后取一下平均值.

同样,旋转的原理相似,就是相同半径的一定邻域内的均值。

缩放时,半径变化,旋转时,角度变化。

使用OpenCV实现

原图:

径向模糊->缩放:

#include <math.h>
#include <opencv/cv.h>
#include <opencv/highgui.h>
 
using namespace cv;
using namespace std;
 
int num=40;//num:均值力度;
 
int main()
{
    Mat src = imread("D:/test3.jpg",1);
    Mat src1u[3];
    split(src,src1u);
 
    int width=src.cols;
    int heigh=src.rows;
    Mat img;
    src.copyTo(img);
    Point center(width/2,heigh/2);
 
 
    for (int y=0; y<heigh; y++)
    {
        
        uchar *imgP  = img.ptr<uchar>(y);
 
        for (int x=0; x<width; x++)
        {
            int R = norm(Point(x,y)-center);
            double angle = atan2((double)(y-center.y),(double)(x-center.x));
 
            int tmp0=0,tmp1=0,tmp2=0;
            
            for (int i=0;i<num;i++)        //num:均值力度 ,i为变化幅度;
            {
                int tmpR = (R-i)>0?(R-i):0;
                
                int newX = tmpR*cos(angle) + center.x;
                int newY = tmpR*sin(angle) + center.y;
                
                if(newX<0)newX=0;
                if(newX>width-1)newX=width-1;
                if(newY<0)newY=0;
                if(newY>heigh-1)newY=heigh-1;
 
                tmp0 += src1u[0].at<uchar>(newY,newX);
                tmp1 += src1u[1].at<uchar>(newY,newX);
                tmp2 += src1u[2].at<uchar>(newY,newX);
 
            }
            imgP[3*x]=(uchar)(tmp0/num);
            imgP[3*x+1]=(uchar)(tmp1/num);
            imgP[3*x+2]=(uchar)(tmp2/num);
        }
        
    }
    imshow("径向模糊",img);
    waitKey();
    imwrite("D:/径向模糊(缩放).jpg",img);
}


径向模糊->旋转:

#include <math.h>
#include <opencv/cv.h>
#include <opencv/highgui.h>
 
using namespace cv;
using namespace std;
 
int num=20;    //均值力度;
 
int main()
{
    Mat src = imread("D:/test3.jpg",1);
    Mat src1u[3];
    split(src,src1u);
 
    int width=src.cols;
    int heigh=src.rows;
    Mat img;
    src.copyTo(img);
    Point center(width/2,heigh/2);
 
 
    for (int y=0; y<heigh; y++)
    {
 
        uchar *imgP  = img.ptr<uchar>(y);
 
        for (int x=0; x<width; x++)
        {
            int R = norm(Point(x,y)-center);
            double angle = atan2((double)(y-center.y),(double)(x-center.x));
 
            int tmp0=0,tmp1=0,tmp2=0;
 
            for (int i=0;i<num;i++)    //均值力度;
            {
 
                angle+=0.01;        //0.01控制变化频率,步长
 
                int newX = R*cos(angle) + center.x;
                int newY = R*sin(angle) + center.y;
 
                if(newX<0)newX=0;
                if(newX>width-1)newX=width-1;
                if(newY<0)newY=0;
                if(newY>heigh-1)newY=heigh-1;
 
                tmp0 += src1u[0].at<uchar>(newY,newX);
                tmp1 += src1u[1].at<uchar>(newY,newX);
                tmp2 += src1u[2].at<uchar>(newY,newX);
 
            }
            imgP[3*x]=(uchar)(tmp0/num);
            imgP[3*x+1]=(uchar)(tmp1/num);
            imgP[3*x+2]=(uchar)(tmp2/num);
        }
 
    }
    imshow("径向模糊",img);
    waitKey();
    imwrite("D:/径向模糊(旋转).jpg",img);

猜你喜欢

转载自blog.csdn.net/u011808673/article/details/83090326