![](/img/trans.png)
[英]How to find and correct skew using leptonica library in android tesseract tool
[英]How to correct a RotatedRect skew with OpenCV?
我使用Opencv編寫了一個Android應用程序,我的圖像處理算法需要針對檢測到的矩形進行正確的旋轉,因此,作為過程的開始,我
RotatedRect
。 getRotationMatrix2D
創建旋轉矩陣 warpAffine
執行仿射變換 getRectSubPix
提取檢測到的矩形 孔處理工作正常,但是對象傾斜有問題。
逆時針旋轉對象時,歪斜校正效果很好, 但是如果順時針旋轉,則矩形將旋轉90度。
我需要提到的是,由於Opencv相機的方向錯誤,因此我首先在矩形旋轉角度上添加-90。
這是我的代碼
/* *******************************************************************************************
* get angle and size from the rotated rect
*******************************************************************************************/
double rect_angle = rbox.angle - 90.0;
Size rect_size = rbox.size;
/* *******************************************************************************************
* correct the orientation
*******************************************************************************************/
double d = rect_size.width;
rect_size.width = rect_size.height;
rect_size.height = d;
M = Imgproc.getRotationMatrix2D(rbox.center, rect_angle, 1.0);
Imgproc.warpAffine(origMat, rotated, M, origMat.size());
/* *******************************************************************************************
* crop the resulting image
*******************************************************************************************/
if (rect_size.width > 75 && rect_size.height > 75)
Imgproc.getRectSubPix(rotated, new Size(rect_size.width - 75, rect_size.height - 75), rbox.center, rotated);
/* *******************************************************************************************
* resize the result image because rotated has the size of the rect not the original image
* which cause the preview camera to be black because of wrong dimensions
*******************************************************************************************/
Imgproc.resize(rotated, rotated, origMat.size());
這是順時針旋轉對象時的結果圖像
這是對象逆時針旋轉的結果圖像
這是原始圖像
我應該提到的是,當對象不旋轉時,角度為-90;如果順時針旋轉,則角度為-179;如果逆時針旋轉,則角度為-179,並趨於-90。
我試圖像這樣設置條件
if (rbox.angle < -90.0) {
rect_angle -= 90.0;
}
但是沒有任何效果。
我知道這是數學問題,但我找不到解決方法,希望大家能幫助我解決這個問題。
對於每個關心答案的人,我都找到了解決方案。
我研究了檢測到的矩形的寬度和高度,而不是角度。
因此,在創建旋轉矩陣之前,我添加了此測試代碼
if (rbox.size.width < rbox.size.height) {
rect_angle += 90.0;
double d1 = rect_size.height;
rect_size.height = rect_size.width;
rect_size.width = d1;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.