【问题标题】:OpenCv assertion failed about rect ROIOpenCv 关于 rect ROI 的断言失败
【发布时间】:2017-04-25 16:46:45
【问题描述】:

我正在尝试编写图像旋转,但我现在遇到了一些问题。这是我的代码:

Mat rotateMagnify (Mat& img, int degree){
double angle = degree  * CV_PI / 180.;
double a = sin(angle), b = cos(angle);

int width = img.cols, height = img.rows;

int width_rotate = int(height * fabs(b) - width * fabs(a) + width);
int height_rotate = int(width * fabs(a) + height * fabs(b) + height);

Mat img_rotate;
img_rotate.create((width_rotate, height_rotate), img.depth(), img.channels());


int tempLength = sqrt((double)width * width + (double)height *height) + 10;
int tempX = (tempLength + 1) / 2 - width / 2;
int tempY = (tempLength + 1) / 2 - height / 2;


Mat temp;
temp.create((tempLength, tempLength), img.depth(), img.channels());
//cvZero(&img_rotate);

Mat roiImage = temp(Rect(Point(tempX, tempY), Point(width, height)));

//roiImage.ResetImageROI(roiImage);

img.copyTo(roiImage);

float m[6];
int w = roiImage.cols;
int h = roiImage.rows;
m[0] = b;
m[1] = a;
m[3] = -m[1];
m[4] = m[0];

m[2] = w * 0.5f;
m[5] = h * 0.5f;
CvMat M = cvMat(2, 3, CV_32F, m);

cvGetQuadrangleSubPix(&roiImage, &img_rotate, &M);
roiImage.release();

return img_rotate;}

很遗憾,此代码不起作用。我收到这样的错误:

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 cv::Mat::Mat, file C:\builds\master_PackSlave-win32-vc12-
shared\opencv\modules\core\src\matrix.cpp, line 495

如何解决此错误。谢谢大家的帮助!

【问题讨论】:

  • 尝试交换widthheight参数....然后试一试
  • 对不起还是同样的问题
  • 究竟是哪一行抛出了错误?
  • 在运行时打印出尺寸,你应该弄清楚为什么会发生这种情况

标签: c++ opencv roi


【解决方案1】:

当您尝试复制(裁剪)图像的一部分但您指定的 x,y 值是 -ve 或大于实际图像的大小时,通常会引发此错误。在尝试之前添加断点并检查int tempX = (tempLength + 1) / 2 - width / 2; int tempY = (tempLength + 1) / 2 - height / 2; 的值尝试处理上述条件

Mat roiImage = temp(Rect(Point(tempX, tempY), Point(width, height)));

//roiImage.ResetImageROI(roiImage);

img.copyTo(roiImage);

【讨论】:

    猜你喜欢
    • 2014-11-24
    • 1970-01-01
    • 2018-01-29
    • 2014-02-10
    • 2012-11-26
    • 2014-05-28
    • 2020-12-31
    • 2015-09-12
    • 2015-04-25
    相关资源
    最近更新 更多