Error with OpenCV ROI











up vote
4
down vote

favorite












I am trying to make a program that will identify a pattern by sliding a ROI over an image and comparing the ROI with a template, it will compare pixel values of the ROI and the template and increase a counter by 1 each time a pixel matches, then I compare the counter with a threshold, if it passes a rectangle will be drawn, if not, it will continue to slide trough the image, if I run the debugger on it, it shows no errors sliding trough the image, but if i run it normally it throws the next 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 Mat, file /home/abuild/rpmbuild/BUILD/opencv-2.4.6.1/modules/core/src/matrix.cpp, line 323
terminate called after throwing an instance of 'cv::Exception'
what(): /home/abuild/rpmbuild/BUILD/opencv-2.4.6.1/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 Mat


I leave the code Bellow :



#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>


using namespace std;
using namespace cv;

Mat iOrig; //imagen que se analizara
Mat patron; //patron buscado
Point corner1,corner2; //esquinas del ROI
bool tresholdpass; //boolean returned by comparar()
Rect box; //rectangulo usado para crear el ROI
float porcentaje; //distancia a recorrer que sera dada por el tamano de la imagen muestra para el patron
float treshold; //treshold que debera ser superado para que se considere similar o igual al patron
float valpx; //valor almacenado en el pixel comparado
float contTreshold; //contador a comparar contra el porcentaje requerido para considerarse como patron encontrado
float totpxpat; //cantidad de pixeles de la imagen muestra del patron
float porctreshold; //porcentaje representativo para considerar que se encontro el patron en esa ROI

bool comparar(Mat region, Mat patron){

int i=0;
int j=0;
contTreshold=0;

for (i=0;i<patron.cols;i++){
for (j=0;j<patron.rows;j++){
Point a(i,j);
if(abs(region.at<float>(a))==abs(patron.at<float>(a))){
//se compara el contenido de el ROI y el patron en un punto
contTreshold++; //en caso de ser cierto, el contador aumenta en 1
}
}

}
totpxpat = patron.rows*patron.cols; //se cuentan la cantidad de pixeles dado columnas*renglones
porctreshold = 0.8; //el porcentaje que se usara para el treshold
treshold = totpxpat * porctreshold; //el treshold que determinara si se cumple el porcentaje de la imagen
//para saber si cumple el patron o no, en caso de q se supere, retornara verdadero
//en caso de que no se supere retornara falso y se elegira otro ROI para analizar
if (contTreshold>treshold){
return true;
}else{
return false;
}
}

int main() {

namedWindow("imagen");
namedWindow("segmento");

iOrig = imread( "/home/diego/Downloads/figuras.jpg",CV_LOAD_IMAGE_GRAYSCALE );
patron = imread("/home/diego/Downloads/patron.jpg",CV_LOAD_IMAGE_GRAYSCALE);

imshow("imagen",iOrig);
imshow("segmento",patron);
corner1.x = 1;
corner1.y = 1;
corner2.x = patron.cols;
corner2.y = patron.rows;


porcentaje = (int)patron.cols * 0.05;

while (corner2.x<iOrig.rows-(patron.rows*2)){
while(corner2.y<iOrig.cols-(patron.cols*2)){
box.width = abs (corner1.x-corner2.x)+1;
box.height = abs (corner1.y - corner2.y)+1;
box.x = min(corner1.x, corner2.x);
box.y = min(corner1.y, corner2.y);


//se crea una imagen de la region de interes seleccionada apartir de las 2 esquinas de la ROI
Mat region(iOrig,box); //region de interes que sera comparada

//se manda a comparar el ROI con el patron
tresholdpass=comparar(region,patron);
if (tresholdpass == true){
Mat local_img = iOrig.clone();
rectangle(local_img,corner1,corner2,Scalar(0,0,255));
imshow("imagen",local_img);

}
corner1.x+=porcentaje;
corner2.x+=porcentaje;
}
corner1.y+=porcentaje;
corner2.y+=porcentaje;
}

while (char(waitKey(1))!= 'q'){}
return 0;
}


I cant upload the images im using because of the reputation... but the original image is 800 x 450 and the template searched in the image is 131 x 132



sorry about the comments in my code being in Spanish, English is not my native language as you have guessed by now, i really don't know where my mistake is but... I hope it is simple thank you in advance!










