I am making omr
scanner in android
using opencv
and i have got image processing code working on sample image. now when i replaced sample image with actual image from camera while converting bitmap
to Mat
it throws nullpointer
, where as if i display that bitmap image it displays it properly.
Please help me with this nullpointer exception.
my image is stored in storage and i am passing uri to current activity through extras in intent.
my image processing activity and displaying image is
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);
}
where Showcircle is
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);
}
Error in android console
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.admin.app, PID: 7310 java.lang.RuntimeException: Unable to start activity 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.handleMessage(ActivityThread.java:1303) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.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) Caused by: 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) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) at android.app.ActivityThread.access$800(ActivityThread.java:151) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372)
I got this issue sorted. It was because my opencv
library was loading after onCreate
method.