2014-05-13 24 views
3

我使用OpenCV進行我的圖像處理的Android應用OCR,我需要使用自適應閾值函數通過OpenCV中得到的二進制圖像,我的代碼如下所示:錯誤CV_8UC1 OpenCV的機器人

String SourcePicture = cursor.getString(URIPicture); 

    Bitmap ImagePicker = BitmapFactory.decodeFile(SourcePicture); 
    Bitmap InputImagePicker = ImagePicker.copy(Bitmap.Config.ARGB_8888, true); 

    Mat MatrixBitmapSmooth = Utils.bitmapToMat(InputImagePicker);     
    Mat MatrixBitmapBiner = new Mat(); 
    Imgproc.adaptiveThreshold(MatrixBitmapSmooth, MatrixBitmapBiner, 255,  
     Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 15, 4); 

    Bitmap BitmapBiner = null; 
    Utils.matToBitmap(MatrixBitmapBiner, BitmapBiner); 
    ImageInput.setImageBitmap(ImagePicker); 

看起來不錯,在Eclipse中沒有發現錯誤,但是當我在模擬器中運行它,它給錯誤「不幸的是APPS已停止工作」,日誌貓展:

05-12 01:09:55.425: E/AndroidRuntime(1164): Caused by: CvException [org.opencv.core.CvException: /home/andreyk/OpenCV2/trunk/opencv_2.3.1.b2/modules/imgproc/src/thresh.cpp:555: error: (-215) src.type() == CV_8UC1 in function void cv::adaptiveThreshold(const cv::_InputArray&, const cv::_OutputArray&, double, int, int, int, double) 

這有什麼錯我的代碼? ?如果我的代碼有問題?你能幫我嗎?

編輯

我從Mr.Roger Rowland有意見必須單通道灰度圖像,所以我編輯我的代碼一樣顯示圖像輸入波紋管:

String SourcePicture = cursor.getString(URIPicture); 
Bitmap InputImagePicker = BitmapFactory.decodeFile(SourcePicture); 

Mat MatrixBitmapSmooth = new 
     Mat(InputImagePicker.getWidth(),InputImagePicker.getHeight(), CvType.CV_8UC1); 
MatrixBitmapSmooth = Utils.bitmapToMat(InputImagePicker);     

Mat MatrixBitmapBiner = new     
     Mat(InputImagePicker.getWidth(),InputImagePicker.getHeight(), CvType.CV_8UC1); 
MatrixBitmapBiner = Utils.bitmapToMat(InputImagePicker); 

Imgproc.cvtColor(MatrixBitmapBiner, MatrixBitmapBiner, Imgproc.COLOR_RGB2GRAY); 
Imgproc.cvtColor(MatrixBitmapSmooth, MatrixBitmapSmooth, Imgproc.COLOR_BGR2GRAY); 
Imgproc.adaptiveThreshold(MatrixBitmapSmooth, MatrixBitmapBiner, 255,  
         Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 15, 8); 

Bitmap BitmapBiner = null; 
Utils.matToBitmap(MatrixBitmapBiner, BitmapBiner); 
BitmapBiner = BitmapBiner.copy(Bitmap.Config.ARGB_8888, true); 
ImageOutput.setImageBitmap(BitmapBiner); 

錯誤的logcat前消失:

05-12 01:09:55.425: E/AndroidRuntime(1164): Caused by: CvException [org.opencv.core.CvException: /home/andreyk/OpenCV2/trunk/opencv_2.3.1.b2/modules/imgproc/src/thresh.cpp:555: error: (-215) src.type() == CV_8UC1 in function void cv::adaptiveThreshold(const cv::_InputArray&, const cv::_OutputArray&, double, int, int, int, double) 

但是,它顯示錯誤它說:

05-14 00:18:40.252: E/AndroidRuntime(1371): Caused by: java.lang.NullPointerException 
+7

錯誤是告訴你,'adaptiveThreshold'預計單通道灰度圖像輸入,這也是[OpenCV文檔所述](http://docs.opencv.org/modules/imgproc/doc/miscellaneous_transformations.html#adaptivethreshold)。 –

+0

我編輯了我的代碼,我添加了下面的代碼:Imgproc.cvtColor(MatrixBitmapBiner,MatrixBitmapBiner,Imgproc.COLOR_RGB2GRAY); ..........但是它顯示錯誤:「由...引發:java.lang.NullPointerException 「,.....就像我上面顯示的更新問題,謝謝 –

+0

可能'BitmapBiner.copy(Bitmap.Config.ARGB_8888,true)'返回空 - 我猜是因爲從灰度到'ARGB_888'的轉換是不支持。也許你需要提出一個新的具體問題。 –

回答

0

此外,創建一個4通道席。

Mat MatrixBitmapSmooth = new 
    Mat(InputImagePicker.getWidth(),InputImagePicker.getHeight(), CvType.CV_8UC4); 

您需要將RGBA轉換爲灰色。使用COLOR_RGBA2GRAY

執行adaptiveThreshold後,如果您將其轉換回位圖,你可能需要將其轉換回RGBA所以使用COLOR_GRAY2RGBA

+1

你能給我舉個例子嗎?...... –