OpenCV ROI 出现错误

7

我正在尝试制作一个程序,通过滑动ROI来识别图案,并将ROI与模板进行比较。它会比较ROI和模板的像素值,并在每次像素匹配时将计数器增加1,然后将计数器与阈值进行比较,如果通过,则绘制矩形,否则它将继续滑动图像。如果我在调试器中运行它,它不会显示滑动图像时的任何错误,但如果我正常运行它,则会抛出下一个异常:

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

我留下了以下代码:

我留下了以下代码:

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

由于声誉问题,我无法上传正在使用的图像...但原始图像大小为800 x 450,搜索的模板大小为131 x 132。

很抱歉我的代码注释是用西班牙语写的,显然英语并不是我的母语,我真的不知道我的错误在哪里,但是...我希望它很简单,提前感谢您!

1个回答

13

这意味着您正在尝试从图像平面中获取ROI区域。为了避免崩溃,您应该确保ROI区域在图像平面内。

对于您的情况,您可以这样做:

// 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...
}

谢谢,我已经纠正了。是的,我的错误在于循环中,我超出了图像的边界。 - Diego Hernandez Reyes

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接