簡體   English   中英

強大的卡檢測/持續更正OpenCV

[英]Robust card detection/persecutive correction OpenCV

我目前有一種方法可以檢測圖像中的卡片,並且大多數情況下,當光線相當一致並且背景非常平靜時,它可以工作。

這是我用來執行此操作的代碼:

    Mat img = inImg.clone();
    outImg = Mat(inImg.size(), CV_8UC1);
    inImg.copyTo(outImg);

    Mat img_fullRes = img.clone();

    pyrDown(img, img);

    Mat imgGray;
    cvtColor(img, imgGray, CV_RGB2GRAY);
    outImg_gray = imgGray.clone();
    // Find Edges //

    Mat detectedEdges = imgGray.clone();

    bilateralFilter(imgGray, detectedEdges, 0, 185, 3, 0);
    Canny( detectedEdges, detectedEdges, 20, 65, 3 );

    dilate(detectedEdges, detectedEdges, Mat::ones(3,3,CV_8UC1));
    Mat cdst = img.clone();

    vector<Vec4i> lines;
    HoughLinesP(detectedEdges, lines, 1, CV_PI/180, 60, 50, 3 );
    for( size_t i = 0; i < lines.size(); i++ )
    {
        Vec4i l = lines[i];
        // For debug
        //line( cdst, cv::Point(l[0], l[1]), cv::Point(l[2], l[3]), Scalar(0,0,255), 1);
    }
    //cdst.copyTo(inImg);

//    // Find points of intersection //

    cv::Rect imgROI;
    int ext = 10;
    imgROI.x = ext;
    imgROI.y = ext;
    imgROI.width = img.size().width - ext;
    imgROI.height = img.size().height - ext;

    int N = lines.size();
    // Creating N amount of points // N == lines.size()
    cv::Point** poi = new cv::Point*[N];
    for( int i = 0; i < N; i++ )
        poi[i] = new cv::Point[N];
    vector<cv::Point> poiList;
    for( int i = 0; i < N; i++ )
    {
        poi[i][i] = cv::Point(-1,-1);
        Vec4i line1 = lines[i];
        for( int j = i + 1; j < N; j++ )
        {
            Vec4i line2 = lines[j];
            cv::Point p = computeIntersect(line1, line2, imgROI);

            if( p.x != -1 )
            {
                //line(cdst, p-cv::Point(2,0), p+cv::Point(2,0), Scalar(0,255,0));
                //line(cdst, p-cv::Point(0,2), p+cv::Point(0,2), Scalar(0,255,0));

                poiList.push_back(p);
            }

            poi[i][j] = p;
            poi[j][i] = p;
        }
    }

    cdst.copyTo(inImg);

    if(poiList.size()==0)
    {
        outImg = inImg.clone();
        //circle(outImg, cv::Point(100,100), 50, Scalar(255,0,0), -1);
        return;
    }

    convexHull(poiList, poiList, false, true);

    for( int i=0; i<poiList.size(); i++ )
    {
        cv::Point p = poiList[i];
        //circle(cdst, p, 3, Scalar(255,0,0), 2);
    }
     //Evaluate all possible quadrilaterals

    cv::Point cardCorners[4];
    float metric_max = 0;
    int Npoi = poiList.size();
    for( int p1=0; p1<Npoi; p1++ )
    {
        cv::Point pts[4];
        pts[0] = poiList[p1];

        for( int p2=p1+1; p2<Npoi; p2++ )
        {
            pts[1] = poiList[p2];
            if( isCloseBy(pts[1],pts[0]) )
                continue;

            for( int p3=p2+1; p3<Npoi; p3++ )
            {
                pts[2] = poiList[p3];
                if( isCloseBy(pts[2],pts[1]) || isCloseBy(pts[2],pts[0]) )
                    continue;


                for( int p4=p3+1; p4<Npoi; p4++ )
                {
                    pts[3] = poiList[p4];
                    if( isCloseBy(pts[3],pts[0]) || isCloseBy(pts[3],pts[1])
                       || isCloseBy(pts[3],pts[2]) )
                        continue;


                    // get the metrics
                    float area = getArea(pts);

                    cv::Point a = pts[0]-pts[1];
                    cv::Point b = pts[1]-pts[2];
                    cv::Point c = pts[2]-pts[3];
                    cv::Point d = pts[3]-pts[0];
                    float oppLenDiff = abs(a.dot(a)-c.dot(c)) + abs(b.dot(b)-d.dot(d));

                    float metric = area - 0.35*oppLenDiff;
                    if( metric > metric_max )
                    {
                        metric_max = metric;
                        cardCorners[0] = pts[0];
                        cardCorners[1] = pts[1];
                        cardCorners[2] = pts[2];
                        cardCorners[3] = pts[3];
                    }

                }
            }
        }
    }

    // find the corners corresponding to the 4 corners of the physical card
    sortPointsClockwise(cardCorners);

    // Calculate Homography //

    vector<Point2f> srcPts(4);
    srcPts[0] = cardCorners[0]*2;
    srcPts[1] = cardCorners[1]*2;
    srcPts[2] = cardCorners[2]*2;
    srcPts[3] = cardCorners[3]*2;


    vector<Point2f> dstPts(4);
    cv::Size outImgSize(1400,800);

    dstPts[0] = Point2f(0,0);
    dstPts[1] = Point2f(outImgSize.width-1,0);
    dstPts[2] = Point2f(outImgSize.width-1,outImgSize.height-1);
    dstPts[3] = Point2f(0,outImgSize.height-1);

    Mat Homography = findHomography(srcPts, dstPts);

    // Apply Homography
    warpPerspective( img_fullRes, outImg, Homography, outImgSize, INTER_CUBIC );
    outImg.copyTo(inImg);

