2013-04-12 193 views
1

我想裁剪圖像,然後能夠顯示圖像。我有兩個數組中的四個點,它會形成一個矩形形狀。我有一個代碼,我認爲可以完成這項工作,但它給出了一個錯誤。這是我希望你能幫助我的代碼。我正在使用android,只使用Java。使用四點OpenCV裁剪圖像

int startX =locListX.get(0); 
int startY =locListY.get(0); 
Collections.sort(locListX); 
Collections.sort(locListY); 
int width=locListX.get(3)-locListX.get(0); 
int height=locListY.get(3)-locListY.get(0); 
Mat image = mFind; 
Rect rectCrop = new Rect(startX, startY, width, height); 
Mat imCrop= new Mat(image,rectCrop); 
Utils.matToBitmap(imCrop, bmp5); 
mGrayView.setImageBitmap(bmp5); 
+1

你什麼錯誤?請同時打印這兩個集合的內容。 – iiro

+0

收藏基本上是向左,向右,bottomright,bottomleft與一個得到的X位置和另一個Y位置,我只是想得到一個粗糙的矩形我想看到的。我使用模板匹配來獲得點,所以我只是使用左上角的點作爲起點,然後使用排序獲得圖像的最長寬度和高度 –

+0

錯誤:斷言失敗(src.dims == 2 && info.height ==(uint32_t)src.rows && info.width ==(uint32_t)src.cols)void Java_org_opencv_android_Utils)_nMatToBitmap2(JNIEnv *,jclass,jlong​​,jobject,jboolean,file/home/reports/ci/slave/opencv/modules/java/generator/src/cpp/util.cpp,第107行 nMatToBitmap catched cv :: Exception:/home/report/ci/slave/opencv/modules/java/generator/src/utils.cpp:107:error: (-215)src.dims == 2 && info.height ==(uint32_t)src.rows && info.width ==(uint32_t)src.cols在函數中void Java_org)opencv_android_Utils_nMatToBitmap2(KNIENV *,jclass,jlong​​,jobject,jboolean) –

回答

0

在這種方法中得四點的原稿板,那麼你可以用下面的方法削減這一形象:

 public Bitmap warpDisplayImage(Mat inputMat) { 
List<Point> newClockVisePoints = new ArrayList<>(); 

int resultWidth = inputMat.width(); 
int resultHeight = inputMat.height(); 

Mat startM = Converters.vector_Point2f_to_Mat(orderRectCorners(Previes method four poit list(like : List<Point> points))); 

Point ocvPOut4 = new Point(0, 0); 
Point ocvPOut1 = new Point(0, resultHeight); 
Point ocvPOut2 = new Point(resultWidth, resultHeight); 
Point ocvPOut3 = new Point(resultWidth, 0); 



    ocvPOut3 = new Point(0, 0); 
    ocvPOut4 = new Point(0, resultHeight); 
    ocvPOut1 = new Point(resultWidth, resultHeight); 
    ocvPOut2 = new Point(resultWidth, 0); 
} 

Mat outputMat = new Mat(resultWidth, resultHeight, CvType.CV_8UC4); 

List<Point> dest = new ArrayList<Point>(); 
dest.add(ocvPOut3); 
dest.add(ocvPOut2); 
dest.add(ocvPOut1); 
dest.add(ocvPOut4); 


Mat endM = Converters.vector_Point2f_to_Mat(dest); 

Mat perspectiveTransform = Imgproc.getPerspectiveTransform(startM, endM); 

Imgproc.warpPerspective(inputMat, outputMat, perspectiveTransform, new Size(resultWidth, resultHeight), Imgproc.INTER_CUBIC); 


Bitmap descBitmap = Bitmap.createBitmap(outputMat.cols(), outputMat.rows(), Bitmap.Config.ARGB_8888); 
Utils.matToBitmap(outputMat, descBitmap); 



return descBitmap; 

}