如何在OpenCV Android中声明要测试的小图像? [英] How to declare a small image for test in openCV Android?

查看:111
本文介绍了如何在OpenCV Android中声明要测试的小图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该文档很小或不存在,我已阅读 OpenCV的官方文档从stackoverflow声明OpenCV Java中的Mat

The document is minimal or non existing, I've read OpenCV's official doc and declare Mat in OpenCV java from stackoverflow

我已经使用Python Jupyter笔记本对图像处理算法进行了一些研究,目前我想在Android python中对其进行验证.

I've did some research of image processing algorithm use Python Jupyter notebook, at this moment I want to verify it in Android python.

对于Python部分:

For the Python part:

from numpy import asarray

dimg = cv2.imread('story/IMG_0371.PNG')
dimg_small = dimg[571:572, 401:402]
dimg_small_gamma = adjust_gamma(dimg_small)
data = asarray(dimg_small)
data_gamma = asarray(dimg_small_gamma)

# print data
array([[[52, 45, 44]]], dtype=uint8)
# print data_gamma
array([[[115, 107, 105]]], dtype=uint8)

dimg_small的图像需要:

与此同时,dimg_small_gamma的图像需要:

Meanwhile the image of dimg_small_gamma would like:

到目前为止,我想在Android中创建具有相同数据[52, 45, 44]115, 107, 105的图像,以验证我的算法,无论Java还是Kotlin都可以.

So far, I want to create an image with the same data [52, 45, 44] and 115, 107, 105 in Android to verify my algorithm, both Java or Kotlin would be fine.

private fun genTestMat(): Mat { //upper one
    val img =  Mat( 3, 1, CvType.CV_8UC3)
    img.put(3, 1,52.0, 45.0, 44.0)
    return img
}

private fun genTestMat2(): Mat { //under one
    val img =  Mat( 3, 1, CvType.CV_8UC3)
    img.put(3, 1,115.0, 107.0, 105.0)
    return img
}

private fun opencvMatToBitmap(mat: Mat): Bitmap {
    val bitmap: Bitmap = Bitmap.createBitmap(mat.width(), mat.height(), Bitmap.Config.ARGB_8888)
    Utils.matToBitmap(mat, bitmap)
    return bitmap
}

...

val testImg = opencvMatToBitmap(genTestMat())
val testImg2 = opencvMatToBitmap(genTestMat2())

image_under.setImageBitmap(testImg2)
image_upper.setImageBitmap(testImg)

在我的Android设备上显示图片:

With the image show in my Android device:

我想知道如何使Android显示与Python Jupyter笔记本相同的图像?

I was wondering how to make the Android show the same image as is in Python Jupyter notebook?

已更新:

使用数组尝试使用Mat.put,如下所示:

Tried to Mat.put using array as follows:

private fun genTestMat(): Mat { //upper one
    val img =  Mat( 3, 1, CvType.CV_8UC3)
    val data = floatArrayOf(52f, 45f, 44f)
    img.put(1,1, data)
    return img
}

不幸的是它崩溃了:

Caused by: java.lang.UnsupportedOperationException: Mat data type is not compatible: 16
        at org.opencv.core.Mat.put(Mat.java:801)
        at com.example.cap2hist.MainActivity.genTestMat(MainActivity.kt:131)

推荐答案

在OpenCV中与人们聊天后论坛,得到了答案.

After chat with folks in openCV forum, got the answer.

private fun genTestMat(): Mat {
    val inputMat = Mat( 1, 1, CvType.CV_8UC3)
    val rgbMat = Mat()
    Imgproc.cvtColor(inputMat, rgbMat, Imgproc.COLOR_BGR2RGBA, 3)
    rgbMat.setTo(Scalar(52.0, 45.0, 44.0))
    return rgbMat
}

这篇关于如何在OpenCV Android中声明要测试的小图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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