Ask Your Question
1

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

asked 2020-04-20 20:46:50 -0600

pivdet gravatar image

updated 2020-04-25 22:51:05 -0600

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.

edit retag flag offensive close merge delete

Comments

floatArrayOf(52f, 45f, 44f) -- if your Mat type is CV_8UC3 you need a byteArrayOf something, not float !

berak gravatar imageberak ( 2020-04-21 04:08:03 -0600 )edit

@berak Thanks for your reply, I've tried to change it to val data = byteArrayOf(52f.toByte(), 45f.toByte(), 44f.toByte()) , but got a vertical long image with different colors, not a square image.

pivdet gravatar imagepivdet ( 2020-04-25 23:05:14 -0600 )edit

Mat( 3, 1, CvType.CV_8UC3) -- that's a 3 rows, 1 column image, and rgbMat.put(3, 1,52.0, 45.0, 44.0) is out of bounds (you can only use (0,0), (1,0), (2,0) as coords)

berak gravatar imageberak ( 2020-04-26 03:11:57 -0600 )edit

@berak Awesome, I was wondering how to declare a small 1x1 Mat with red=52, green=45 and blue=44 ?

pivdet gravatar imagepivdet ( 2020-04-26 20:03:33 -0600 )edit

@berak Finally I make it by rgbMat.setTo(Scalar(52.0, 45.0, 44.0)) , awesome thanks for your help

pivdet gravatar imagepivdet ( 2020-04-26 21:14:32 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
1

answered 2020-04-21 03:43:46 -0600

mvuori gravatar image

Count your channels - you try to set 3 channel BGR data into RGBA (8888) bitmap. Convert your Mat to RGBA first.

edit flag offensive delete link more

Comments

Hi, I'm new to the openCV API, I've tried several ways, would you mind give me some example code?

pivdet gravatar imagepivdet ( 2020-04-21 05:20:37 -0600 )edit

https://stackoverflow.com/a/10266854/... Thanks, I just find what you mentioned cv::cvtColor(frame, continuousRGBA, CV_BGR2RGBA, 4);

pivdet gravatar imagepivdet ( 2020-04-25 20:30:54 -0600 )edit

Hi @mvuori, I've updated my question, but I still couldn't make it work.

pivdet gravatar imagepivdet ( 2020-04-25 22:51:43 -0600 )edit
0

answered 2020-04-27 01:45:58 -0600

pivdet gravatar image

Got answer right now:

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
}
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2020-04-20 20:46:50 -0600

Seen: 1,520 times

Last updated: Apr 27 '20