簡體   English   中英

openCV中的模板匹配[JAVA]

[英]Template matching in openCV [JAVA]

我正在使用OpenCV(opencv_java248) 我有一個模板圖像。 該模板圖像具有徽標的一些公司。 我想知道此徽標是否包含在其他圖像中。 我在某處遵循了代碼。

public void run(String inFile, String templateFile, String outFile,
        int match_method) {
    System.out.println("Running Template Matching");

    Mat img = Highgui.imread(inFile);
    Mat templ = Highgui.imread(templateFile);

    // / Create the result matrix
    int result_cols = img.cols() - templ.cols() + 1;
    int result_rows = img.rows() - templ.rows() + 1;
    Mat result = new Mat(result_rows, result_cols, CvType.CV_32FC1);

    // / Do the Matching and Normalize
    Imgproc.matchTemplate(img, templ, result, match_method);
    // Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new
    // Mat());
    Imgproc.threshold(result, result, 0.5, 1.0, Imgproc.THRESH_TOZERO);
    // / Localizing the best match with minMaxLoc
    MinMaxLocResult mmr = Core.minMaxLoc(result);

    Point matchLoc;
    if (match_method == Imgproc.TM_SQDIFF
            || match_method == Imgproc.TM_SQDIFF_NORMED) {
        matchLoc = mmr.minLoc;
    } else {
        matchLoc = mmr.maxLoc;
    }
    double threashhold = 0.40;
    if (mmr.maxVal > threashhold) {
        Core.rectangle(img, matchLoc, new Point(matchLoc.x + templ.cols(),
                matchLoc.y + templ.rows()), new Scalar(0, 255, 0));
    }
    // Save the visualized detection.
    Highgui.imwrite(outFile, img);
}

當模板和目標圖像尺寸相同時,效果很好。 我的問題是如何使比例無關緊要? 我的搜尋圖片 目標圖像 和徽標 模板

您想要匹配的圖像或一些代碼將非常有用。

我不知道Java API,但最近確實研究了用C ++編寫的類似內容。 轉換應該相當簡單,因為兩種語言的處理過程都是相同的。

首先,像往常一樣執行模板匹配,讀入圖像並創建墊以保存結果。

cv::Mat drawing = cv::imread(_drawing); //Read drawing
cv::Mat tmp = cv::imread(_tmp);  //Read template
cv::Mat res(drawing.rows - tmp.rows + 1, drawing.cols - tmp.cols + 1, CV_32FC1); //Create result matrix

//Perform template matching, normalise results 0 -> 1
cv::matchTemplate(tmp, drawing, res, CV_TM_CCOEFF_NORMED);
cv::threshold(res, res, 0.8, 1.0, CV_THRESH_TOZERO); //Can thresh to filter results if needed

現在已經填充了結果,創建變量以保存最小/最大分數及其在結果矩陣中的位置。

容差值用於過濾可接受的結果,其中1.0可以看作是100%匹配,而0.25可以看作是25%。

//min/max values and acceptable tolerance
double min, max, tolerance = 0.90;
cv::Point minloc, maxloc; //min/max value locations

現在從結果中提取值,並根據容差檢查最大值,如果在容差范圍之內。

您還可以循環執行此過程,並檢查所有結果以查看圖像是否包含模板的多次出現。

//Loop through all results
while (true){

    //Pull out min/max values from results matrix
    cv::minMaxLoc(res, &min, &max, &minloc, &maxloc);

    //Is max within tolerance
    if (max >= tolerance){

        //Yes - Match found, do stuff //

        //Blank out that result in matrix so next highest can be extracted
        cv::floodFill(res, maxloc, cv::Scalar(0), 0, cv::Scalar(0.1), cv::Scalar(1.0));

    }
    else
        break; //No more results within tolerance, break search
}

您可能需要嘗試公差/圖像質量以及什么才算合格。 但是對於簡單的匹配來說,效果很好。

編輯-模板匹配和比例

標准模板匹配僅由於其工作原理而在縮放方面的表現非常差-搜索窗口只能與提供的模板一樣小,因此要查找任何較小(或較大)的對象將很困難。

精簡模板匹配以實現尺度不變性不是一件容易的事,您可以嘗試的一種簡單方法是創建模板的尺度變化(請看一下OpenCVs圖像金字塔)。

如果感興趣的話,還有很多論文涉及更高級的模板匹配變體(Google搜索將帶來最多的收獲)。

您可能需要研究不變於縮放和旋轉的特征檢測

同樣,如果您可以貼上徽標圖片和搜索圖片來幫助您,則可能有一個簡單的選擇。

暫無
暫無

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

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