[英]Convert OpenCV mat to Android Bitmap
我正在嘗試將OpenCV墊轉換為android位圖,但這給我的圖像帶藍色調(黃色變成藍色)! 即使我沒有對圖像進行任何處理! 我不知道為什么會這樣。 以下是一些相關代碼:
File file = new File(imgDecodableString);
image = Imgcodecs.imread(file.getAbsolutePath(),Imgcodecs.CV_LOAD_IMAGE_COLOR);
resultBitmap = Bitmap.createBitmap(image.cols(), image.rows(),Bitmap.Config.ARGB_8888);;
Utils.matToBitmap(image, resultBitmap);
Bitmap mResult = resultBitmap;
ImageView imgView = (ImageView) findViewById(R.id.imgView);
imgView.setImageBitmap(mResult);
//imgView.setImageBitmap(BitmapFactory.decodeFile(imgDecodableString));
我是android應用程序開發的新手,所以我可能缺少一些簡單的東西。 感謝幫助!
編輯:
我正在上傳圖像以供參考。
疑似問題在於RGB
顏色約定,Android遵循RGB
顏色約定,但OpenCV遵循BGR
顏色約定,您可以使用Imgproc.cvtColor(mat, Imgproc.COLOR_BGR2RGBA)
對其進行糾正,然后再將其顯示在ImageView
。
根據給出的建議,我制作了一個將Mat轉換為Bitmap的函數。 此功能運行完美。
private static Bitmap convertMatToBitMap(Mat input){
Bitmap bmp = null;
Mat rgb = new Mat();
Imgproc.cvtColor(input, rgb, Imgproc.COLOR_BGR2RGB);
try {
bmp = Bitmap.createBitmap(rgb.cols(), rgb.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(rgb, bmp);
}
catch (CvException e){
Log.d("Exception",e.getMessage());
}
return bmp;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.