share|improve this question


























    up vote
    4
    down vote

    favorite












    I am trying to make a program that will identify a pattern by sliding a ROI over an image and comparing the ROI with a template, it will compare pixel values of the ROI and the template and increase a counter by 1 each time a pixel matches, then I compare the counter with a threshold, if it passes a rectangle will be drawn, if not, it will continue to slide trough the image, if I run the debugger on it, it shows no errors sliding trough the image, but if i run it normally it throws the next 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 Mat, file /home/abuild/rpmbuild/BUILD/opencv-2.4.6.1/modules/core/src/matrix.cpp, line 323
    terminate called after throwing an instance of 'cv::Exception'
    what(): /home/abuild/rpmbuild/BUILD/opencv-2.4.6.1/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 Mat


    I leave the code Bellow :



    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream>
    #include <opencv2/highgui/highgui.hpp>
    #include <opencv2/core/core.hpp>
    #include <opencv2/imgproc/imgproc.hpp>


    using namespace std;
    using namespace cv;

    Mat iOrig; //imagen que se analizara
    Mat patron; //patron buscado
    Point corner1,corner2; //esquinas del ROI
    bool tresholdpass; //boolean returned by comparar()
    Rect box; //rectangulo usado para crear el ROI
    float porcentaje; //distancia a recorrer que sera dada por el tamano de la imagen muestra para el patron
    float treshold; //treshold que debera ser superado para que se considere similar o igual al patron
    float valpx; //valor almacenado en el pixel comparado
    float contTreshold; //contador a comparar contra el porcentaje requerido para considerarse como patron encontrado
    float totpxpat; //cantidad de pixeles de la imagen muestra del patron
    float porctreshold; //porcentaje representativo para considerar que se encontro el patron en esa ROI

    bool comparar(Mat region, Mat patron){

    int i=0;
    int j=0;
    contTreshold=0;

    for (i=0;i<patron.cols;i++){
    for (j=0;j<patron.rows;j++){
    Point a(i,j);
    if(abs(region.at<float>(a))==abs(patron.at<float>(a))){
    //se compara el contenido de el ROI y el patron en un punto
    contTreshold++; //en caso de ser cierto, el contador aumenta en 1
    }
    }

    }
    totpxpat = patron.rows*patron.cols; //se cuentan la cantidad de pixeles dado columnas*renglones
    porctreshold = 0.8; //el porcentaje que se usara para el treshold
    treshold = totpxpat * porctreshold; //el treshold que determinara si se cumple el porcentaje de la imagen
    //para saber si cumple el patron o no, en caso de q se supere, retornara verdadero
    //en caso de que no se supere retornara falso y se elegira otro ROI para analizar
    if (contTreshold>treshold){
    return true;
    }else{
    return false;
    }
    }

    int main() {

    namedWindow("imagen");
    namedWindow("segmento");

    iOrig = imread( "/home/diego/Downloads/figuras.jpg",CV_LOAD_IMAGE_GRAYSCALE );
    patron = imread("/home/diego/Downloads/patron.jpg",CV_LOAD_IMAGE_GRAYSCALE);

    imshow("imagen",iOrig);
    imshow("segmento",patron);
    corner1.x = 1;
    corner1.y = 1;
    corner2.x = patron.cols;
    corner2.y = patron.rows;


    porcentaje = (int)patron.cols * 0.05;

    while (corner2.x<iOrig.rows-(patron.rows*2)){
    while(corner2.y<iOrig.cols-(patron.cols*2)){
    box.width = abs (corner1.x-corner2.x)+1;
    box.height = abs (corner1.y - corner2.y)+1;
    box.x = min(corner1.x, corner2.x);
    box.y = min(corner1.y, corner2.y);


    //se crea una imagen de la region de interes seleccionada apartir de las 2 esquinas de la ROI
    Mat region(iOrig,box); //region de interes que sera comparada

    //se manda a comparar el ROI con el patron
    tresholdpass=comparar(region,patron);
    if (tresholdpass == true){
    Mat local_img = iOrig.clone();
    rectangle(local_img,corner1,corner2,Scalar(0,0,255));
    imshow("imagen",local_img);

    }
    corner1.x+=porcentaje;
    corner2.x+=porcentaje;
    }
    corner1.y+=porcentaje;
    corner2.y+=porcentaje;
    }

    while (char(waitKey(1))!= 'q'){}
    return 0;
    }


    I cant upload the images im using because of the reputation... but the original image is 800 x 450 and the template searched in the image is 131 x 132



    sorry about the comments in my code being in Spanish, English is not my native language as you have guessed by now, i really don't know where my mistake is but... I hope it is simple thank you in advance!










    share|improve this question
























      up vote
      4
      down vote

      favorite









      up vote
      4
      down vote

      favorite











      I am trying to make a program that will identify a pattern by sliding a ROI over an image and comparing the ROI with a template, it will compare pixel values of the ROI and the template and increase a counter by 1 each time a pixel matches, then I compare the counter with a threshold, if it passes a rectangle will be drawn, if not, it will continue to slide trough the image, if I run the debugger on it, it shows no errors sliding trough the image, but if i run it normally it throws the next 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 Mat, file /home/abuild/rpmbuild/BUILD/opencv-2.4.6.1/modules/core/src/matrix.cpp, line 323
      terminate called after throwing an instance of 'cv::Exception'
      what(): /home/abuild/rpmbuild/BUILD/opencv-2.4.6.1/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 Mat


      I leave the code Bellow :



      #include <stdio.h>
      #include <stdlib.h>
      #include <iostream>
      #include <opencv2/highgui/highgui.hpp>
      #include <opencv2/core/core.hpp>
      #include <opencv2/imgproc/imgproc.hpp>


      using namespace std;
      using namespace cv;

      Mat iOrig; //imagen que se analizara
      Mat patron; //patron buscado
      Point corner1,corner2; //esquinas del ROI
      bool tresholdpass; //boolean returned by comparar()
      Rect box; //rectangulo usado para crear el ROI
      float porcentaje; //distancia a recorrer que sera dada por el tamano de la imagen muestra para el patron
      float treshold; //treshold que debera ser superado para que se considere similar o igual al patron
      float valpx; //valor almacenado en el pixel comparado
      float contTreshold; //contador a comparar contra el porcentaje requerido para considerarse como patron encontrado
      float totpxpat; //cantidad de pixeles de la imagen muestra del patron
      float porctreshold; //porcentaje representativo para considerar que se encontro el patron en esa ROI

      bool comparar(Mat region, Mat patron){

      int i=0;
      int j=0;
      contTreshold=0;

      for (i=0;i<patron.cols;i++){
      for (j=0;j<patron.rows;j++){
      Point a(i,j);
      if(abs(region.at<float>(a))==abs(patron.at<float>(a))){
      //se compara el contenido de el ROI y el patron en un punto
      contTreshold++; //en caso de ser cierto, el contador aumenta en 1
      }
      }

      }
      totpxpat = patron.rows*patron.cols; //se cuentan la cantidad de pixeles dado columnas*renglones
      porctreshold = 0.8; //el porcentaje que se usara para el treshold
      treshold = totpxpat * porctreshold; //el treshold que determinara si se cumple el porcentaje de la imagen
      //para saber si cumple el patron o no, en caso de q se supere, retornara verdadero
      //en caso de que no se supere retornara falso y se elegira otro ROI para analizar
      if (contTreshold>treshold){
      return true;
      }else{
      return false;
      }
      }

      int main() {

      namedWindow("imagen");
      namedWindow("segmento");

      iOrig = imread( "/home/diego/Downloads/figuras.jpg",CV_LOAD_IMAGE_GRAYSCALE );
      patron = imread("/home/diego/Downloads/patron.jpg",CV_LOAD_IMAGE_GRAYSCALE);

      imshow("imagen",iOrig);
      imshow("segmento",patron);
      corner1.x = 1;
      corner1.y = 1;
      corner2.x = patron.cols;
      corner2.y = patron.rows;


      porcentaje = (int)patron.cols * 0.05;

      while (corner2.x<iOrig.rows-(patron.rows*2)){
      while(corner2.y<iOrig.cols-(patron.cols*2)){
      box.width = abs (corner1.x-corner2.x)+1;
      box.height = abs (corner1.y - corner2.y)+1;
      box.x = min(corner1.x, corner2.x);
      box.y = min(corner1.y, corner2.y);


      //se crea una imagen de la region de interes seleccionada apartir de las 2 esquinas de la ROI
      Mat region(iOrig,box); //region de interes que sera comparada

      //se manda a comparar el ROI con el patron
      tresholdpass=comparar(region,patron);
      if (tresholdpass == true){
      Mat local_img = iOrig.clone();
      rectangle(local_img,corner1,corner2,Scalar(0,0,255));
      imshow("imagen",local_img);

      }
      corner1.x+=porcentaje;
      corner2.x+=porcentaje;
      }
      corner1.y+=porcentaje;
      corner2.y+=porcentaje;
      }

      while (char(waitKey(1))!= 'q'){}
      return 0;
      }


      I cant upload the images im using because of the reputation... but the original image is 800 x 450 and the template searched in the image is 131 x 132



      sorry about the comments in my code being in Spanish, English is not my native language as you have guessed by now, i really don't know where my mistake is but... I hope it is simple thank you in advance!










      share|improve this question













      I am trying to make a program that will identify a pattern by sliding a ROI over an image and comparing the ROI with a template, it will compare pixel values of the ROI and the template and increase a counter by 1 each time a pixel matches, then I compare the counter with a threshold, if it passes a rectangle will be drawn, if not, it will continue to slide trough the image, if I run the debugger on it, it shows no errors sliding trough the image, but if i run it normally it throws the next 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 Mat, file /home/abuild/rpmbuild/BUILD/opencv-2.4.6.1/modules/core/src/matrix.cpp, line 323
      terminate called after throwing an instance of 'cv::Exception'
      what(): /home/abuild/rpmbuild/BUILD/opencv-2.4.6.1/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 Mat


      I leave the code Bellow :



      #include <stdio.h>
      #include <stdlib.h>
      #include <iostream>
      #include <opencv2/highgui/highgui.hpp>
      #include <opencv2/core/core.hpp>
      #include <opencv2/imgproc/imgproc.hpp>


      using namespace std;
      using namespace cv;

      Mat iOrig; //imagen que se analizara
      Mat patron; //patron buscado
      Point corner1,corner2; //esquinas del ROI
      bool tresholdpass; //boolean returned by comparar()
      Rect box; //rectangulo usado para crear el ROI
      float porcentaje; //distancia a recorrer que sera dada por el tamano de la imagen muestra para el patron
      float treshold; //treshold que debera ser superado para que se considere similar o igual al patron
      float valpx; //valor almacenado en el pixel comparado
      float contTreshold; //contador a comparar contra el porcentaje requerido para considerarse como patron encontrado
      float totpxpat; //cantidad de pixeles de la imagen muestra del patron
      float porctreshold; //porcentaje representativo para considerar que se encontro el patron en esa ROI

      bool comparar(Mat region, Mat patron){

      int i=0;
      int j=0;
      contTreshold=0;

      for (i=0;i<patron.cols;i++){
      for (j=0;j<patron.rows;j++){
      Point a(i,j);
      if(abs(region.at<float>(a))==abs(patron.at<float>(a))){
      //se compara el contenido de el ROI y el patron en un punto
      contTreshold++; //en caso de ser cierto, el contador aumenta en 1
      }
      }

      }
      totpxpat = patron.rows*patron.cols; //se cuentan la cantidad de pixeles dado columnas*renglones
      porctreshold = 0.8; //el porcentaje que se usara para el treshold
      treshold = totpxpat * porctreshold; //el treshold que determinara si se cumple el porcentaje de la imagen
      //para saber si cumple el patron o no, en caso de q se supere, retornara verdadero
      //en caso de que no se supere retornara falso y se elegira otro ROI para analizar
      if (contTreshold>treshold){
      return true;
      }else{
      return false;
      }
      }

      int main() {

      namedWindow("imagen");
      namedWindow("segmento");

      iOrig = imread( "/home/diego/Downloads/figuras.jpg",CV_LOAD_IMAGE_GRAYSCALE );
      patron = imread("/home/diego/Downloads/patron.jpg",CV_LOAD_IMAGE_GRAYSCALE);

      imshow("imagen",iOrig);
      imshow("segmento",patron);
      corner1.x = 1;
      corner1.y = 1;
      corner2.x = patron.cols;
      corner2.y = patron.rows;


      porcentaje = (int)patron.cols * 0.05;

      while (corner2.x<iOrig.rows-(patron.rows*2)){
      while(corner2.y<iOrig.cols-(patron.cols*2)){
      box.width = abs (corner1.x-corner2.x)+1;
      box.height = abs (corner1.y - corner2.y)+1;
      box.x = min(corner1.x, corner2.x);
      box.y = min(corner1.y, corner2.y);


      //se crea una imagen de la region de interes seleccionada apartir de las 2 esquinas de la ROI
      Mat region(iOrig,box); //region de interes que sera comparada

      //se manda a comparar el ROI con el patron
      tresholdpass=comparar(region,patron);
      if (tresholdpass == true){
      Mat local_img = iOrig.clone();
      rectangle(local_img,corner1,corner2,Scalar(0,0,255));
      imshow("imagen",local_img);

      }
      corner1.x+=porcentaje;
      corner2.x+=porcentaje;
      }
      corner1.y+=porcentaje;
      corner2.y+=porcentaje;
      }

      while (char(waitKey(1))!= 'q'){}
      return 0;
      }


      I cant upload the images im using because of the reputation... but the original image is 800 x 450 and the template searched in the image is 131 x 132



      sorry about the comments in my code being in Spanish, English is not my native language as you have guessed by now, i really don't know where my mistake is but... I hope it is simple thank you in advance!







      c++ opencv






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Apr 18 '15 at 1:57









      Diego Hernandez Reyes

      2113




      2113
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          8
          down vote













          It means you're trying to get the ROI region out of the image plane. You should make sure the ROI area to be within the image plane in order to avoid the crash.



          For your case, you can do like this:



          // check the box within the image plane
          if (0 <= box.x
          && 0 <= box.width
          && box.x + box.width <= iOrig.cols
          && 0 <= box.y
          && 0 <= box.height
          && box.y + box.height <= iOrig.rows){
          // box within the image plane
          Mat region(iOrig, box);
          }
          else{
          // box out of image plane, do something...
          }





          share|improve this answer





















          • thanks I have corrected it and yeah my mistake was in the loops, I was out of the bounds of the image
            – Diego Hernandez Reyes
            Jun 16 '15 at 16:38











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f29712057%2ferror-with-opencv-roi%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          8
          down vote













          It means you're trying to get the ROI region out of the image plane. You should make sure the ROI area to be within the image plane in order to avoid the crash.



          For your case, you can do like this:



          // check the box within the image plane
          if (0 <= box.x
          && 0 <= box.width
          && box.x + box.width <= iOrig.cols
          && 0 <= box.y
          && 0 <= box.height
          && box.y + box.height <= iOrig.rows){
          // box within the image plane
          Mat region(iOrig, box);
          }
          else{
          // box out of image plane, do something...
          }





          share|improve this answer





















          • thanks I have corrected it and yeah my mistake was in the loops, I was out of the bounds of the image
            – Diego Hernandez Reyes
            Jun 16 '15 at 16:38















          up vote
          8
          down vote













          It means you're trying to get the ROI region out of the image plane. You should make sure the ROI area to be within the image plane in order to avoid the crash.



          For your case, you can do like this:



          // check the box within the image plane
          if (0 <= box.x
          && 0 <= box.width
          && box.x + box.width <= iOrig.cols
          && 0 <= box.y
          && 0 <= box.height
          && box.y + box.height <= iOrig.rows){
          // box within the image plane
          Mat region(iOrig, box);
          }
          else{
          // box out of image plane, do something...
          }





          share|improve this answer





















          • thanks I have corrected it and yeah my mistake was in the loops, I was out of the bounds of the image
            – Diego Hernandez Reyes
            Jun 16 '15 at 16:38













          up vote
          8
          down vote










          up vote
          8
          down vote









          It means you're trying to get the ROI region out of the image plane. You should make sure the ROI area to be within the image plane in order to avoid the crash.



          For your case, you can do like this:



          // check the box within the image plane
          if (0 <= box.x
          && 0 <= box.width
          && box.x + box.width <= iOrig.cols
          && 0 <= box.y
          && 0 <= box.height
          && box.y + box.height <= iOrig.rows){
          // box within the image plane
          Mat region(iOrig, box);
          }
          else{
          // box out of image plane, do something...
          }





          share|improve this answer












          It means you're trying to get the ROI region out of the image plane. You should make sure the ROI area to be within the image plane in order to avoid the crash.



          For your case, you can do like this:



          // check the box within the image plane
          if (0 <= box.x
          && 0 <= box.width
          && box.x + box.width <= iOrig.cols
          && 0 <= box.y
          && 0 <= box.height
          && box.y + box.height <= iOrig.rows){
          // box within the image plane
          Mat region(iOrig, box);
          }
          else{
          // box out of image plane, do something...
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Apr 18 '15 at 13:06









          herohuyongtao

          36k1797126




          36k1797126












          • thanks I have corrected it and yeah my mistake was in the loops, I was out of the bounds of the image
            – Diego Hernandez Reyes
            Jun 16 '15 at 16:38


















          • thanks I have corrected it and yeah my mistake was in the loops, I was out of the bounds of the image
            – Diego Hernandez Reyes
            Jun 16 '15 at 16:38
















          thanks I have corrected it and yeah my mistake was in the loops, I was out of the bounds of the image
          – Diego Hernandez Reyes
          Jun 16 '15 at 16:38




          thanks I have corrected it and yeah my mistake was in the loops, I was out of the bounds of the image
          – Diego Hernandez Reyes
          Jun 16 '15 at 16:38


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f29712057%2ferror-with-opencv-roi%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          鏡平學校

          ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

          Why https connections are so slow when debugging (stepping over) in Java?