2013-05-28 6 views
3

이미지 (비트 맵)에 적응 형 임계 값을 사용하려고합니다. 안드로이드에서 매트로 변경 한 다음 그레이 스케일로 변환해야합니다.cverror android assertion failed (scn == 3) Android

다음은 비트 맵 만들기부터 시작하는 코드입니다. 방금 전에 사진을 찍었 기 때문에 자르기라는 이름이 붙었습니다.

  Bitmap bmCrop = BitmapFactory.decodeStream(iStream); 
      Bitmap bmThreshed = null; 
      /* 
      * Initialize the Mats 
      */ 
      Mat threshed = new Mat(bmCrop.getHeight(),bmCrop.getWidth(), CvType.CV_8UC1, new Scalar(4));//, new Scalar(4) 
      //Mat crop = new Mat(); 
      Mat crop = new Mat(bmCrop.getHeight(),bmCrop.getWidth(), CvType.CV_8UC1,new Scalar(4));//, new Scalar(4) 
      /* 
      * Convert the Mats to Grayscale 
      */ 
      if(!threshed.empty()) 
       Imgproc.cvtColor(threshed, threshed, Imgproc.COLOR_RGB2GRAY,1);// CURRENTLY BREAKING HERE 
      if(!crop.empty()) 
       Imgproc.cvtColor(crop, crop, Imgproc.COLOR_BGR2GRAY,1);   


      Utils.bitmapToMat(bmCrop, crop); 

      // Mat src, Mat dst, double maxValue, int adaptiveMethod, int thresholdType, int blockSize, double C 
      Imgproc.adaptiveThreshold(crop, threshed, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY_INV, 15, 8); 

      Utils.matToBitmap(threshed, bmThreshed); 
      bmThreshed = bmCrop; 

그것은 현재 라인에 위반된다 "Imgproc.cvtColor이 (타작 타작, Imgproc.COLOR_RGB2GRAY, 1)"여기서 내가 매트에 cvtColor을 시도합니다.

05-28 11:25:21.172: E/cv::error()(32505): OpenCV Error: Assertion failed (scn == 3 || scn == 4) in void cv::cvtColor(cv::InputArray, cv::OutputArray, int, int), file /home/reports/ci/slave/50-SDK/opencv/modules/imgproc/src/color.cpp, line 3414 
05-28 11:25:25.697: D/AndroidRuntime(32505): Shutting down VM 
05-28 11:25:25.707: W/dalvikvm(32505): threadid=1: thread exiting with uncaught exception (group=0x414b1930) 
05-28 11:25:25.747: E/AndroidRuntime(32505): FATAL EXCEPTION: main 
05-28 11:25:25.747: E/AndroidRuntime(32505): CvException [org.opencv.core.CvException: /home/reports/ci/slave/50-SDK/opencv/modules/imgproc/src/color.cpp:3414: error: (-215) scn == 3 || scn == 4 in function void cv::cvtColor(cv::InputArray, cv::OutputArray, int, int) 
05-28 11:25:25.747: E/AndroidRuntime(32505): ] 
05-28 11:25:25.747: E/AndroidRuntime(32505): at org.opencv.imgproc.Imgproc.cvtColor_0(Native Method) 
05-28 11:25:25.747: E/AndroidRuntime(32505): at org.opencv.imgproc.Imgproc.cvtColor(Imgproc.java:4017) 
05-28 11:25:25.747: E/AndroidRuntime(32505): at com.activity.IMGP_Camera$1.onManagerConnected(IMGP_Camera.java:263) 
05-28 11:25:25.747: E/AndroidRuntime(32505): at org.opencv.android.AsyncServiceHelper$1.onServiceConnected(AsyncServiceHelper.java:318) 
05-28 11:25:25.747: E/AndroidRuntime(32505): at android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:1101) 
05-28 11:25:25.747: E/AndroidRuntime(32505): at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:1118) 
05-28 11:25:25.747: E/AndroidRuntime(32505): at android.os.Handler.handleCallback(Handler.java:725) 
05-28 11:25:25.747: E/AndroidRuntime(32505): at android.os.Handler.dispatchMessage(Handler.java:92) 
05-28 11:25:25.747: E/AndroidRuntime(32505): at android.os.Looper.loop(Looper.java:137) 
05-28 11:25:25.747: E/AndroidRuntime(32505): at android.app.ActivityThread.main(ActivityThread.java:5226) 
05-28 11:25:25.747: E/AndroidRuntime(32505): at java.lang.reflect.Method.invokeNative(Native Method) 
05-28 11:25:25.747: E/AndroidRuntime(32505): at java.lang.reflect.Method.invoke(Method.java:511) 
05-28 11:25:25.747: E/AndroidRuntime(32505): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795) 
05-28 11:25:25.747: E/AndroidRuntime(32505): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562) 
05-28 11:25:25.747: E/AndroidRuntime(32505): at dalvik.system.NativeStart.main(Native Method) 

