无法执行approxPolyDP()

时间:2014-01-15 05:38:41

标签: c++ opencv detection contour

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace cv;
using namespace std;

int main()
{
    Mat image=cvLoadImage("C:/Users/Administrator/Desktop/Desert.jpg",1);
    Mat imagegray, output, imageresult;;

    int thresh=150;

    cvtColor(image, imagegray,CV_BGR2GRAY);

    vector<vector<Point>>contours;
    vector<Vec4i>hierarchy;

    Canny(imagegray, imageresult,thresh, thresh*2);

    findContours(imageresult,contours,hierarchy,CV_RETR_TREE,CV_CHAIN_APPROX_SIMPLE,cvPoint(0,0));

    Mat drawing=Mat::zeros(imagegray.size(),CV_8UC3);
    approxPolyDP(contours,imageresult,100,true);

    namedWindow("Display",1);
    imshow("Display",imageresult);
    waitKey(0);
    return(0);
}

在上面给出的代码中

  

approxPolyDP()

功能不起作用。在使用断点运行它时,程序在此函数后不会执行。这里给出的代码有什么问题?

1 个答案:

答案 0 :(得分:2)

以下代码可以使用

Mat image=cvLoadImage("face1.jpg",1);
Mat imagegray, output, imageresult;;
Mat canny;

cvtColor(image, imagegray,CV_BGR2GRAY);

vector<vector<Point>>contours;
vector<Vec4i>hierarchy;

Canny(imagegray,canny,50,150);

findContours(canny,contours,hierarchy,CV_RETR_TREE,CV_CHAIN_APPROX_SIMPLE,cvPoint(0,0));



   Mat drawing=Mat::zeros(imagegray.size(),CV_8UC3);
 vector<Point>contours_approx;
    approxPolyDP(contours[0],contours_approx,100,true)
//approxPolyDP(contours,imageresult,100,true);

drawContours(image,contours,-1,Scalar(255,0,0));

namedWindow("Display");
imshow("Display",image);
waitKey(0);
return(0);

findcontour()需要一个canny / threshold二进制图像作为输入。

approxPolyDP()用于将曲线近似为另一条曲线(点向量)。