android grab cut open cv not getting bit map displayed in image view. [closed]

asked 2018-07-27 00:26:35 -0600

updated 2020-10-05 23:24:18 -0600

I am running a grab cut algorithm where the user can choose an image he wants to grab cut in his external storage on android phone. Problem is during the onPostExecute method of async task I get a super tiny bitmap that looks somewhat like a dead pixel and I can't even see the image. I have tried the same code on open cv 2.x version and it worked fine but when I switch over to open cv 3.4.0 I can't see the result being displayed.

MainActivity.

private int STORAGE_PERMISSION_CODE = 1;
private static String TAG = "GrabCutActivity";
static final int REQUEST_OPEN_IMAGE = 1;
String mCurrentPhotoPath;
Bitmap mBitmap;
ImageView mImageView;
int touchCount = 0;
Point tl;
Point br;
boolean targetChose = false;
ProgressDialog dlg;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (!OpenCVLoader.initDebug()) {
        Log.e(this.getClass().getSimpleName(), "  OpenCVLoader.initDebug(), not working.");
    } else {
        Log.d(this.getClass().getSimpleName(), "  OpenCVLoader.initDebug(), working.");
    }


    mImageView = findViewById(R.id.imgDisplay);
    dlg = new ProgressDialog(this);
    tl = new Point();
    br = new Point();
    if (!OpenCVLoader.initDebug()) {
        Log.i(TAG,"load successful");
    }
}

public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main,menu);
    return true;
}




private void setPic() {
    // use to compress image
    int targetW = mImageView.getWidth();
    int targetH = mImageView.getHeight();

    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;

    int scaleFactor = Math.min(photoW / targetW, photoH / targetH);

    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;
    bmOptions.inPurgeable = true;

    mBitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
    mImageView.setImageBitmap(mBitmap);
    Log.i(TAG,"set pic here");

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case REQUEST_OPEN_IMAGE:
            if (resultCode == RESULT_OK) {
                Uri imgUri = data.getData();
                String[] filePathColumn = { MediaStore.Images.Media.DATA };

                // used to get file path from uri.
                Cursor cursor = getContentResolver().query(imgUri, filePathColumn,
                        null, null, null);
                cursor.moveToFirst();

                int colIndex = cursor.getColumnIndex(filePathColumn[0]);
                mCurrentPhotoPath = cursor.getString(colIndex);
                Log.i(TAG,mCurrentPhotoPath);
                cursor.close();
                setPic();
            }
            break;
    }
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    switch (id) {
        case R.id.action_open_img:
            if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE )==
                    PackageManager.PERMISSION_GRANTED) {
                Intent getPictureIntent = new Intent(Intent.ACTION_GET_CONTENT);
                getPictureIntent.setType("image/*");
                Intent pickPictureIntent = new Intent(Intent.ACTION_PICK,
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                Intent chooserIntent = Intent.createChooser(getPictureIntent, "Select Image");
                chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{
                        pickPictureIntent
                });
                startActivityForResult(chooserIntent, REQUEST_OPEN_IMAGE);
            } else {
                requestStoragePermission();
                Toast.makeText(MainActivity.this, "No permission?", Toast.LENGTH_SHORT).show();
            }
            return true;
        case R.id.action_choose_target:
            if (mCurrentPhotoPath != null)
                targetChose = false;
            mImageView.setOnTouchListener(new View.OnTouchListener() {

                //what happens when user touches the image
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    // event.getAction() returns what type of action performed by the user
                    // ACTION_DOWN means make your finger touch the screen.
                    // ACTION_UP opposite
                    if (event.getAction() == MotionEvent.ACTION_DOWN) {
                        if (touchCount == 0) {
                            tl.x = event.getX();
                            tl.y = event.getY();

                            Log.i(TAG,"x " + tl.x + " y " + tl.y);
                            touchCount++;
                        }
                        else if (touchCount == 1) {
                            br.x = event.getX();
                            br.y = event.getY();
                            Log.i(TAG,"br.x " + br.x + " br.y " + br.y);
                            Paint rectPaint ...
(more)
edit retag flag offensive reopen merge delete

Closed for the following reason question is not relevant or outdated by sturkmen
close date 2020-11-07 02:19:06.949437