1. 程式人生 > >影象模糊的處理方法

影象模糊的處理方法

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);