其中computeIntersect定義為:

cv::Point computeIntersect(cv::Vec4i a, cv::Vec4i b, cv::Rect ROI)
{
    int x1 = a[0], y1 = a[1], x2 = a[2], y2 = a[3];
    int x3 = b[0], y3 = b[1], x4 = b[2], y4 = b[3];

    cv::Point  p1 = cv::Point (x1,y1);
    cv::Point  p2 = cv::Point (x2,y2);
    cv::Point  p3 = cv::Point (x3,y3);
    cv::Point  p4 = cv::Point (x4,y4);
    // Check to make sure all points are within the image boundrys, if not reject them.
    if( !ROI.contains(p1) || !ROI.contains(p2)
       || !ROI.contains(p3) || !ROI.contains(p4) )
        return cv::Point (-1,-1);

    cv::Point  vec1 = p1-p2;
    cv::Point  vec2 = p3-p4;

    float vec1_norm2 = vec1.x*vec1.x + vec1.y*vec1.y;
    float vec2_norm2 = vec2.x*vec2.x + vec2.y*vec2.y;
    float cosTheta = (vec1.dot(vec2))/sqrt(vec1_norm2*vec2_norm2);

    float den = ((float)(x1-x2) * (y3-y4)) - ((y1-y2) * (x3-x4));
    if(den != 0)
    {
        cv::Point2f pt;
        pt.x = ((x1*y2 - y1*x2) * (x3-x4) - (x1-x2) * (x3*y4 - y3*x4)) / den;
        pt.y = ((x1*y2 - y1*x2) * (y3-y4) - (y1-y2) * (x3*y4 - y3*x4)) / den;

        if( !ROI.contains(pt) )
            return cv::Point (-1,-1);

        // no-confidence metric
        float d1 = MIN( dist2(p1,pt), dist2(p2,pt) )/vec1_norm2;
        float d2 = MIN( dist2(p3,pt), dist2(p4,pt) )/vec2_norm2;

        float no_confidence_metric = MAX(sqrt(d1),sqrt(d2));

        // If end point ratios are greater than .5 reject
        if( no_confidence_metric < 0.5 && cosTheta < 0.707 )
            return cv::Point (int(pt.x+0.5), int(pt.y+0.5));
    }

    return cv::Point(-1, -1);
}

sortPointsClockWise定義為:

