OpenCV是一個基於BSD許可(開源)發行的跨平臺計算機視覺庫,能夠運行在Linux、Windows和Mac OS操做系統上。它輕量級並且高效——由一系列 C 函數和少許 C++ 類構成,同時提供了java, Python、Ruby、MATLAB等語言的接口,實現了圖像處理和計算機視覺方面的不少通用算法。java
在用java的jni調用opencv時,識別單張圖片沒問題,當圖片數量不少時,報錯c++
OpenCV Error: Insufficient memory (Failed to allocate 411068927 bytes) in cv::OutOfMemoryError, file opencv\modules\core\src\alloc.cpp, line 52 OpenCV Error: 算法
System.out.println("\nRunning Template Matching"); Mat img = Highgui.imread(inFile); Mat templ = Highgui.imread(templateFile); // / Create the result matrix int result_cols = img.cols() - templ.cols() + 1; int result_rows = img.rows() - templ.rows() + 1; Mat result = new Mat(result_rows, result_cols, CvType.CV_64FC1); // / Do the Matching and Normalize Imgproc.matchTemplate(img, templ, result, match_method); Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat()); // / Localizing the best match with minMaxLoc MinMaxLocResult mmr = Core.minMaxLoc(result); Point matchLoc; if (match_method == Imgproc.TM_SQDIFF || match_method == Imgproc.TM_SQDIFF_NORMED) { matchLoc = mmr.minLoc; } else { matchLoc = mmr.maxLoc; } // / Show me what you got Core.rectangle(img, matchLoc, new Point(matchLoc.x + templ.cols(), matchLoc.y + templ.rows()), new Scalar(0, 255, 0)); // Save the visualized detection. System.out.println("Writing "+ outFile); Highgui.imwrite(outFile, img);
jni調用native的c++方法是內存溢出,原來是沒釋放對象。顯示調用:函數
img.release();ui
templ.release();
result.release();操作系統
解決。c++就是麻煩,主動釋放引用的對象。code