Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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

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

 for (int i=0; i<contours.size(); i++)
   { 

    //  if(Imgproc.contourArea(contours.get(i)) >threshold){

       //Convert contours(i) from MatOfPoint to MatOfPoint2f
       MatOfPoint2f contour2f = new MatOfPoint2f( contours.get(i).toArray() );

       //Processing on mMOP2f1 which is in type MatOfPoint2f
       double approxDistance = Imgproc.arcLength(contour2f, true)*0.02;

       Imgproc.approxPolyDP(contour2f, approxCurve, approxDistance, true);
       //Convert back to MatOfPoint
       MatOfPoint points = new MatOfPoint( approxCurve.toArray() );

       // Get bounding rect of contour
       Rect rect = Imgproc.boundingRect(points);



       // draw enclosing rectangle (all same color, but you could use variable i to make them unique)

   if (approxDistance >1.5){


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



         Mat ROI = gray.submat(rect.y, rect.y + rect.height, rect.x, rect.x+ rect.width); 
         Imgproc.resize(ROI,ROI, new Size(25,25) );
         Imgcodecs.imwrite("/storage/emulated/0/DCIM/OurText/IMG"+k3+".png",ROI);
         k3++;






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



      //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 hauteur
     if(k1==hauteur.size()){
       Collections.sort(hauteur);
         for(int p=0; p<hauteur.size(); p++){
             System.out.println("hauteur trié"+hauteur.get(p));
             }
     }



   }

    count=k1;


   }

  Log.i(TAG,"nombre de lettres"+ count);
  int x=hauteur.get(hauteur.size()-1)+8; 
    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), new Scalar(255,0,0),2);
  Mat su = gray.submat(k,k+x,valx.get(0),gray.width());
  Imgcodecs.imwrite("/storage/emulated/0/DCIM/Lignes/sub"+j+".png",su);
  j++;

 }

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

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

 for (int i=0; i<contours.size(); i++)
   { 

    //  if(Imgproc.contourArea(contours.get(i)) >threshold){

       //Convert contours(i) from MatOfPoint to MatOfPoint2f
       MatOfPoint2f contour2f = new MatOfPoint2f( contours.get(i).toArray() );

       //Processing on mMOP2f1 which is in type MatOfPoint2f
       double approxDistance = Imgproc.arcLength(contour2f, true)*0.02;

       Imgproc.approxPolyDP(contour2f, approxCurve, approxDistance, true);
       //Convert back to MatOfPoint
       MatOfPoint points = new MatOfPoint( approxCurve.toArray() );

       // Get bounding rect of contour
       Rect rect = Imgproc.boundingRect(points);



       // draw enclosing rectangle (all same color, but you could use variable i to make them unique)

   if (approxDistance >1.5){


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



         Mat ROI = gray.submat(rect.y, rect.y + rect.height, rect.x, rect.x+ rect.width); 
         Imgproc.resize(ROI,ROI, new Size(25,25) );
         Imgcodecs.imwrite("/storage/emulated/0/DCIM/OurText/IMG"+k3+".png",ROI);
         k3++;






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



      //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 hauteur
     if(k1==hauteur.size()){
       Collections.sort(hauteur);
         for(int p=0; p<hauteur.size(); p++){
             System.out.println("hauteur trié"+hauteur.get(p));
             }
     }



   }

    count=k1;


   }

  Log.i(TAG,"nombre de lettres"+ count);
  int x=hauteur.get(hauteur.size()-1)+8; 
    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), new Scalar(255,0,0),2);
  Mat su = gray.submat(k,k+x,valx.get(0),gray.width());
  Imgcodecs.imwrite("/storage/emulated/0/DCIM/Lignes/sub"+j+".png",su);
  j++;

 }

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

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

 for (int i=0; i<contours.size(); i++)
   { 

    //  if(Imgproc.contourArea(contours.get(i)) >threshold){

       //Convert contours(i) from MatOfPoint to MatOfPoint2f
       MatOfPoint2f contour2f = new MatOfPoint2f( contours.get(i).toArray() );

       //Processing on mMOP2f1 which is in type MatOfPoint2f
       double approxDistance = Imgproc.arcLength(contour2f, true)*0.02;

       Imgproc.approxPolyDP(contour2f, approxCurve, approxDistance, true);
       //Convert back to MatOfPoint
       MatOfPoint points = new MatOfPoint( approxCurve.toArray() );

       // Get bounding rect of contour
       Rect rect = Imgproc.boundingRect(points);



       // draw enclosing rectangle (all same color, but you could use variable i to make them unique)

   if (approxDistance >1.5){

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





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



      //trier //Ajouter les colonnes

   if(k1==valx.size()){
       Collections.sort(valx);
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 hauteur
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)+8; 
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), 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++;
   }