How to access points in CvSeq in javacv?

I am developing a project using javacv and I was able to identify the polygon and I store it in CvSeq. I am trying to access points in this structure, but this does not work for me. So please, can someone explain how to access the points in the CvSeq structure in javacv?

For example, I need to access the 8 edge points of the next image. So I could access the lengths of each side of the polygon.

enter image description here

+4
source share
2 answers

You can go through the following class to identify CvSeq points. This method is used to identify points in CvSeq.

for(int i = 0; i < rslt.total(); i++){ CvPoint v=new CvPoint(cvGetSeqElem(rslt, i)); cvDrawCircle(image, v, 5, CvScalar.BLUE, -1, 8, 0); System.out.println(" X value = "+vx()+" ; Y value ="+vy()); } 

In the rslt method above is CvSeq, which contains the Contour details and in the cvDrawCircle () method, it will draw a circle at each point with a blue color. and the next line will print the x, y coordinates of each point.

  import com.googlecode.javacpp.Loader; import com.googlecode.javacv.CanvasFrame; import static com.googlecode.javacpp.Loader.*; import static com.googlecode.javacv.cpp.opencv_core.*; import static com.googlecode.javacv.cpp.opencv_highgui.*; import static com.googlecode.javacv.cpp.opencv_imgproc.*; public class Test { public static void main(String[] args) { CvMemStorage storage=CvMemStorage.create(); CvSeq squares = new CvContour(); squares = cvCreateSeq(0, sizeof(CvContour.class), sizeof(CvSeq.class), storage); String path="C:/Users/Smash/Desktop/A.PNG"; IplImage src = cvLoadImage(path);//hear path is actual path to image IplImage gry=cvCreateImage(cvGetSize(src),IPL_DEPTH_8U,1); cvCvtColor(src, gry, CV_BGR2GRAY); cvFindContours(gry, storage, squares, Loader.sizeof(CvContour.class), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE); CvSeq ss=null; for (int i=0; i<1; i++) { cvDrawContours(gry, squares, CvScalar.WHITE, CV_RGB(248, 18, 18), 1, -1, 8); ss=cvApproxPoly(squares, sizeof(CvContour.class), storage, CV_POLY_APPROX_DP, 8, 0); } if(!ss.isNull()){ drawPoly(src, ss); } } public static void drawPoly( IplImage image, final CvSeq Poly ) { CvSeq rslt=cvApproxPoly(Poly, Loader.sizeof(CvContour.class), cvCreateMemStorage(0), CV_POLY_APPROX_DP, cvContourPerimeter(Poly)*0.02, 0); System.out.println(rslt.total()); for(int i = 0; i < rslt.total(); i++){ CvPoint v=new CvPoint(cvGetSeqElem(rslt, i)); cvDrawCircle(image, v, 5, CvScalar.BLUE, -1, 8, 0); System.out.println(" X value = "+vx()+" ; Y value ="+vy()); } final CanvasFrame canvas = new CanvasFrame("Point Identify"); canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE); canvas.showImage(image); } } 

This is the output image.

enter image description here

These are the associated x, y coordinates

  X value = 72 ; Y value =61 X value = 72 ; Y value =177 X value = 211 ; Y value =178 X value = 211 ; Y value =380 X value = 315 ; Y value =380 X value = 316 ; Y value =177 X value = 465 ; Y value =177 X value = 465 ; Y value =61 
+4
source

To access the CvSeq element, you can use cvGetSeqElem () and CvPoint3D32f.

For example, if the output of cvHoughCircles () is stored in CvSeq circles, you can use the following code to access each circle, its center and radius.

 for(int i = 0; i < circles.total(); i++){ CvPoint3D32f circle = new CvPoint3D32f(cvGetSeqElem(circles, i)); CvPoint center = cvPointFrom32f(new CvPoint2D32f(circle.x(), circle.y())); int radius = Math.round(circle.z()); } 

You can find the full code at http://opencvlover.blogspot.in/2012/07/hough-circle-in-javacv.html

EDIT

You can use the following snippet to access points in cvSeq

 int i = 0; while(cvSeq != null){ i = i+1; for(int j = 0; j < cvSeq.total(); j++){ Pointer line = cvGetSeqElem(cvSeq, j); CvPoint pt = new CvPoint(line).position(0); System.out.println("co-ordinate of point "+ j + " : "+ pt); } cvSeq = cvSeq.h_next(); } System.out.println("Number of contours: " + i); 
+2
source

All Articles