我正在使用opencv
来制作omr
扫描仪android
,并且我已经获得了用于处理示例图像的图像处理代码。现在当我将图像替换为来自相机的实际图像时,将bitmap
转换为Mat
,它会抛出nullpointer
,其中就好像我显示该位图图像一样正确显示它。 请帮我解决这个空指针异常。opencv android位图将结果映射为null
我的图像存储在存储器中,我通过意图通过额外元素将uri传递给当前活动。
我的图像处理活动和显示图像是
public class ImageDisplayActivity extends AppCompatActivity {
private static final String TAG = "ImageDisplayActivity";
private static ImageView imageView;
private String filename;
private Uri fileUri;
private Bitmap image;
private Mat localMat;
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS:
{
Log.i(TAG, "OpenCV loaded successfully");
localMat = new Mat();
} break;
default:
{
super.onManagerConnected(status);
} break;
}
}
};
public ImageDisplayActivity() {
Log.i(TAG, "Instantiated new " + this.getClass());
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_display);
filename = getIntent().getStringExtra("filename");
fileUri = Uri.parse(filename);
//filename="/storage/sdcard/omr.jpg";
//filename= Environment.getExternalStorageDirectory().getPath()+"download/BJq8M.jpg";
imageView = (ImageView)findViewById(R.id.displayImage);
BitmapFactory.Options options = new BitmapFactory.Options();
// downsizing image as it throws OutOfMemory Exception for larger
// images
image=BitmapFactory.decodeFile(fileUri.getPath());
Utils.bitmapToMat(image, localMat);
//detectEdges(image);
showAllCircles(image);
//this line properly displays image preview
//this.imageView.setImageBitmap(image);
}
其中Showcircle是在机器人控制台
E/AndroidRuntime
public void showAllCircles(Bitmap paramView) { Mat localMat1 = new Mat(); //throws nullpointer exception here for localmat1 Utils.bitmapToMat(paramView, localMat1); Mat localMat2 = new Mat(); Imgproc.GaussianBlur(localMat1, localMat2, new Size(5.0D, 5.0D), 7.0D, 6.5D); Object localObject = new Mat(); Imgproc.cvtColor(localMat2, (Mat)localObject, COLOR_RGB2GRAY); Mat cloneMat= ((Mat) localObject).clone(); //Mat blackwhite= ((Mat) localObject).clone(); localMat2 = localMat1.clone(); bitwise_not(cloneMat,cloneMat); Imgproc.threshold(cloneMat,localMat2,127,255,Imgproc.THRESH_OTSU); Mat thresh=localMat2.clone(); List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); List<MatOfPoint> questions = new ArrayList<MatOfPoint>(); List<MatOfPoint> sorted = new ArrayList<MatOfPoint>(); Mat hierarchy = new Mat(); Imgproc.findContours(localMat2, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE); Rect rect,rect2; int groups[] = new int[30]; int i=0,l=0; for (int contourIdx = 0; contourIdx < contours.size(); contourIdx++) { rect = Imgproc.boundingRect(contours.get(contourIdx)); //float area=rect.width/(float)rect.height; if(rect.width>=29 && rect.height>=29){ questions.add(contours.get(contourIdx)); //rect3 = Imgproc.boundingRect(questions.get(i)); //Log.i("Before------",rect3.tl()+" "); for(int ctr=0;ctr<questions.size()-1;ctr++){ MatOfPoint ctr1 = questions.get(i); rect = Imgproc.boundingRect(questions.get(i)); MatOfPoint ctr2 = questions.get(ctr); rect2 = Imgproc.boundingRect(questions.get(ctr)); if(rect.tl().x<rect2.tl().x){ questions.set(ctr,ctr1); questions.set(i,ctr2); } } //rect3 = Imgproc.boundingRect(questions.get(i)); //Log.i("after",rect3.tl()+" "); i++; if(i%5==0){ groups[l]=questions.indexOf(contours.get(contourIdx)); l++; //Log.i("groups---",""+groups[l-1]); } } } int j=0;i=0; while(j!=questions.size()){ for(int ctr=0;ctr<questions.size()-1;ctr++){ MatOfPoint ctr1 = questions.get(i); rect = Imgproc.boundingRect(questions.get(i)); MatOfPoint ctr2 = questions.get(ctr); rect2 = Imgproc.boundingRect(questions.get(ctr)); if(rect.tl().y<rect2.tl().y){ questions.set(ctr,ctr1); questions.set(i,ctr2); } } i++; j++; } //Collections.sort(questions, Collections.reverseOrder()); int bubble =0; for (int contourIdx = 0; contourIdx < questions.size(); contourIdx++) { Rect rectCrop = boundingRect(questions.get(contourIdx)); Mat imageROI= thresh.submat(rectCrop); int total = countNonZero(imageROI); double pixel =total/contourArea(questions.get(contourIdx))*100; if(pixel>=80){ Log.i("Answer:",bubble+" - "+contourIdx%5); //sorted.add(questions.get(contourIdx)); Imgproc.drawContours(localMat1, questions, contourIdx, new Scalar(255.0D, 0.0D, 0.0D), 2); bubble++; } } //Random rnd = new Random(); //i=0; //int r=0,g=0,b=0; /*for (int contourIdx = 0; contourIdx <sorted.size(); contourIdx++) { // r=rnd.nextInt(256); //g=rnd.nextInt(256); //b=rnd.nextInt(256); Imgproc.drawContours(localMat1, sorted, contourIdx, new Scalar(255.0D, 0.0D, 0.0D), 2); i=i+1; //Log.i("Local Objects","Local Object Point - // ------------------"+localMat2); }*/ // displays final image Utils.matToBitmap(localMat1, paramView); this.imageView.setImageBitmap(paramView); }
错误:致命异常:主 过程: com.example.admin.app,PID:7310 java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.admin.app/com.example.admin.app.ImageDisplayActivity}:java.lang.IllegalArgumentException:mat == null at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2325) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) at android.app.ActivityThread.access $ 800(ActivityThread.java:151) at android.app.ActivityThread $ H。在android.app.ActivityThread.main处的android.os.Looper.loop(Looper.java:135) 处使用handleMessage(ActivityThread.java:1303) (android.os.Handler.dispatchMessage(Handler.java:102) ActivityT hibernate.java:5254) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 引起来自:java.lang.IllegalArgumentException:mat == null at org.opencv .android.Utils.bitmapToMat(Utils.java:92) at org.opencv.android.Utils.bitmapToMat(Utils.java:102) at com.example.admin.bubbleboard.ImageDisplayActivity.onCreate(ImageDisplayActivity.java:94 ) at android.app.Activity.performCreate(Activity.java:5990) 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) 在机器人。 app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 在android.app.ActivityThread.access $ 800(ActivityThread.java:151) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1303) 在安卓.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThrea d.java:5254) at java.lang.reflect.Method。调用(本地方法) at java.lang.reflect.Method.invoke(Method.java:372)