Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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

I've asked the same question in SO but got no response, I was wondering if someone could do me a favor here.

Since 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:

image description

Meanwhile the image of dimg_small_gamma would like: image description

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: image description

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)

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

I've asked the same question in SO but got no response, I was wondering if someone could do me a favor here.

Since 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:

image description

Meanwhile the image of dimg_small_gamma would like: image description

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: image description

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)

Updated 2:

Tried to convert Mat to RGBA first as @mvuori suggested:

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

    val rgbMat = Mat()
    Imgproc.cvtColor(inputMat, rgbMat, Imgproc.COLOR_BGR2RGBA, 3)

    return rgbMat
}

Or :

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

But both of it got a vertical long image with different color, but not a square image.