void sortPointsClockwise(cv::Point a[])
{
    cv::Point b[4];

    cv::Point ctr = (a[0]+a[1]+a[2]+a[3]);
    ctr.x /= 4;
    ctr.y /= 4;
    b[0] = a[0]-ctr;
    b[1] = a[1]-ctr;
    b[2] = a[2]-ctr;
    b[3] = a[3]-ctr;

    for( int i=0; i<4; i++ )
    {
        if( b[i].x < 0 )
        {
            if( b[i].y < 0 )
                a[0] = b[i]+ctr;
            else
                a[3] = b[i]+ctr;
        }
        else
        {
            if( b[i].y < 0 )
                a[1] = b[i]+ctr;
            else
                a[2] = b[i]+ctr;
        }
    }

}

getArea定義為:

float getArea(cv::Point arr[])
{
    cv::Point  diag1 = arr[0]-arr[2];
    cv::Point  diag2 = arr[1]-arr[3];

    return 0.5*(diag1.cross(diag2));
}

isCloseBy定義為:

bool isCloseBy( cv::Point p1, cv::Point p2 )
{
    int D = 10;
    // Checking that X values are within 10, same for Y values.
    return ( abs(p1.x-p2.x)<=D && abs(p1.y-p2.y)<=D );
}

最后dist2

float dist2( cv::Point p1, cv::Point p2 )
{
    return float((p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y));
}

以下是幾個測試圖像及其結果:

對於非常冗長的帖子感到抱歉,但我希望有人可以提出一種方法,我可以使我的方法從圖像中提取卡片更加健壯。 可以更好地處理破壞性背景以及不一致的照明。

當卡片放置在具有良好照明的對比背景上時,我的方法幾乎可以在90%的時間內工作。 但顯然我需要一種更強大的方法。

有沒有人有什么建議?

謝謝。

嘗試dhanushka的解決方案

Mat gray, bw;     pyrDown(inImg, inImg);
cvtColor(inImg, gray, CV_RGB2GRAY);
int morph_size = 3;
Mat element = getStructuringElement( MORPH_ELLIPSE, cv::Size( 4*morph_size + 1, 2*morph_size+1 ), cv::Point( morph_size, morph_size ) );

morphologyEx(gray, gray, 2, element);
threshold(gray, bw, 160, 255, CV_THRESH_BINARY);

vector<vector<cv::Point> > contours;
vector<Vec4i> hierarchy;
findContours( bw, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cv::Point(0, 0) );

int largest_area=0;
int largest_contour_index=0;
cv::Rect bounding_rect;

for( int i = 0; i< contours.size(); i++ )
{
    double a=contourArea( contours[i],false);  //  Find the area of contour
    if(a>largest_area){
        largest_area=a;
        largest_contour_index=i;                //Store the index of largest contour
        bounding_rect=boundingRect(contours[i]);
    }
}

//Scalar color( 255,255,255);
rectangle(inImg, bounding_rect,  Scalar(0,255,0),1, 8,0);
Mat biggestRect = inImg(bounding_rect);
Mat card1 = biggestRect.clone();

圖像處理的藝術(在我10多年的經驗中)就是:藝術。 沒有單一的答案,並且總有不止一種方法可以做到這一點。 在某些情況下,肯定會失敗。

根據我在醫學圖像中自動檢測特征的經驗,構建可靠算法需要很長時間,但事后看來,使用相對簡單的算法可以獲得最佳結果。 但是,需要花費大量時間才能使用這個簡單的算法。

要做到這一點,一般方法總是一樣的:

  • 入門是建立一個大型的測試圖像數據庫(至少100個)。 這定義了應該起作用的“正常”圖像。 通過收集圖像,您已經開始考慮問題。
  • 注釋圖像以構建一種“基本事實”。 在這種情況下,“基本事實”應該包含卡片的4個角落,因為這些是有趣的點。
  • 創建一個在這些圖像上運行算法的應用程序,並將結果與​​基本事實進行比較。 在這種情況下,“與地面實況比較”將是找到的4個角點與地面實況角點的平均距離。
  • 輸出以制表符分隔的文件,您調用.xls,因此可以通過雙擊在Excel中打開(在Windows上)。 很高興快速了解案例。 先看看最壞的情況。 然后手動打開這些案例以嘗試理解它們不起作用的原因。
  • 現在您已准備好更改算法。 改變一些東西,並重新運行。 將新Excel表格與舊Excel表格進行比較。 現在你開始意識到你必須做出的權衡。

