Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

OpenCV Error: Assertion failed

Hi,

hopefully my english is good enough for describing my problem. I am working only a short time on opencv. In a little project I tried to detect the number plates from cars. The detection works very well. Only the last function in my script throws an exception.

I tried to create a new mat from the picture and the rectangle but that throws the following exception: OpenCV Error: Assertion failed (0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows) in cv::Mat::Mat, file ........\opencv\modules\core\src\matrix.cpp, line 323 Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: ........\opencv\modules\core\src\matrix.cpp:323: error: (-215) 0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows in function cv::Mat::Mat

But the detection works fine. the picture shows that the script creates rectangles around them. image description

Could somebody help me?

/*

* To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package demoOpenCV;

import java.util.ArrayList; import java.util.Iterator; import java.util.ListIterator; import org.opencv.core.Core; import org.opencv.core.CvType; import org.opencv.core.Mat; import org.opencv.core.MatOfPoint; import org.opencv.core.MatOfPoint2f; import org.opencv.core.Point; import org.opencv.core.Rect; import org.opencv.core.MatOfRect; import org.opencv.core.RotatedRect; import org.opencv.core.Scalar; import org.opencv.core.Size; import org.opencv.highgui.Highgui; import org.opencv.imgproc.Imgproc; import static org.opencv.imgproc.Imgproc.MORPH_RECT;

/** * * @author Manuel */ public class NumberPlate {

public void run() {
    System.out.println("Kennzeichenerkennung is running...");

    Mat img = Highgui.imread("c:/tmp/plate2.jpg");
    Mat kopie = new Mat();
    img.copyTo(kopie);

    Mat img_grey = new Mat();

    Mat img_sobel = new Mat();

    Mat img_threshold = new Mat();

    Mat element = Imgproc.getStructuringElement(MORPH_RECT, new Size(17, 3));

    ArrayList<RotatedRect> rects = new ArrayList<RotatedRect>();
    ArrayList<MatOfPoint> contors = new ArrayList<MatOfPoint>();

    Mat schild;

    Imgproc.cvtColor(img, img_grey, Imgproc.COLOR_BGR2GRAY);

    Imgproc.blur(img_grey, img_grey, new Size(5, 5));

    Imgproc.Sobel(img_grey, img_sobel, CvType.CV_8U, 1, 0, 3, 1, 0);

    Imgproc.threshold(img_sobel, img_threshold, 0, 255,
            ((Imgproc.THRESH_OTSU) + (Imgproc.THRESH_BINARY)));

    Imgproc.morphologyEx(img_threshold, img_threshold, Imgproc.MORPH_CLOSE, element);

    Imgproc.findContours(img_threshold, contors, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_NONE);

    ListIterator<MatOfPoint> it = contors.listIterator();
    while (it.hasNext()) {
        MatOfPoint2f mp2f = new MatOfPoint2f(it.next().toArray());
        RotatedRect mr = Imgproc.minAreaRect(mp2f);
        double area = Math.abs(Imgproc.contourArea(mp2f));

        double bbArea = mr.size.area();            
        double ratio = area / bbArea;            
        if ((ratio < 0.45) || (bbArea < 400)) {                
            it.remove();                
        } else {                
            rects.add(mr);                
        }            
    }

    for (Iterator<RotatedRect> it1 = rects.iterator(); it1.hasNext();) {
        RotatedRect roRe = it1.next();
        Rect r = roRe.boundingRect();

        Core.rectangle(kopie, new Point((r.x), (r.y)),
                new Point((r.x + r.width), (r.y + r.height)),
                new Scalar(0, 0, 255));

        ***schild = kopie.submat(r);***
        Imgproc.blur(schild, schild, new Size(3, 3), new Point(4, 4));
    }

    String datei = "c:/tmp/plate2_neu.png";
    boolean imwrite = Highgui.imwrite(datei, img);
}

}