c++ - OpenCV convexity defects drawing -


i have stored defects using convexity defects in 4 element vector integer array using vec4i.

my convex hull array in hull element , contours in contours; want draw line start point of convexity defect end point of one. need access element start index present in vec4i of defects vector!

how do this??

   #include <opencv\cv.h> #include <opencv2\highgui\highgui.hpp> #include<opencv\cvaux.h> #include<opencv\cxcore.h> #include <opencv2\imgproc\imgproc.hpp> #include <iostream> #include<conio.h> #include <stdlib.h>    using namespace cv; using namespace std;  int main(){       mat img, frame, img2, img3;       videocapture cam(0);     while (true){         cam.read(frame);         cvtcolor(frame, img, cv_bgr2hsv);         //thresholding          inrange(img, scalar(0, 143, 86), scalar(39, 255, 241), img2);          imshow("hi", img2);          //finding contours         vector<vector<point>> contours;         vector<vec4i> hier;         //morphological transformations         erode(img2, img2, getstructuringelement(morph_rect, size(3, 3)));         erode(img2, img2, getstructuringelement(morph_rect, size(3, 3)));          dilate(img2, img2, getstructuringelement(morph_rect, size(8, 8)));         dilate(img2, img2, getstructuringelement(morph_rect, size(8, 8)));           //finding contours required         findcontours(img2, contours, hier, cv_retr_ccomp, cv_chain_approx_none, point(0, 0));            //finding contour of largest area , storing index         int lrgctridx = 0;         int maxarea = 0;         (int = 0; < contours.size(); i++)         {             double = contourarea(contours[i]);             if (a> maxarea)             {                 maxarea = a;                 lrgctridx = i;             }          }         //convex hulls         vector<vector<point> >hull(contours.size());         vector<vector<vec4i>> defects(contours.size());         (int = 0; < contours.size(); i++)         {             convexhull(contours[i], hull[i], false);             convexitydefects(contours[i], hull[i], defects[i]);         }         //required contour detected,then convex hell found , convexity defects found , stored in defects             if (maxarea>100){             drawcontours(frame, hull, lrgctridx, scalar(255, 255, 255), 1, 8, vector<vec4i>(), 0, point());         \\ drawing required lines joining defects!im facing problem on how acheive since dont know how access elements stored in defects            line(frame, \\startindex, \\endindex, \\color, 1);          }           imshow("output", frame);         char key = waitkey(33);         if (key == 27) break;        } 

}

also output window shows error when add convexitydefects(..) line think in wrong format! in advance.

convexitydefects needs

convex hull obtained using convexhull() should contain indices of contour points make hull.

that contains more 3 indices. need this:

vector<vector<point> >hull(contours.size()); vector<vector<int> > hullsi(contours.size()); // indices contour points vector<vector<vec4i>> defects(contours.size()); (int = 0; < contours.size(); i++) {     convexhull(contours[i], hull[i], false);     convexhull(contours[i], hullsi[i], false);      if(hullsi[i].size() > 3 ) // need more 3 indices               {         convexitydefects(contours[i], hullsi[i], defects[i]);     } } 

then drawing part (adapted here):

/// draw convexitydefects (int = 0; < contours.size(); ++i) {     for(const vec4i& v : defects[i])     {         float depth = v[3] / 256;         if (depth > 10) //  filter defects depth, e.g more 10         {             int startidx = v[0]; point ptstart(contours[i][startidx]);             int endidx = v[1]; point ptend(contours[i][endidx]);             int faridx = v[2]; point ptfar(contours[i][faridx]);              line(frame, ptstart, ptend, scalar(0, 255, 0), 1);             line(frame, ptstart, ptfar, scalar(0, 255, 0), 1);             line(frame, ptend, ptfar, scalar(0, 255, 0), 1);             circle(frame, ptfar, 4, scalar(0, 255, 0), 2);         }     } }    

complete code

#include <opencv2\opencv.hpp> #include <iostream>  using namespace cv; using namespace std;  int main() {     mat img, frame, img2, img3;     videocapture cam(0);     while (true){         cam.read(frame);          cvtcolor(frame, img, cv_bgr2hsv);          //thresholding          inrange(img, scalar(0, 143, 86), scalar(39, 255, 241), img2);          imshow("hi", img2);          //finding contours         vector<vector<point>> contours;         vector<vec4i> hier;         //morphological transformations         erode(img2, img2, getstructuringelement(morph_rect, size(3, 3)));         erode(img2, img2, getstructuringelement(morph_rect, size(3, 3)));          dilate(img2, img2, getstructuringelement(morph_rect, size(8, 8)));         dilate(img2, img2, getstructuringelement(morph_rect, size(8, 8)));          //finding contours required         findcontours(img2, contours, hier, cv_retr_ccomp, cv_chain_approx_none, point(0, 0));          //finding contour of largest area , storing index         int lrgctridx = 0;         int maxarea = 0;         (int = 0; < contours.size(); i++)         {             double = contourarea(contours[i]);             if (a> maxarea)             {                 maxarea = a;                 lrgctridx = i;             }         }         //convex hulls         vector<vector<point> >hull(contours.size());         vector<vector<int> > hullsi(contours.size());          vector<vector<vec4i>> defects(contours.size());         (int = 0; < contours.size(); i++)         {             convexhull(contours[i], hull[i], false);             convexhull(contours[i], hullsi[i], false);              if(hullsi[i].size() > 3 )                         {                 convexitydefects(contours[i], hullsi[i], defects[i]);             }         }         //required contour detected,then convex hell found , convexity defects found , stored in defects          if (maxarea>100){             drawcontours(frame, hull, lrgctridx, scalar(2555, 0, 255), 3, 8, vector<vec4i>(), 0, point());              /// draw convexitydefects             for(int j=0; j<defects[lrgctridx].size(); ++j)             {                 const vec4i& v = defects[lrgctridx][j];                 float depth = v[3] / 256;                 if (depth > 10) //  filter defects depth                 {                     int startidx = v[0]; point ptstart(contours[lrgctridx][startidx]);                     int endidx = v[1]; point ptend(contours[lrgctridx][endidx]);                     int faridx = v[2]; point ptfar(contours[lrgctridx][faridx]);                      line(frame, ptstart, ptend, scalar(0, 255, 0), 1);                     line(frame, ptstart, ptfar, scalar(0, 255, 0), 1);                     line(frame, ptend, ptfar, scalar(0, 255, 0), 1);                     circle(frame, ptfar, 4, scalar(0, 255, 0), 2);                 }             }         }          imshow("output", frame);         char key = waitkey(33);         if (key == 27) break;      } } 

Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -