最近有個需求:拍攝證件或紙質檔案上傳時,需要自動將拍攝背景去除,只保留證件或檔案那部分的影象。

先來一張效果圖
這裡寫圖片描述

首先使用opencv提供的CvVideoCamera類來載入視訊流
實現CvVideoCameraDelegate的方法:

- (void)processImage:(Mat &)mat;

這個代理方法能實時獲取攝像頭輸入的每一幀影象

- (void)processImage:(Mat &)mat 
{
    Mat src_gray, filtered, edges, dilated_edges;

    //獲取灰度影象
    cvtColor(mat, src_gray, COLOR_BGR2GRAY);
    //濾波,模糊處理,消除某些背景干擾資訊
    blur(src_gray, filtered, cv::Size(3, 3));
    //腐蝕操作,消除某些背景干擾資訊
    erode(filtered, filtered, Mat(),cv::Point(-1, -1), 3, 1, 1);

    int thresh = 35;
    //邊緣檢測
    Canny(filtered, edges, thresh, thresh*3, 3);
    //膨脹操作,儘量使邊緣閉合
    dilate(edges, dilated_edges, Mat(), cv::Point(-1, -1), 3, 1, 1);

    vector<vector<cv::Point> > contours, squares, hulls;
    //尋找邊框
    findContours(dilated_edges, contours, RETR_LIST, CHAIN_APPROX_SIMPLE);

    vector<cv::Point> hull, approx;
    for (size_t i = 0; i < contours.size(); i++)
    {
        //邊框的凸包
        convexHull(contours[i], hull);
        //多邊形擬合凸包邊框(此時的擬合的精度較低)
        approxPolyDP(Mat(hull), approx, arcLength(Mat(approx), true)*0.02, true);
        //篩選出面積大於某一閾值的,且四邊形的各個角度都接近直角的凸四邊形
        if (approx.size() == 4 && fabs(contourArea(Mat(approx))) > 40000 &&
                isContourConvex(Mat(approx)))
        {
            double maxCosine = 0;
            for (int j = 2; j < 5; j++)
            {
                double cosine = fabs(getAngle(approx[j%4], approx[j-2], approx[j-1]));
                maxCosine = MAX(maxCosine, cosine);
            }
                //角度大概72度
            if (maxCosine < 0.3)
            {
                squares.push_back(approx);
                hulls.push_back(hull);
             }
         }
      }

        vector<cv::Point> largest_square;
        //找出外接矩形最大的四邊形
        int idex = findLargestSquare(squares, largest_square);
        if (largest_square.size() == 0 || idex == -1) return;

        //找到這個最大的四邊形對應的凸邊框,再次進行多邊形擬合,此次精度較高,擬合的結果可能是大於4條邊的多邊形
        //接下來的操作,主要是為了解決,證件有圓角時檢測到的四個頂點的連線會有切邊的問題
        hull = hulls[idex];
        approxPolyDP(Mat(hull), approx, 3, true);
        vector<cv::Point> newApprox;
        double maxL = arcLength(Mat(approx), true)*0.02;
        //找到高精度擬合時得到的頂點中 距離小於 低精度擬合得到的四個頂點 maxL的頂點,排除部分頂點的干擾
        for (cv::Point p : approx) 
        {
            if (!(getSpacePointToPoint(p, largest_square[0]) > maxL && 
                getSpacePointToPoint(p, largest_square[1]) > maxL && 
                getSpacePointToPoint(p, largest_square[2]) > maxL && 
                getSpacePointToPoint(p, largest_square[3]) > maxL)) 
            {
                newApprox.push_back(p);
            }
        }
        //找到剩餘頂點連線中,邊長大於 2 * maxL的四條邊作為四邊形物體的四條邊
        vector<Vec4i> lines;
        for (int i = 0; i < newApprox.size(); i++) 
        {
            cv::Point p1 = newApprox[i];
            cv::Point p2 = newApprox[(i+1)%newApprox.size()];
            if (getSpacePointToPoint(p1, p2) > 2 * maxL) 
            {
                lines.push_back(Vec4i(p1.x, p1.y, p2.x,p2.y));
            }
        }

        //計算出這四條邊中 相鄰兩條邊的交點,即物體的四個頂點
        vector<cv::Point> cornors1;
        for (int i = 0; i < lines.size(); i++) 
        {
            cv::Point cornor = computeIntersect(lines[i],lines[(i+1)%lines.size()]);
            cornors1.push_back(cornor);
        }
        //繪製出四條邊
        for (int i = 0; i < cornors1.size(); i++) 
        {
            line(mat, cornors1[i], cornors1[(i+1)%cornors1.size()], Scalar(0,0,255), 5);
        }
}

相關自定義函式:

