2017-01-05 7 views
0

openCV javaでテンプレートマッチングを使用して、サブイメージがより大きなイメージに存在するかどうかを確認しています。正確な部分画像が大きな画像で利用できる場合にのみ、一致の座標を取得したい。私はこのコードを使用していますが、多くの偽陽性が出ています。サブ画像と大きな画像が添付されています。サブイメージは、より大きな画像に存在していないが、私はここ(873715)larger imagesubimageテンプレート一致false positive

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

    Mat img = Imgcodecs.imread(inFile); 
    Mat templ = Imgcodecs.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.1, 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 = 1.0; 
    if (mmr.maxVal > threashhold) { 
     System.out.println(matchLoc.x+" "+matchLoc.y); 
     Imgproc.rectangle(img, matchLoc, new Point(matchLoc.x + templ.cols(), 
       matchLoc.y + templ.rows()), new Scalar(0, 255, 0)); 
    } 
    // Save the visualized detection. 
    Imgcodecs.imwrite(outFile, img); 
} 

答えて

0

私はJavaのOpenCVではなくOpenCV C++をよく知っています。

次のコードは必要ないと思います。

Imgproc.threshold(result, result, 0.1, 1.0, Imgproc.THRESH_TOZERO); 

正規化オプションを使用すると、「マット結果」の最小値と最大値は-1と1の間になります。したがって、正規化オプションを使用すると、しきい値が1.0になるため、次のコードは機能しません。上記のコード

また
if (mmr.maxVal > threshold) 

、あなたがCV_TM_SQDIFFを使用している場合は、適切な閾値と

if (mmr.minVal < threshold) 

でなければなりません。

minVal/maxValとthresholdを比較する前に、minMaxLocを描画するのはどうですか?それが正しい結果を与えることを確認するには? (873,715)での試合はばかげているからです。

関連する問題