简体   繁体   English

OpenCV图像裁剪和显示不起作用

[英]Opencv image cropping and displaying not working

Imageview showing the image from the uri perfectly fine. Imageview显示来自uri的图像非常好。 But i am trying to crop a portion from the image and display the cropped section using opencv. 但是我试图从图像中裁剪出一部分,并使用opencv显示裁剪的部分。

so i converted my bitmap object into mat and tried to crop it but the resulted bitmap not showing the image 所以我将位图对象转换为mat并尝试对其进行裁剪,但是结果位图未显示图像

bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imgUri);
Utils.bitmapToMat(bitmap, unCropped);
Rect roi = new Rect(Utility.x, Utility.y, Utility.width, Utility.height);
Mat cropped = new Mat(unCropped, roi);
Utils.matToBitmap(cropped,bitmap);
imageView.setImageBitmap(bitmap);
imageView.setVisibility(View.VISIBLE);

Whats i am doing wrong in here?? 我在这里做错了什么?

you can use the manual method for tht like i did in my project: Its for round image result: 您可以像在项目中那样使用手动方法:对于圆形图像结果:

public Bitmap getRoundedShape(Bitmap scaleBitmapImage) {
    int targetWidth = 50;
    int targetHeight = 50;
    Bitmap targetBitmap = Bitmap.createBitmap(targetWidth,
            targetHeight,Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(targetBitmap);
    Path path = new Path();
    path.addCircle(((float) targetWidth - 1) / 2,
            ((float) targetHeight - 1) / 2,
            (Math.min(((float) targetWidth),
                    ((float) targetHeight)) / 2),
            Path.Direction.CCW);

    canvas.clipPath(path);
    Bitmap sourceBitmap = scaleBitmapImage;
    canvas.drawBitmap(sourceBitmap,
            new Rect(0, 0, sourceBitmap.getWidth(),
                    sourceBitmap.getHeight()),
            new Rect(0, 0, targetWidth, targetHeight), null);
    return targetBitmap;
}

so just do:: 所以做:

imageView.setImageBitmap(getRoundedShape(bitmap));

It will give you a round image. 它会给您一个圆润的形象。 You can make change as shape your want..Hope this will help. 您可以根据自己的需要进行更改。希望这会有所帮助。 Appreciate if help... 感谢帮助...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM