opencv error when trying to submat an image in an android project

asked 2016-04-27 08:55:00 -0500

nadia gravatar image

updated 2016-04-28 11:46:32 -0500

I tried to detect contours in an image ,draw a bounding box for each contours ,put all rect.x rect.y rect.height in an array lists ,sorting them and finally draw a rectangle for each lignes in the image .but i got this error(the error is here : Imgcodecs.imwrite("/storage/emulated/0/DCIM/Lignes/sub"+j+".png",su);

04-27 14:33:43.818: E/cv::error()(25787): OpenCV Error: Assertion failed (0 <= _rowRange.start && _rowRange.start <= _rowRange.end && _rowRange.end <= m.rows) in cv::Mat::Mat(const cv::Mat&, const cv::Range&, const cv::Range&), file /home/maksim/workspace/android-pack/opencv/modules/core/src/matrix.cpp, line 456

    if (approxDistance >1.5){

            Imgproc.rectangle(gray, new Point(rect.x-3,rect.y-3), new Point(rect.x+rect.width+5,rect.y+5+rect.height), new Scalar(0,0,0),2);   



         //Ajouter les colonnes dans un tableau
           valx.add(rect.x);
          for(int p=0; p<valx.size(); p++){

              System.out.println("colx = " + valx.get(p));
               }

          k1++;



          //Ajouter les lignes dans un tableau

          valy.add(rect.y);
        for(int p=0; p<valy.size(); p++){
       System.out.println(" coly = " + valy.get(p));
       }   

        //Ajouter les hauteur des bounding boxes


        hauteur.add(rect.height);
        for(int p=0; p<valy.size(); p++){
       System.out.println(" hauteur = " + hauteur.get(p));
       } 


//Ajouter les largeurs des bounding boxes


        largeur.add(rect.width);
        for(int p=0; p<valx.size(); p++){
       System.out.println(" largeur = " + hauteur.get(p));
       } 
          //trier les colonnes

       if(k1==valx.size()){
           Collections.sort(valx);
           for(int p=0; p<valx.size(); p++){
               System.out.println("colx trié"+ valx.get(p));
               }
       }


       //trier les lignes

         if(k1==valy.size()){
            Collections.sort(valy);


            for(int p=0; p<valy.size(); p++){
            System.out.println("coly trié"+ valy.get(p));   



          }
         }




         //trier les hauteurs
         if(k1==hauteur.size()){
           Collections.sort(hauteur);
             for(int p=0; p<hauteur.size(); p++){
                 System.out.println("hauteur trié"+hauteur.get(p));
                 }
         }

         //trier les largeurs

         if(k1==largeur.size()){
               Collections.sort(largeur);
               for(int p=0; p<largeur.size(); p++){
                   System.out.println("largeur trié"+hauteur.get(p));
                   }
           } 






       }

        count=k1;


       }

      Log.i(TAG,"nombre de lettres"+ count);
      int x=hauteur.get(hauteur.size()-1)-2;
      int y=largeur.get(largeur.size()-1)-2; 

      int j=0;
    for(int k=valy.get(0);k<gray.height();k=k+x)
      {

    Imgproc.rectangle(gray, new Point(valx.get(0),k), new Point(gray.width(),k+x+2), new Scalar(255,0,0),2);
    Mat su = gray.submat(k,k+x+2,valx.get(0),gray.width());

   //  Mat su = gray.submat(k,k+x,valx.get(0),gray.width());
      Log.i(TAG,"erreur1");

      Imgcodecs.imwrite("/storage/emulated/0/DCIM/Lignes/sub"+j+".png",su);
      Log.i(TAG,"erreur2");

         j++;
       }
edit retag flag offensive close merge delete

Comments

try to print out the Rect, before applying submat:

    System.out.println(rect);
    System.out.println(gray);

and check, if it fits inside the gray image, or if it has an area at all.

maybe you need to check the Rect's area, and if it is too small, continue.

berak gravatar imageberak ( 2016-04-28 00:19:33 -0500 )edit

The error is at the end of the code (i delete the first submat at the begining ).it seems like an infinite loop .i found all the "su" mat stored in Lignes directory but the application stopped. My aim here is to sort extracted rect ligne per ligne and for each ligne i should sort the rect in the x axis.

nadia gravatar imagenadia ( 2016-04-28 11:41:43 -0500 )edit