如何使用javacv编辑此方法来绘制多边形?

时间:2012-07-13 05:40:07

标签: java image-processing javacv

我在stackoverflow中经历了许多问题和问题,我找到了以下用于绘制矩形的方法。

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);
}

这段代码在绘制矩形时正是这样做的。但是我需要修改这个方法来绘制具有8个边的多边形。我尝试按如下方式更改代码,但它没有给出我的预期。

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);
} 

这会导致错误信息,但我无法理解原因。

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

请问有人可以解释为什么它没有运行以及我的Methid有什么问题?

1 个答案:

答案 0 :(得分:2)

您需要做的第一件事是将问题域缩减为简单的可重复案例。确定哪些多边形形状导致崩溃。您可以查看opencv的C / C ++源代码,看看它没有执行哪些检查,例如它是否允许屏幕外像素?

你可以进一步编写一个包装类,它执行一系列检查并记录每个函数调用,让你准确知道错误发生的时间,错误是否每次都在同一个地方发生,也是顺序导致错误的事件。

或者,您可以暂时通过drawPoly调用drawPoly来测试传递给drawSquare的点,首先发送点0-3,然后点4-7,以验证事实发送的积分不会触发问题。