简体   繁体   中英

How to edit this method to draw polygon using javacv?

I went through many turorials and cupple of questions in stackoverflow and I found following method which used to draw rectangles.

public static void drawSquares( IplImage image, final CvSeq squares )
{
    if(!squares.isNull()){
            CvSeq p = new CvSeq(squares.total());
            cvCvtSeqToArray(squares, p, CV_WHOLE_SEQ);
            System.out.println(squares.total());
        for(int i = 0; i < squares.total(); i ++  )
        {
             CvPoint pts = new CvPoint(4);

             cvCvtSeqToArray(p.position(i), pts, CV_WHOLE_SEQ);
//                 //cvBoundingRect(image, i);
            int npt[] = {4, 4};
//                //DrawLine() reference http://opencv.willowgarage.com/documentation/cpp/drawing_functions.html#cv-line
            cvDrawLine(image, new CvPoint(pts.position(0).x(),pts.position(0).y()), new CvPoint(pts.position(1).x(),pts.position(1).y()), CvScalar.GREEN, 3, CV_AA, 0);
            cvDrawLine(image, new CvPoint(pts.position(1).x(),pts.position(1).y()), new CvPoint(pts.position(2).x(),pts.position(2).y()), CvScalar.GREEN, 3, CV_AA, 0);
            cvDrawLine(image, new CvPoint(pts.position(2).x(),pts.position(2).y()), new CvPoint(pts.position(3).x(),pts.position(3).y()), CvScalar.GREEN, 3, CV_AA, 0);
            cvDrawLine(image, new CvPoint(pts.position(3).x(),pts.position(3).y()), new CvPoint(pts.position(0).x(),pts.position(0).y()), CvScalar.GREEN, 3, CV_AA, 0);

        }
    }
    final CanvasFrame canvas = new CanvasFrame(wndname);
    canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
    canvas.showImage(image);
}

This code is exactly doing when it drawing rectangles. But I need to modify this method to draw polygon which having which having 8 sides. I try to change the code as follows but it doesn't give what I expected.

public static void drawPoly( IplImage image, final CvSeq poly )
{
    if(!poly.isNull()){
            CvSeq p = new CvSeq(poly.total());
            cvCvtSeqToArray(poly, p, CV_WHOLE_SEQ);
            System.out.println(poly.total());
        for(int i = 0; i < poly.total(); i ++  )
        {
                System.out.println(i);
             CvPoint pts = new CvPoint(8);

             cvCvtSeqToArray(p.position(i), pts, CV_WHOLE_SEQ);

            cvDrawLine(image, new CvPoint(pts.position(0).x(),pts.position(0).y()), new CvPoint(pts.position(1).x(),pts.position(1).y()), CvScalar.GREEN, 3, CV_AA, 0);
            cvDrawLine(image, new CvPoint(pts.position(1).x(),pts.position(1).y()), new CvPoint(pts.position(2).x(),pts.position(2).y()), CvScalar.GREEN, 3, CV_AA, 0);
            cvDrawLine(image, new CvPoint(pts.position(2).x(),pts.position(2).y()), new CvPoint(pts.position(3).x(),pts.position(3).y()), CvScalar.GREEN, 3, CV_AA, 0);
            cvDrawLine(image, new CvPoint(pts.position(3).x(),pts.position(3).y()), new CvPoint(pts.position(4).x(),pts.position(4).y()), CvScalar.GREEN, 3, CV_AA, 0);
            cvDrawLine(image, new CvPoint(pts.position(4).x(),pts.position(4).y()), new CvPoint(pts.position(5).x(),pts.position(5).y()), CvScalar.GREEN, 3, CV_AA, 0);
            cvDrawLine(image, new CvPoint(pts.position(5).x(),pts.position(5).y()), new CvPoint(pts.position(6).x(),pts.position(6).y()), CvScalar.GREEN, 3, CV_AA, 0);
            cvDrawLine(image, new CvPoint(pts.position(6).x(),pts.position(6).y()), new CvPoint(pts.position(7).x(),pts.position(7).y()), CvScalar.GREEN, 3, CV_AA, 0);
            cvDrawLine(image, new CvPoint(pts.position(7).x(),pts.position(7).y()), new CvPoint(pts.position(0).x(),pts.position(0).y()), CvScalar.GREEN, 3, CV_AA, 0);

        }
    }
    final CanvasFrame canvas = new CanvasFrame("Test inside last");
    canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
    canvas.showImage(image);
} 

This results following error message but I can't understand the reason for than.

0
#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x5c995048, pid=5992, tid=4464
#
# JRE version: 6.0_16-b01
# Java VM: Java HotSpot(TM) Client VM (14.2-b01 mixed mode, sharing windows-x86 )
# Problematic frame:
# C  [opencv_core240.dll+0x55048]
#
# An error report file with more information is saved as:
# C:\Users\Space\Documents\NetBeansProjects\1MyJavacv\hs_err_pid5992.log
#
# If you would like to submit a bug report, please visit:
#   http://java.sun.com/webapps/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#
Java Result: 1

Please can some one explain why it doesn't run and what is the wrong with my methid ?

The first thing you need to do is reduce the problem domain down to a simple repeatable case. Identify which polygon shapes are leading to the crash. You could have a look at the C/C++ source code for opencv to see what checks it does not carry out, for example does it allow off-screen pixels?

You could go further and write a wrapper class that performs a series of checks and logs each function call, allowing you to know exactly when the error takes place, whether the error happens in the same place every time and also the sequence of events that led to the error.

Alternatively you could test the points passed to drawPoly by temporarily having drawPoly call drawSquare , first sending points 0-3, and then points 4-7, just to validate the fact that the points sent are not triggering the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM