Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Convexity Defects in OpenCV 2.4.2

Can anyone tell me if convexityDefects() is supported in OpenCV 2.4.2. The documentation says it does but I'm getting an error when trying to use it and I can't seem to find the problem. Can somebody perhaps identify the problem? Below is my code (The code reads in an image, finds the contours, the convex hull and supposed to find the convexity defects).

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <fstream>
using std::ifstream;
using std::ofstream;
#include <iostream>
using std::cerr;
using std::cout;
using std::endl;
#include <dirent.h>
#include <cstdlib> 
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string>
using namespace std;
using namespace cv;

Mat contour(Mat image);

int main( int argc, const char** argv )
{
    Mat image = imread( argv[1], 1 );       
    Mat new_image = contour(image);
    imshow("Output", new_image);
    waitKey();      
    return 0;
}

Mat contour(Mat image)
{
    cvtColor(image,image,CV_BGR2GRAY);

    int threshval = 40;
    int max_thresh = 255;
    RNG rng(12345);

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

    threshold( image, threshold_output, threshval, 255, THRESH_BINARY );

    findContours(threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );

    Mat cimage = Mat::zeros(image.size(), CV_8UC3);

    for(size_t i = 0; i < contours.size(); i++)
    {
        size_t count = contours[i].size();
        if( count < 6 )
            continue;

        Mat pointsf;
        Mat(contours[i]).convertTo(pointsf, CV_32F);
        RotatedRect box = fitEllipse(pointsf);
        cout << "width: " << box.size.width << " height: " <<  box.size.height << endl;
        if( MAX(box.size.width, box.size.height) > MIN(box.size.width, box.size.height)*30 )
            continue;
        drawContours(cimage, contours, (int)i, Scalar::all(255), 1, 8, vector<Vec4i>(), 0, Point());

        //Convex Hull -------------
        vector<Point> hullPoints;   
        vector<Point> convexityDefects;

        convexHull(contours[i], hullPoints, true, true);
        convexityDefects(contours[i], hullPoints, convexityDefects);

        int hullcount = (int)hullPoints.size();
        for( int j = 1; j < hullcount; j++ )
        {
            line(cimage, hullPoints[j-1], hullPoints[j], Scalar(0, 255, 0), 1, CV_AA);
            circle(cimage,  hullPoints[j], 1, Scalar(0, 0, 255), CV_FILLED, CV_AA);
        }
    }
    return cimage;
}

Convexity Defects in OpenCV 2.4.2

Can anyone tell me if convexityDefects() is supported in OpenCV 2.4.2. The documentation says it does but I'm getting an error when trying to use it and I can't seem to find the problem. The error I get is,

convexDefect.cpp: In function ‘cv::Mat contour(cv::Mat)’:
convexDefect.cpp:68:66: error: no match for call to ‘(std::vector<cv::Point_<int> >)
(std::vector<cv::Point_<int> >&, std::vector<cv::Point_<int> >&, 
std::vector<cv::Point_<int> >&)

Can somebody perhaps identify the problem? Below is my code (The code reads in an image, finds the contours, the convex hull and supposed to find the convexity defects).defects).

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <fstream>
using std::ifstream;
using std::ofstream;
#include <iostream>
using std::cerr;
using std::cout;
using std::endl;
#include <dirent.h>
#include <cstdlib> 
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string>
using namespace std;
using namespace cv;

Mat contour(Mat image);

int main( int argc, const char** argv )
{
    Mat image = imread( argv[1], 1 );       
    Mat new_image = contour(image);
    imshow("Output", new_image);
    waitKey();      
    return 0;
}

Mat contour(Mat image)
{
    cvtColor(image,image,CV_BGR2GRAY);

    int threshval = 40;
    int max_thresh = 255;
    RNG rng(12345);

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

    threshold( image, threshold_output, threshval, 255, THRESH_BINARY );

    findContours(threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );

    Mat cimage = Mat::zeros(image.size(), CV_8UC3);

    for(size_t i = 0; i < contours.size(); i++)
    {
        size_t count = contours[i].size();
        if( count < 6 )
            continue;

        Mat pointsf;
        Mat(contours[i]).convertTo(pointsf, CV_32F);
        RotatedRect box = fitEllipse(pointsf);
        cout << "width: " << box.size.width << " height: " <<  box.size.height << endl;
        if( MAX(box.size.width, box.size.height) > MIN(box.size.width, box.size.height)*30 )
            continue;
        drawContours(cimage, contours, (int)i, Scalar::all(255), 1, 8, vector<Vec4i>(), 0, Point());

        //Convex Hull -------------
        vector<Point> hullPoints;   
        vector<Point> convexityDefects;

        convexHull(contours[i], hullPoints, true, true);
        convexityDefects(contours[i], hullPoints, convexityDefects);

        int hullcount = (int)hullPoints.size();
        for( int j = 1; j < hullcount; j++ )
        {
            line(cimage, hullPoints[j-1], hullPoints[j], Scalar(0, 255, 0), 1, CV_AA);
            circle(cimage,  hullPoints[j], 1, Scalar(0, 0, 255), CV_FILLED, CV_AA);
        }
    }
    return cimage;
}

Convexity Defects in OpenCV 2.4.2

Can anyone tell me if convexityDefects() is supported in OpenCV 2.4.2. The documentation says it does but I'm getting an error when trying to use it and I can't seem to find the problem. The error I get is,

convexDefect.cpp: In function ‘cv::Mat contour(cv::Mat)’:
convexDefect.cpp:68:66: error: no match for call to ‘(std::vector<cv::Point_<int> >)
(std::vector<cv::Point_<int> >&, std::vector<cv::Point_<int> >&, 
std::vector<cv::Point_<int> >&)

Can somebody perhaps identify the problem? Below is my code (The code reads in an image, finds the contours, the convex hull and supposed to find the convexity defects).

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <fstream>
using std::ifstream;
using std::ofstream;
#include <iostream>
using std::cerr;
using std::cout;
using std::endl;
#include <dirent.h>
#include <cstdlib> 
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string>
using namespace std;
using namespace cv;

Mat contour(Mat image);

int main( int argc, const char** argv )
{
    Mat image = imread( argv[1], 1 );       
    Mat new_image = contour(image);
    imshow("Output", new_image);
    waitKey();      
    return 0;
}

Mat contour(Mat image)
{
    cvtColor(image,image,CV_BGR2GRAY);

    int threshval = 40;
    int max_thresh = 255;
    RNG rng(12345);

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

    threshold( image, threshold_output, threshval, 255, THRESH_BINARY );

    findContours(threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );

    Mat cimage = Mat::zeros(image.size(), CV_8UC3);

    for(size_t i = 0; i < contours.size(); i++)
    {
        size_t count = contours[i].size();
        if( count < 6 )
            continue;

        Mat pointsf;
        Mat(contours[i]).convertTo(pointsf, CV_32F);
        RotatedRect box = fitEllipse(pointsf);
        cout << "width: " << box.size.width << " height: " <<  box.size.height << endl;
        if( MAX(box.size.width, box.size.height) > MIN(box.size.width, box.size.height)*30 )
            continue;
        drawContours(cimage, contours, (int)i, Scalar::all(255), 1, 8, vector<Vec4i>(), 0, Point());

        //Convex Hull -------------
        vector<Point> hullPoints;   
        vector<Point> convexityDefects;

        convexHull(contours[i], hullPoints, true, true);
        convexityDefects(contours[i], hullPoints, convexityDefects);

        int hullcount = (int)hullPoints.size();
        for( int j = 1; j < hullcount; j++ )
        {
            line(cimage, hullPoints[j-1], hullPoints[j], Scalar(0, 255, 0), 1, CV_AA);
            circle(cimage,  hullPoints[j], 1, Scalar(0, 0, 255), CV_FILLED, CV_AA);
        }
    }
    return cimage;
}

Convexity Defects in OpenCV 2.4.2

Can anyone tell me if convexityDefects() is supported in OpenCV 2.4.2. The documentation says it does but I'm getting an error when trying to use it and I can't seem to find the problem. The error I get is,

convexDefect.cpp: In function ‘cv::Mat contour(cv::Mat)’:
convexDefect.cpp:68:66: error: no match for call to ‘(std::vector<cv::Point_<int> >)
(std::vector<cv::Point_<int> >&, std::vector<cv::Point_<int> >&, 
std::vector<cv::Point_<int> >&)

Can somebody perhaps identify the problem? Below is my code (The code reads in an image, finds the contours, the convex hull and supposed to find the convexity defects).

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <fstream>
using std::ifstream;
using std::ofstream;
#include <iostream>
using std::cerr;
using std::cout;
using std::endl;
#include <dirent.h>
#include <cstdlib> 
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string>
using namespace std;
using namespace cv;

Mat contour(Mat image);

int main( int argc, const char** argv )
{
    Mat image = imread( argv[1], 1 );       
    Mat new_image = contour(image);
    imshow("Output", new_image);
    waitKey();      
    return 0;
}

Mat contour(Mat image)
{
    cvtColor(image,image,CV_BGR2GRAY);

    int threshval = 40;
    int max_thresh = 255;
    RNG rng(12345);

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

    threshold( image, threshold_output, threshval, 255, THRESH_BINARY );

    findContours(threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );

    Mat cimage = Mat::zeros(image.size(), CV_8UC3);

    for(size_t i = 0; i < contours.size(); i++)
    {
        size_t count = contours[i].size();
        if( count < 6 )
            continue;

        Mat pointsf;
        Mat(contours[i]).convertTo(pointsf, CV_32F);
        RotatedRect box = fitEllipse(pointsf);
        cout << "width: " << box.size.width << " height: " <<  box.size.height << endl;
        if( MAX(box.size.width, box.size.height) > MIN(box.size.width, box.size.height)*30 )
            continue;
        drawContours(cimage, contours, (int)i, Scalar::all(255), 1, 8, vector<Vec4i>(), 0, Point());

         //Convex Hull -------------
         // vector<Point> hullPoints;   
        vector<Point>  vector<int> hullIndices; 
         vector<Vec4i> convexityDefects;

        convexHull(contours[i], //convexHull(contours[i], hullPoints, true, true);
        convexHull(contours[i], hullIndices, true);
        convexityDefects(contours[i], hullPoints, hullIndices, convexityDefects);

        int //int hullcount = (int)hullPoints.size();
        for( /*for( int j = 1; j < hullcount; j++ )
        {
            line(cimage, hullPoints[j-1], hullPoints[j], Scalar(0, 255, 0), 1, CV_AA);
            circle(cimage,  hullPoints[j], 1, Scalar(0, 0, 255), CV_FILLED, CV_AA);
        }
}*/
    }
    return cimage;
}

Convexity Defects in OpenCV 2.4.2

Can anyone tell me if convexityDefects() is supported in OpenCV 2.4.2. The documentation says it does but I'm getting an error when trying to use it and I can't seem to find the problem. The error I get is,

convexDefect.cpp: In function ‘cv::Mat contour(cv::Mat)’:
convexDefect.cpp:68:66: error: no match for call to ‘(std::vector<cv::Point_<int> >)
(std::vector<cv::Point_<int> >&, std::vector<cv::Point_<int> >&, 
std::vector<cv::Point_<int> >&)

Can somebody perhaps identify the problem? Below is my code (The code reads in an image, finds the contours, the convex hull and supposed to find the convexity defects).

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <fstream>
using std::ifstream;
using std::ofstream;
#include <iostream>
using std::cerr;
using std::cout;
using std::endl;
#include <dirent.h>
#include <cstdlib> 
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string>
using namespace std;
using namespace cv;

Mat contour(Mat image);

int main( int argc, const char** argv )
{
    Mat image = imread( argv[1], 1 );       
    Mat new_image = contour(image);
    imshow("Output", new_image);
    waitKey();      
    return 0;
}

Mat contour(Mat image)
{
    cvtColor(image,image,CV_BGR2GRAY);

    int threshval = 40;
    int max_thresh = 255;
    RNG rng(12345);

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

    threshold( image, threshold_output, threshval, 255, THRESH_BINARY );

    findContours(threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );

    Mat cimage = Mat::zeros(image.size(), CV_8UC3);

    for(size_t i = 0; i < contours.size(); i++)
    {
        size_t count = contours[i].size();
        if( count < 6 )
            continue;

        Mat pointsf;
        Mat(contours[i]).convertTo(pointsf, CV_32F);
        RotatedRect box = fitEllipse(pointsf);
        if( MAX(box.size.width, box.size.height) > MIN(box.size.width, box.size.height)*30 )
            continue;
        drawContours(cimage, contours, (int)i, Scalar::all(255), 1, 8, vector<Vec4i>(), 0, Point());

         //Convex Hull -------------
         // vector<Point> hullPoints;   
         vector<int> hullIndices; 
         vector<Vec4i> convexityDefects;

        //convexHull(contours[i], hullPoints, true, true);
        convexHull(contours[i], hullIndices, true);
        convexityDefects(contours[i], hullIndices, convexityDefects);

    //int hullcount = (int)hullPoints.size();
        /*for( int j = 1; j < hullcount; j++ )
        {
            line(cimage, hullPoints[j-1], hullPoints[j], Scalar(0, 255, 0), 1, CV_AA);
            circle(cimage,  hullPoints[j], 1, Scalar(0, 0, 255), CV_FILLED, CV_AA);
        }*/
    }
    return cimage;
}