opencv android bitmap to map results in null
opencv android bitmap to map results in null
我正在使用 opencv
在 android
中制作 omr
扫描仪,并且我有图像处理代码处理示例图像。现在,当我在将 bitmap
转换为 Mat
时用相机的实际图像替换示例图像时,它会抛出 nullpointer
,就像我显示该位图图像一样,它会正确显示它。
请帮我解决这个空指针异常。
我的图片存储在存储中,我正在通过 extras in intent 将 uri 传递给当前 activity。
我的图像处理activity和显示图像是
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 在哪里
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);
}
android 控制台出错
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.access0(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.access0(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)
我已经解决了这个问题。这是因为我的 opencv
库是在 onCreate
方法之后加载的。
我正在使用 opencv
在 android
中制作 omr
扫描仪,并且我有图像处理代码处理示例图像。现在,当我在将 bitmap
转换为 Mat
时用相机的实际图像替换示例图像时,它会抛出 nullpointer
,就像我显示该位图图像一样,它会正确显示它。
请帮我解决这个空指针异常。
我的图片存储在存储中,我正在通过 extras in intent 将 uri 传递给当前 activity。
我的图像处理activity和显示图像是
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 在哪里
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);
}
android 控制台出错
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.access0(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.access0(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)
我已经解决了这个问题。这是因为我的 opencv
库是在 onCreate
方法之后加载的。