OpenCV中文网站

 找回密码
 立即注册
搜索
热搜: 安装 配置
查看: 5139|回复: 12

chamfer例程中断问题

[复制链接]
发表于 2017-4-7 11:05:57 | 显示全部楼层 |阅读模式
小白求助。
今天尝试了opencv里面自带的chamfermatch函数,一直出现问题。
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/contrib/contrib.hpp"

#include <iostream>

using namespace cv;
using namespace std;

static void help()
{

   cout << "\nThis program demonstrates Chamfer matching -- computing a distance between an \n"
            "edge template and a query edge image.\n"
            "Usage: \n"
            "./chamfer <image edge map> <template edge map>,"
            " By default the inputs are logo_in_clutter.png logo.png\n";
}

const char* keys =
{
    "{1| |logo_in_clutter.png|image edge map    }"
    "{2| |logo.png               |template edge map}"
};

int main( int argc, const char** argv )
{

    help();
    CommandLineParser parser(argc, argv, keys);

    string image = parser.get<string>("1");
    string templ = parser.get<string>("2");
    Mat img = imread(image.c_str(), 0);
    Mat tpl = imread(templ.c_str(), 0);

    if (img.empty() || tpl.empty())
    {
        cout << "Could not read image file " << image << " or " << templ << "." << endl;
        return -1;
    }
    Mat cimg;
    cvtColor(img, cimg, COLOR_GRAY2BGR);

    // if the image and the template are not edge maps but normal grayscale images,
    // you might want to uncomment the lines below to produce the maps. You can also
    // run Sobel instead of Canny.

     Canny(img, img, 5, 50, 3);
    // Canny(tpl, tpl, 5, 50, 3);

    vector<vector<oint> > results;
    vector<float> costs;
    int best = chamerMatching( img, tpl, results, costs );
    if( best < 0 )
    {
        cout << "matching not found" << endl;
        return -1;
    }

    size_t i, n = results[best].size();
    for( i = 0; i < n; i++ )
    {
        Point pt = results[best];
        if( pt.inside(Rect(0, 0, cimg.cols, cimg.rows)) )
           cimg.at<Vec3b>(pt) = Vec3b(0, 255, 0);
    }

    imshow("result", cimg);

    waitKey();

    return 0;
}

F:\QQ截图20170407104928.jpg
回复

使用道具 举报

 楼主| 发表于 2017-4-7 11:07:52 | 显示全部楼层
F:\QQ截图20170407104928.jpg
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-4-7 15:06:22 | 显示全部楼层
回复 支持 反对

使用道具 举报

发表于 2017-4-7 15:19:10 | 显示全部楼层

試試這個範例
  1. #include "opencv2/highgui/highgui.hpp"
  2. #include "opencv2/imgproc/imgproc.hpp"
  3. #include <iostream>
  4. #include <stdio.h>

  5. using namespace std;
  6. using namespace cv;

  7. /// Global Variables
  8. Mat img; Mat templ; Mat result;
  9. char* image_window = "Source Image";
  10. char* result_window = "Result window";

  11. int match_method;
  12. int max_Trackbar = 5;

  13. /// Function Headers
  14. void MatchingMethod( int, void* );

  15. /** @function main */
  16. int main( int argc, char** argv )
  17. {
  18.   /// Load image and template
  19.   img = imread( "1.jpg", 1 );
  20.   templ = imread( "1t.jpg", 1 );

  21.   /// Create windows
  22.   namedWindow( image_window, CV_WINDOW_AUTOSIZE );
  23.   namedWindow( result_window, CV_WINDOW_AUTOSIZE );

  24.   /// Create Trackbar
  25.   char* trackbar_label = "Method: \n 0: SQDIFF \n 1: SQDIFF NORMED \n 2: TM CCORR \n 3: TM CCORR NORMED \n 4: TM COEFF \n 5: TM COEFF NORMED";
  26.   createTrackbar( trackbar_label, image_window, &match_method, max_Trackbar, MatchingMethod );

  27.   MatchingMethod( 0, 0 );

  28.   waitKey(0);
  29.   return 0;
  30. }

  31. /**
  32. * @function MatchingMethod
  33. * @brief Trackbar callback
  34. */
  35. void MatchingMethod( int, void* )
  36. {
  37.   /// Source image to display
  38.   Mat img_display;
  39.   img.copyTo( img_display );

  40.   /// Create the result matrix
  41.   int result_cols =  img.cols - templ.cols + 1;
  42.   int result_rows = img.rows - templ.rows + 1;

  43.   result.create( result_rows, result_cols, CV_32FC1 );

  44.   /// Do the Matching and Normalize
  45.   matchTemplate( img, templ, result, match_method );
  46.   normalize( result, result, 0, 1, NORM_MINMAX, -1, Mat() );

  47.   /// Localizing the best match with minMaxLoc
  48.   double minVal; double maxVal; Point minLoc; Point maxLoc;
  49.   Point matchLoc;

  50.   minMaxLoc( result, &minVal, &maxVal, &minLoc, &maxLoc, Mat() );

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

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

  59.   imshow( image_window, img_display );
  60.   imshow( result_window, result );

  61.   return;
  62. }
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-4-7 15:20:33 | 显示全部楼层

好的,谢谢大神。我试试
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-4-7 15:28:17 | 显示全部楼层

大神,这个例程是模板匹配的。我想知道的是如何用chamfermatch这个函数。用了opencv sample文件里面的例子去尝试,但是历程跑不通。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-4-7 15:28:42 | 显示全部楼层
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-4-7 15:35:05 | 显示全部楼层
h ttps://github.com/zfxxfeng/picture/blob/master/chamfer.zip?raw=true

项目工程文件
回复 支持 反对

使用道具 举报

发表于 2017-4-7 15:52:01 | 显示全部楼层
本帖最后由 smallyouyou 于 2017-4-7 15:56 编辑

http://answers.opencv.org/question/52460/chamfer-matching/
好像有人是說函式本身有bug存在在chamfermatching.cpp中註解掉delete templates;這行看看,

  1. ~Matching()
  2. {
  3.     for (size_t i = 0; i<templates.size(); i++) {
  4.         //delete templates[i];
  5.     }
  6. }
复制代码

並重新編譯,你可以試試

回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-4-7 17:37:36 | 显示全部楼层
smallyouyou 发表于 2017-4-7 15:52
http://answers.opencv.org/question/52460/chamfer-matching/
好像有人是說函式本身有bug存在在chamferma ...

大神,注释了之后依旧是7楼图片那样的中断。而且这个注释掉的语句是match类的析构函数。感觉不是这里的问题。大神方便的话8楼那个网址的工程下载跑一下看看。这个是opencv的例程。不知道会不会因为我自己的操作问题而出错。能不能帮我验证一下。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|OpenCV中文网站

GMT+8, 2024-5-4 00:54 , Processed in 0.009965 second(s), 15 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表