【问题标题】:OpenCV MatchTemplate limited to roiOpenCV MatchTemplate 仅限于 roi
【发布时间】:2013-08-08 07:30:58
【问题描述】:

我的问题与在 OpenCV 中使用 matchTemplate 有关。我可以使用该功能在整个图像中查找模板。可以将“搜索区域”限制在图像内的受限区域,即使用 roi?我尝试在调用 matchTemplate 之前设置 roi,但这没有任何效果。

那么,你知道有什么方法可以将模板的搜索限制在图像的一个子区域吗?那是因为我知道我的目标只能在这个有限的区域内找到。

以下是直接取自 OpenCV 示例的一些代码:

void MatchingMethod( int, void* )
{
    // Source image to display    
    Mat img_display;
    img.copyTo( img_display );

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

    // Do the Matching and Normalize
    img.adjustROI(100, 100, 500, 500);
    matchTemplate( img, templ, result, match_method );
    normalize( result, result, 0, 1, NORM_MINMAX, -1, Mat() );

    // Localizing the best match with minMaxLoc
    double minVal; double maxVal; Point minLoc; Point maxLoc;
    Point matchLoc;
    minMaxLoc( result, &minVal, &maxVal, &minLoc, &maxLoc, Mat() );

    // For SQDIFF and SQDIFF_NORMED, the best matches are lower values. For all the other methods, the higher the better
    if( match_method  == CV_TM_SQDIFF || match_method == CV_TM_SQDIFF_NORMED )
    { matchLoc = minLoc; }
    else
    { matchLoc = maxLoc; }

    // Show me what you got
    rectangle( img_display, matchLoc, Point( matchLoc.x + templ.cols , matchLoc.y + templ.rows ), Scalar::all(0), 2, 8, 0 );
    rectangle( result,    matchLoc, Point( matchLoc.x + templ.cols , matchLoc.y + templ.rows ), Scalar::all(0), 2, 8, 0 );

    imshow( image_window, img_display );
    imshow( result_window, result );
}

【问题讨论】:

  • 一段代码可能显示你的尝试?

标签: visual-c++ opencv matchtemplate


【解决方案1】:

确定!

Rect roi( x,y,w,h );
matchTemplate( img( roi ), templ, result, method );

【讨论】:

    猜你喜欢
    • 2016-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-31
    • 2014-02-26
    • 1970-01-01
    • 1970-01-01
    • 2012-10-16
    相关资源
    最近更新 更多