我有一个输入图像sourceMat。
裁剪位于(10,10)的40x40 roi:
Mat roi = sourceMat.submat(10, 50, 10, 50);
创建大小为40x40的位图:
Bitmap tempBitmap = Bitmap.createBitmap(40, 40, thumbnail.getConfig());
将Mat转换为位图并在ImageView中设置:
Utils.matToBitmap(roi, tempBitmap);
imageView.setImageBitmap(tempBitmap );
它不是显示裁剪的图像,而是显示整个原始图像缩小为40x40位图。
我正在使用openCVLibrary320和android sdk 25版
答案 0 :(得分:0)
替换:
Bitmap tempBitmap = Bitmap.createBitmap(40, 40, thumbnail.getConfig());
其中任何一个:
Bitmap.createBitmap(40, 40, Bitmap.Config.RGB_565);
Bitmap.createBitmap(40, 40, Bitmap.Config.ARGB_8888);
请参阅documentation。
答案 1 :(得分:-1)
代码工作正常,代码会有小变化。
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);
}