2017-05-09 3 views
1

Ich habe eine Eingabe Bild sourceMat.kopierte Bild (Opencv Mat) kann nicht in Bitmap

Crop ein 40x40 roi bei (10,10) gelegen:

Mat roi = sourceMat.submat(10, 50, 10, 50); 

eine Bitmap der Größe 40x40 erstellen:

Bitmap tempBitmap = Bitmap.createBitmap(40, 40, thumbnail.getConfig()); 

die Matte konvertieren und in Image auf Bitmap:

Utils.matToBitmap(roi, tempBitmap); 
imageView.setImageBitmap(tempBitmap); 

Anstatt das beschnittene Bild anzuzeigen, wird das gesamte Originalbild in eine 40x40-Bitmap verkleinert.

Ich arbeite mit openCVLibrary320 und android sdk Version 25

+0

Sie müssen einen Teil aus einer ImageMat-Datei, d. H. SourceMat, zuschneiden und auf einem imageView anzeigen. –

+0

Danke für Ihre Antwort @Antonio und Arbeiten an openCVLibrary320 und Android SDK Version 25. –

+0

Ich würde auch versuchen, Bitmap.createBitmap (40, 40, RGB_565); 'Was ist' thumbnail' übrigens? – Antonio

Antwort

0

ersetzen:

Bitmap tempBitmap = Bitmap.createBitmap(40, 40, thumbnail.getConfig()); 

mit entweder einer von ihnen:

Bitmap.createBitmap(40, 40, Bitmap.Config.RGB_565); 
Bitmap.createBitmap(40, 40, Bitmap.Config.ARGB_8888); 

documentation See.

0

Der Code funktioniert einwandfrei und der Code wird geringfügig geändert.

private void showDialog(File pictureFile) { 
    // custom dialog 
    final Dialog dialog = new Dialog(this, android.R.style.Theme_Black_NoTitleBar_Fullscreen); 
    dialog.setContentView(R.layout.image_dialog); 
    dialog.setTitle("Image"); 
    // find the imageview and draw it! 
    ImageView image = (ImageView) dialog.findViewById(R.id.image); 
    Bitmap thumbnail; 
    try { 
     thumbnail = MediaStore.Images.Media.getBitmap(
       getContentResolver(), Uri.fromFile(pictureFile)); 
     cropImage(image, thumbnail); 
//   Imgproc.threshold(tmp, tmp, 60, 100, Imgproc.THRESH_BINARY); 
//   Utils.matToBitmap(tmp, image1); 
//   Imgproc.threshold(tmp, tmp, 1, 255, Imgproc.THRESH_OTSU); 
     //using floodfill and watershed to remove noise 
//   Mat mask = new Mat(tmp.rows() + 2, tmp.cols() + 2, CvType.CV_8UC1); 
//   Imgproc.floodFill(tmp, mask, new Point(tmp.cols() - 10, 10), new Scalar(255.0, 255.0, 255)); 
     image.setImageBitmap(thumbnail); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK); 
    // if button is clicked, close the custom dialog 
    dialogButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      dialog.dismiss(); 
     } 
    }); 
    dialog.show(); 
} 

private void cropImage(ImageView image, Bitmap thumbnail) { 
    Mat src = new Mat(); 
    Utils.bitmapToMat(thumbnail, src); 
    //converts mat color 
    Imgproc.cvtColor(src, src, Imgproc.COLOR_RGBA2BGR); 
    Rect roi = new Rect(40, 100, 100, 120); 
    Mat cropped = new Mat(src, roi); 
    Bitmap tempBmp1 = Bitmap.createBitmap(100, 120, Bitmap.Config.ARGB_8888); 
    Utils.matToBitmap(cropped, tempBmp1); 
    image.setImageBitmap(tempBmp1); 
} 
+0

Können Sie sich darauf entwickeln? Irgendetwas muss sich geändert haben, von dem Zeitpunkt an, an dem es nicht mehr funktionierte, bis zu dem Moment, an dem es zu arbeiten begann. Diese Antwort/Frage wird niemandem so helfen! – Antonio