Question

#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);
}

In the code given above the

approxPolyDP()

function is not working. On running it with breakpoints the program does not execute after this function. What is wrong with the code given here?

Was it helpful?

Solution

following codes works

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() needs a canny/threshold binary image for the input.

approxPolyDP() is to approximates a curve to another curve(vector of points).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top