#pragma mark =========== 尋找最大邊框 ===========
int findLargestSquare(const vector<vector<cv::Point> >& squares, vector<cv::Point>& biggest_square)
{
    if (!squares.size()) return -1;

    int max_width = 0;
    int max_height = 0;
    int max_square_idx = 0;
    for (int i = 0; i < squares.size(); i++)
    {
        cv::Rect rectangle = boundingRect(Mat(squares[i]));
        if ((rectangle.width >= max_width) && (rectangle.height >= max_height))
        {
            max_width = rectangle.width;
            max_height = rectangle.height;
            max_square_idx = i;
        }
    }
    biggest_square = squares[max_square_idx];
    return max_square_idx;
}

/**
 根據三個點計算中間那個點的夾角   pt1 pt0 pt2
 */
double getAngle(cv::Point pt1, cv::Point pt2, cv::Point pt0)
{
    double dx1 = pt1.x - pt0.x;
    double dy1 = pt1.y - pt0.y;
    double dx2 = pt2.x - pt0.x;
    double dy2 = pt2.y - pt0.y;
    return (dx1*dx2 + dy1*dy2)/sqrt((dx1*dx1 + dy1*dy1)*(dx2*dx2 + dy2*dy2) + 1e-10);
}

/**
 點到點的距離

 @param p1 點1
 @param p2 點2
 @return 距離
 */
double getSpacePointToPoint(cv::Point p1, cv::Point p2)
{
    int a = p1.x-p2.x;
    int b = p1.y-p2.y;
    return sqrt(a * a + b * b);
}

/**
 兩直線的交點

 @param a 線段1
 @param b 線段2
 @return 交點
 */
cv::Point2f computeIntersect(cv::Vec4i a, cv::Vec4i b)  
{  
    int x1 = a[0], y1 = a[1], x2 = a[2], y2 = a[3], x3 = b[0], y3 = b[1], x4 = b[2], y4 = b[3];  

    if (float d = ((float)(x1 - x2) * (y3 - y4)) - ((y1 - y2) * (x3 - x4)))  
    {  
        cv::Point2f pt;  
        pt.x = ((x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4)) / d;  
        pt.y = ((x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4)) / d;  
        return pt;  
    }  
    else  
        return cv::Point2f(-1, -1);  
}  

/**
 對多個點按順時針排序

 @param corners 點的集合
 */
void sortCorners(std::vector<cv::Point2f>& corners)  
{
    if (corners.size() == 0) return;
    //先延 X軸排列
    cv::Point pl = corners[0];
    int index = 0;
    for (int i = 1; i < corners.size(); i++) 
    {
        cv::Point point = corners[i];
        if (pl.x > point.x) 
        {
            pl = point;
            index = i;
        }
    }
    corners[index] = corners[0];
    corners[0] = pl;

    cv::Point lp = corners[0];
    for (int i = 1; i < corners.size(); i++) 
    {
        for (int j = i+1; j<corners.size(); j++) 
        {
            cv::Point point1 = corners[i];
            cv::Point point2 = corners[j];
            if ((point1.y-lp.y*1.0)/(point1.x-lp.x)>(point2.y-lp.y*1.0)/(point2.x-lp.x)) 
            {
                cv::Point temp = point1;
                corners[i] = corners[j];
                corners[j] = temp;
            }
        }
    }
}

根據四邊形的四個頂點,提取目標影象

    //對頂點順時針排序
    sortCorners(_corners);

    //計算目標影象的尺寸
    cv::Point2f p0 = _corners[0];
    cv::Point2f p1 = _corners[1];
    cv::Point2f p2 = _corners[2];
    cv::Point2f p3 = _corners[3];
    float space0 = getSpacePointToPoint(p0, p1);
    float space1 = getSpacePointToPoint(p1, p2);
    float space2 = getSpacePointToPoint(p2, p3);
    float space3 = getSpacePointToPoint(p3, p0);

    float width = space1 > space3 ? space1 : space3;
    float height = space0 > space2 ? space0 : space2;

    cv::Mat quad = cv::Mat::zeros(height * 3, width * 3, CV_8UC3);
    std::vector<cv::Point2f> quad_pts;
    quad_pts.push_back(cv::Point2f(0, quad.rows));
    quad_pts.push_back(cv::Point2f(0, 0));
    quad_pts.push_back(cv::Point2f(quad.cols, 0));
    quad_pts.push_back(cv::Point2f(quad.cols, quad.rows));

    //提取影象
    cv::Mat transmtx = cv::getPerspectiveTransform(_corners , quad_pts);
    cv::warpPerspective(mat, quad, transmtx, quad.size());

最後可以利用 拉普拉斯運算元可以增強區域性的影象對比度,是影象更清晰

    Mat imageMat;
    Mat kernel = (Mat_<float>(3,3) << 0, -1, 0,  -1, 5, -1, 0, -1, 0);
    filter2D(quad, imageMat, quad.depth(), kernel);
    //Mat --> UIImage
    self.imageView.image = MatToUIImage(imageMat);

好了,到這裡就基本實現了對影象中的四邊形檔案或證件的提取。

如有問題,歡迎交流!