【问题标题】:ROI Assertion failed (C++ and openCV)ROI 断言失败(C++ 和 openCV)
【发布时间】:2012-11-19 20:37:08
【问题描述】:

我有一个 Student 类和一个 imagehandler 类(作为 opencv 图像处理程序)

学生对象有一个字段:

imagehandler img;

在 imagehandler 类中有一个函数可以将图像从一个图像复制到另一个图像到某个位置。

void imagehandler::copy_paste_image(imagehandler& dst, int xLoc, int yLoc){
 cv::Rect roi(xLoc, yLoc, m_image.size().width, m_image.size().height);
 cv::Mat imageROI (dst.m_image, roi);
 m_image.copyTo(imageROI);
}

并且 imagehandler 类有一个 Mat 对象 m_image:

private:
 cv::Mat m_image;

现在主要是通过imagehandler指定的构造函数声明了一个新的图像。

我用来制作图像的构造函数:

imagehandler::imagehandler(int width, int height)
: m_image(width, height, CV_8UC3){


}

主要是这样声明图像:

imagehandler CSImg((4*300), (320 * ceil((float)(numOfCSStudents/4))));

相信我这一点:CSImg 比我想输入的所有图像都要大。

现在我想复制某个学生的图片,并将其输入到 CS Img 中。我就是这样做的:

studentsVector.at(i)->getImg().copy_paste_image(CSImg, CSWidthCount*300, CSHeightCount*320);

我得到这个错误:

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 /build/buildd/opencv-2.3.1/modules/core/src/matrix.cpp, line 303
terminate called after throwing an instance of 'cv::Exception'
  what():  /build/buildd/opencv-2.3.1/modules/core/src/matrix.cpp:303: 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

我注意到这发生在 roi 中,我不知道为什么。我是 openCV 的初学者,我这样做是为了做家庭作业。

谢谢。 如果您需要任何进一步的信息,请询问。

【问题讨论】:

  • 这表示数学/数字错误,您超出了某些东西的数组边界。检查以确保您的 ROI 小于您的图像,您的副本保持在所有图像范围内,等等
  • 他们确实...所有尺寸都是正确的。
  • 出错时 numOfCSStudents 的值是多少? numOfCSSStudents 是整数吗?

标签: c++ opencv


【解决方案1】:

我不确定 C++ API,但这是我在 C API 中所做的;

CvRect ROI = cvRect(x, y, width, height);
cvSetImageROI(srcImg, ROI);
IplImage* cropImg = cvCreateImage(cvGetSize(srcImg), IPL_DEPTH_8U, 1);//this part very important
cvCopy(srcImg, cropImg);
cvResetImageROI(srcImg);

【讨论】:

    猜你喜欢
    • 2014-11-24
    • 2018-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-15
    • 1970-01-01
    • 2014-02-10
    • 2016-07-21
    相关资源
    最近更新 更多