답변

4

다음은 현재 작동중인 코드입니다. 문제는 내가 매트를 초기화 한 직후와 cvtColor()를 수행하기 전에 Utils.bitmapToMat()을 수행하지 않고 있다는 것입니다.

  /* 
      * Initialize the bitmaps 
      */ 
      Bitmap bmCrop = BitmapFactory.decodeStream(iStream); 
      Bitmap bmThreshed = bmCrop; 
      /* 
      * Initialize the Mats 
      */ 
      Mat threshed = new Mat(bmCrop.getWidth(),bmCrop.getHeight(), CvType.CV_8UC1); 
      Utils.bitmapToMat(bmCrop, threshed); 

      Mat crop = new Mat(bmCrop.getWidth(),bmCrop.getHeight(), CvType.CV_8UC1); 
      Utils.bitmapToMat(bmCrop, crop); 
      /* 
      * Convert the Mats to Grayscale 
      */ 
      if(!threshed.empty()) 
       Imgproc.cvtColor(threshed, threshed, Imgproc.COLOR_RGB2GRAY); 
      if(!crop.empty()) 
       Imgproc.cvtColor(crop, crop, Imgproc.COLOR_BGR2GRAY);   

      /* 
      * Use Adaptive Thresholding on the grayscaled Mats 
      * crop -> threshed 
      * Mat src, Mat dst, double maxValue, int adaptiveMethod, int thresholdType, int blockSize, double C 
      */ 
      Imgproc.adaptiveThreshold(crop, threshed, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 15, 8);//15, 8 were original tests. Casey was 75,10 

      Utils.matToBitmap(threshed, bmThreshed); 
      bmThreshed = bmCrop; 
5

어설 실패 cvtColor()에 입력 화상이 어느 채널 3 또는 4가없는 것을 나타낸다 :

다음은 로그 캣의 출력이다. 문제가 CV_8UC1 (단일 채널 그레이 스케일) 인 threshed을 초기화하지만, 을 RGB2GRAY 코드로 호출하면 3 채널 RGB 이미지가 필요합니다.

회색 음영으로되어 있기 때문에 cvtColor()threshed에 보내지 않아도됩니다. 그리고 자른 이미지를 그레이 스케일로 변환하는 것이라고 가정하면 뒤에 색 변환을 수행하고 데이터로 Mat을 채 웁니다.

 Mat threshed = new Mat(bmCrop.getHeight(),bmCrop.getWidth(), CvType.CV_8UC1, new Scalar(4));//, new Scalar(4) 
     //Mat crop = new Mat(); 
     Mat crop = new Mat(bmCrop.getHeight(),bmCrop.getWidth(), CvType.CV_8UC1,new Scalar(4));//, new Scalar(4) 

     Utils.bitmapToMat(bmCrop, crop); 
     if(!crop.empty()) 
      Imgproc.cvtColor(crop, crop, Imgproc.COLOR_BGR2GRAY,1); 

면책 조항 : : 나는 자바 또는 OpenCV의 자바 API에 익숙하지 않은, 그래서이 코드를 조절해야합니다 따라서, 코드의 관련 부분은 다음과 같이 보일 수 있습니다.

+0

감사합니다. –

관련 문제