2017-04-25 92 views
0

我正在嘗試寫圖像旋轉,但我現在有一些問題。這是我的代碼:OpenCv聲明失敗關於rect ROI

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 

我該如何解決這個錯誤。謝謝大家的幫助!

+0

嘗試交換「寬度」和「高度」參數....然後試一試 –

+0

對不起仍然是同樣的問題 –

+0

哪條線準確地拋出錯誤? –

回答

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