話雖如此,我認為你需要在調整算法時回答這些問題:

  • 你是否允許折疊卡片? 那么沒有完全直線? 如果是這樣,請更多地集中在角落而不是線條/邊緣。
  • 你是否允許在照明方面逐漸發生差異? 如果是這樣,局部對比拉伸過濾器可能會有所幫助。
  • 您是否允許卡與背景顏色相同? 如果是這樣,你必須專注於卡的內容而不是卡的邊框。
  • 你是否允許使用非完美鏡片? 如果是這樣,到哪個延伸?
  • 你允許旋轉卡嗎? 如果是這樣,到哪個延伸?
  • 背景顏色和/或紋理應該是否均勻?
  • 最小的可檢測卡相對於圖像尺寸應該有多小? 如果您認為應該覆蓋至少80%的寬度或高度,那么您將獲得堅固性。
  • 如果圖像中可以看到多張卡,那么算法是否應該是健壯的並且只選擇一個,或者輸出是否正常?
  • 如果沒有卡片可見,是否應該檢測到這種情況? 建立檢測這種情況將使其更加用戶友好('找不到卡'),但也不那么健壯。

這些將使圖像的要求和假設得以獲得。 您可以依賴的假設非常強大:如果您選擇正確的算法,它們會使算法快速,穩健和簡單。 還要讓這些要求和假設成為測試數據庫的一部分。

那么我會選擇什么? 根據您提供的三張圖片,我將從以下內容開始:

  • 假設卡片將圖像從50%填充到100%。
  • 假設卡片旋轉最多10度左右。
  • 假設角落很清晰可見。
  • 假設卡的縱橫比(高度除以寬度)在1/3和3之間。
  • 假設背景中沒有類似卡片的物體

然后算法看起來像:

  • 使用角過濾器在圖像的每個象限中檢測特定角落。 所以在圖像的左上象限中,卡的左上角。 例如, 請訪問http://www.ee.surrey.ac.uk/CVSSP/demos/corners/results3.html ,或者像cornerHarris一樣使用OpenCV函數。
  • 為了更加健壯,每個象限計算多個角。
  • 嘗試通過組合每個象限的點來構建每個象限一個角的平行四邊形。 創建一個健身功能,可以獲得更高的分數:

    • 內角接近90度
    • 很大
    • 可選地,根據照明或其他特征比較卡的角。

    這種適應度函數可以在以后提供很多調整功能。

  • 返回得分最高的平行四邊形。

那么為什么使用角點檢測而不是霍夫變換來進行線檢測呢? 在我看來,霍夫變換(接下來很慢)對背景中的模式非常敏感(這是你在第一張圖片中看到的 - 它在背景中檢測到比卡更強的線條),它不能除非你使用更大的箱子尺寸會使檢測更加惡化,否則處理好一點曲線。

祝好運!

更一般的方法肯定會像Rutger Nijlunsing在他的回答中所說的那樣。 但是,在您的情況下,至少對於提供的樣本圖像,一個非常簡單的方法,如形態開放,然后是閾值處理,輪廓處理和凸包,將產生您想要的結果。 使用縮小版本的圖像進行處理,這樣您就不必使用大型內核進行形態學操作。 以下是以這種方式處理的圖像。

    pyrDown(large, rgb0);
    pyrDown(rgb0, rgb0);
    pyrDown(rgb0, rgb0);

    Mat small;
    cvtColor(rgb0, small, CV_BGR2GRAY);

    Mat morph;
    Mat kernel = getStructuringElement(MORPH_ELLIPSE, Size(11, 11));
    morphologyEx(small, morph, MORPH_OPEN, kernel);

    Mat bw;
    threshold(morph, bw, 0, 255.0, CV_THRESH_BINARY | CV_THRESH_OTSU);

    Mat bdry;
    kernel = getStructuringElement(MORPH_ELLIPSE, Size(3, 3));
    erode(bw, bdry, kernel);
    subtract(bw, bdry, bdry);

    // do contour processing on bdry

這種方法一般不起作用,所以我強烈建議像Rutger建議的那樣。

在此輸入圖像描述

在此輸入圖像描述

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM