Jinu Joseph Daniel
Jinu Joseph Daniel

Reputation: 6291

Problems in finding contours and convex hull in openCV

I have written the following code

#include"opencv2/opencv.hpp"

 #include<iostream>
 #include<math.h>
 using namespace std;
 using namespace cv;

main()
{
Mat img1,img2,sub,gray1,gray2,lab,ycbcr;
int v[3];

int row,col,i,j,t;
VideoCapture cap(0);

namedWindow("current");

cap>>img1;
sub=img1;

row=img1.rows;
col=img1.cols;

cvtColor(img1,gray1,CV_BGR2GRAY);


vector<vector<Point> > cont;

vector<Vec4i> hierarchy;

while (1) {

    cap>>img2;

    cvtColor(img2,gray2,CV_BGR2GRAY);


    for(i=0;i<row;++i)
    {
        for (j=0; j<col; ++j)
        {


            if(abs(gray1.at<uchar>(i,j) - gray2.at<uchar>(i,j))>10)
            {
                sub.at<Vec3b>(i,j)[0] = img2.at<Vec3b>(i,j)[0];
                sub.at<Vec3b>(i,j)[1] = img2.at<Vec3b>(i,j)[1];
                sub.at<Vec3b>(i,j)[2] = img2.at<Vec3b>(i,j)[2];

            }
            else
            {

                sub.at<Vec3b>(i,j)[0]=0;
                sub.at<Vec3b>(i,j)[1]=0;
                sub.at<Vec3b>(i,j)[2]=0;
            }

        }


    }


    cvtColor(sub,ycbcr,CV_BGR2YCrCb);

    inRange(ycbcr,Scalar(7,133,106),Scalar(255,178,129),ycbcr);
    findContours(ycbcr,cont,hierarchy,CV_RETR_LIST,CV_CHAIN_APPROX_SIMPLE);
    Scalar color = CV_RGB(255,0,0);
    for(int i1 = 0 ;i1 >= 0; i1 = hierarchy[i1][0] )
    drawContours( ycbcr, cont, i1, color,2, CV_AA, hierarchy );



    vector<Point2f > hullPoints;
  // convexHull(Mat(cont),hullPoints,false);
    imshow("current",ycbcr);

a if(waitKey(33)=='q') break; img1=img2.clone(); }

}

1.)Why the contours are not displaying in red color although i specified it through CV_RGB(255,0,0). 2.)When i uncomment the line convexHull(Mat(cont),hullPoints,false); ,the program shows runtime error.Why is it happening.Can anybody tell me the exact format of convexHull() and the meaning of its arguments

Upvotes: 0

Views: 2875

Answers (2)

user2362956
user2362956

Reputation:

Your hullPoints vector is empty. If you check the size of vector,you don't get an error.

if(hullPoints.size() > 2) {
  convexHull(....);
 }

Upvotes: 1

Abid Rahman K
Abid Rahman K

Reputation: 52646

1) Try (0,0,255) for red color. OpenCV uses BGR format.

2) For finding convex hull, try the code in OpenCV tutorial.

Also try similar question as yours on Convexhull. How to calculate convex hull area using openCV functions?

Upvotes: 2

Related Questions