How to declare a small image for test in openCV Android?

孤人 提交于 2020-04-18 06:11:38

问题


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

I've did some research of image processing algorithm use Python Jupyter notebook, at this moment I want to verify it in Android 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)

The image of dimg_small would like:

Meanwhile the image of dimg_small_gamma would like:

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)

With the image show in my Android device:

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

Updated:

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
}

Unfortunately it crashed:

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)

来源:https://stackoverflow.com/questions/61226304/how-to-declare-a-small-image-for-test-in-opencv-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!