Ask Your Question

OpenCV Error: Assertion failed

asked 2014-11-05 09:03:36 -0500

mp352 gravatar image


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();
    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(;
        RotatedRect mr = Imgproc.minAreaRect(mp2f);
        double area = Math.abs(Imgproc.contourArea(mp2f));
    double bbArea = mr.size.area();            
    double ratio = area / bbArea;            
    if ((ratio &lt; 0.45) || (bbArea &lt; 400)) {                
    } else {                
    } for (Iterator<RotatedRect> it1 = rects.iterator(); it1.hasNext();) { RotatedRect roRe =; 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 ...
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2014-11-05 09:12:37 -0500

unxnut gravatar image

You should print the dimensions of the rectangles prior to calling the OpenCV function and see if they are as expected. I have a feeling that the thresholding and closing operations may give you some different results than what you expect and that may result in an exception.

edit flag offensive delete link more


Thanks for answering. I have checked the koordinates, widths and heights of the detected rects. One of them has a calculated koordinate r.width+r.x which is wider than the orignial. The original has a width of 505px and one of the rects 507px. How could I avoid this exception?


mp352 gravatar imagemp352 ( 2014-11-05 10:04:06 -0500 )edit

You will need to investigate why such a condition arises, especially if it should not arise. The problem may be in the code preceding the exception point. Debugger can help.

unxnut gravatar imageunxnut ( 2014-11-05 13:09:58 -0500 )edit
Login/Signup to Answer

Question Tools


Asked: 2014-11-05 09:03:36 -0500

Seen: 11,527 times

Last updated: Nov 05 '14