适用于Android的OpenCV:将图像转换为灰度的简单示例 [英] OpenCV for Android: Simple example to convert Image to Greyscale

查看:75
本文介绍了适用于Android的OpenCV:将图像转换为灰度的简单示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我只想通过OpenCV将位图转换为灰度.我拥有了一切 运行,但是一旦我想将图像转换为灰度,它就会崩溃得很厉害. 有人可以帮忙吗?我希望这些摘要足够,如果没有,我可以附加其余的内容.

As a starter I just want to convert an Bitmap to greyscale via OpenCV. I have everything running, but it crashes hard as soon as i want to convert the image to greyscale. Can anyone help out? I hope the snippets are enough, if not i can attach the rest.

java文件的一部分:

part of the java file:

                // convert to opencv structure
                Mat image = new Mat();
                Mat grayimage = new Mat();
                Utils.bitmapToMat(b2, image);

                // call opencv for processing
                grayimage = convertToGray (image);

                // convert back
                Utils.matToBitmap(grayimage, b2);

JNI cpp文件:

JNI cpp file:

JNIEXPORT jlong JNICALL Java_com_my_sample_MainMenuActivity_convertToGray (JNIEnv*, jobject, jlong addrRGBA)
{
    LOGI("Converting to Gray.");
    Mat *mRGBA  = (Mat*)addrRGBA;

    Mat *_retval_;
    cvtColor(*mRGBA, *_retval_, CV_RGB2GRAY);

    LOGI("Successfully finished Converting to Gray.");
    return (jlong) _retval_;
}

转换图像后再也无法成功登录. 好像位图没有正确转换为Mat. 位图本身确实存在,我可以在ImageView上显示它. 有人知道我(显然)在做什么错吗?

It never gets to it logging successful having converted the image. Seems as if the Bitmap was not properly converted to a Mat. The Bitmap itself does exist, I can show it on a ImageView. Anyone a clue what I'm (obviously) doing wrong?

05-14 21:26:27.082: I/native(22394): Converting to Gray.
05-14 21:26:27.082: A/libc(22394): Fatal signal 11 (SIGSEGV) at 0xcd10001d (code=1), thread 22394 (ialabs.mysample)

很抱歉,如果这个问题在其他地方得到回答,我还没有找到Mat的例子, 似乎只有一些与IplImage有关.

Sorry if this question is answered elsewhere, i haven't found an example with Mat, just some with IplImage, it seems.

推荐答案

您有两个空的Mat对象.您无法将空的内容转换为灰色.

You have two empty Mat objects. You can't convert something that's empty into gray.

尝试一下:

Mat tmp = new Mat (b.getWidth(), b.getHeight(), CvType.CV_8UC1);
Utils.bitmapToMat(b, tmp);
Imgproc.cvtColor(tmp, tmp, Imgproc.COLOR_RGB2GRAY);

bBitmap的图片.

Utils.matToBitmap(tmp, b);

添加此项以将Mat对象转换回Bitmap.

Add this to convert the Mat object back to a Bitmap.

这篇关于适用于Android的OpenCV:将图像转换为灰度的简单示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