4

我正在尝试将 bmp 文件转换为 Mat,然后将其转换为灰度。但我无法让它工作。这是我所拥有的:

String filename = "/mnt/sdcard/DCIM/01.bmp";    
Bitmap bmp = BitmapFactory.decodeFile(filename);
Mat imgToProcess = null;

Utils.bitmapToMat(bmp, imgToProcess);

但是每当使用最后一行时,应用程序就会崩溃(其余时间它会继续正常运行)。

其余的代码将是:

Imgproc.cvtColor(imgToProcess, imgToProcess, Imgproc.COLOR_BGR2GRAY);
Imgproc.cvtColor(imgToProcess, imgToProcess, Imgproc.COLOR_GRAY2RGBA, 4);
Utils.matToBitmap(imgToProcess, bmp);

我不知道这是否有效,因为我无法从代码的早期部分将文件转换为 Mat 。查看 Utils 的文档(在此处找到)我正在正确使用它,但它仍然无法正常工作。

有谁可以帮我离开这里吗?

4

1 回答 1

3

换线:

Mat imgToProcess = null;

对此:

Mat imgToProcess = new Mat();

或这个:

 Mat imgToProcess = new Mat(bmp.getHeight(), bmp.getHeight(), CvType.CV_8UC4);

你为什么不直接使用Highgui.imread呢?

Mat imgToProcess = Highgui.imread(filename);
于 2012-12-11T16:27:00.507 回答