Convert Python Into Java / Kotlin using these operations

asked 2020-10-16 22:28:33 -0500

Fazz gravatar image

updated 2020-10-17 10:57:39 -0500

I'm totally new in OpenCV. Does anyone knows how to convert below Python into Java/Kotlin? Only these a bit tricky to translate it.

my code so far:

    fun resize(bgMat: Mat, imgMat: Mat): Mat{
        val resizeimage = Mat()
        val sz = org.opencv.core.Size(imgMat.width().toDouble(), imgMat.height().toDouble())
        resize(bgMat, resizeimage, sz,0.0 ,0.0, INTER_AREA)
        return resizeimage

    var refMat = Mat()
    fun removeTheSelectedColorBackground(imgBmp: Bitmap, bgBmp: Bitmap) : Bitmap {
        val imgMat = Mat(imgBmp.getWidth(), imgBmp.getHeight(), CvType.CV_8UC1)
        Utils.bitmapToMat(imgBmp, imgMat)
        val bgMat = Mat(bgBmp.getWidth(), bgBmp.getHeight(), CvType.CV_8UC1)
        Utils.bitmapToMat(bgBmp, bgMat)

        if (isFirstTime!!) {
            isFirstTime = false
            refMat = Mat(imgBmp.getWidth(), imgBmp.getHeight(), CvType.CV_8UC1)
            Utils.bitmapToMat(imgBmp, refMat)
        val updatedBgMat = resize(bgMat, refMat)
        val maskDiff1 = Mat()
        val maskDiff2 = Mat()
        subtract(imgMat, refMat, maskDiff1)
        subtract(refMat, imgMat, maskDiff2)
        val maskDiff = Mat()
        add(maskDiff1, maskDiff2, maskDiff)
        //        diff[abs(diff)<13.0]=0
        val grayMat = Mat()
        cvtColor(maskDiff, grayMat, COLOR_BGR2GRAY)

    //gray[np.abs(gray) < 10] = 0
        //fgmask = gray.astype(np.uint8)
        //invert the mask
        //fgmask_inv = cv2.bitwise_not(fgmask)
        //use the masks to extract the relevant parts from FG and BG
        //fgimg = cv2.bitwise_and(img,img,mask = fgmask)
        //bgimg = cv2.bitwise_and(bg,bg,mask = fgmask_inv)
        //combine both the BG and the FG images
        //dst = cv2.add(bgimg,fgimg)
edit retag flag offensive close merge delete



where did you get the python code? are you sure it works well ? converting from python to java seems easy but i did not understand what do you want to achieve with this code. please provide img1 and img2

sturkmen gravatar imagesturkmen ( 2020-10-17 06:19:08 -0500 )edit

The objective is to remove the background in a video. These codes removing the background except a subject in the video (foreground). Then the other video running in the background. (background video).

It does work in python. I got the full source code from link below.

I managed to translate a few parts but only above method & APIs are a bit confusing. Written in Kotlin you can refer here. Really appreciate if someone could help this T.T

Fazz gravatar imageFazz ( 2020-10-17 10:33:48 -0500 )edit

could you paste your code in your question. i can't access pastebin

sturkmen gravatar imagesturkmen ( 2020-10-17 10:37:35 -0500 )edit

OK updated!

Fazz gravatar imageFazz ( 2020-10-17 10:44:34 -0500 )edit

i will look soon and try to provide a better python code that probably you can convert easily.

sturkmen gravatar imagesturkmen ( 2020-10-17 11:04:49 -0500 )edit

Alright! thanks bro. the CV provided methods are understandable. except the set pixels operation like eg.

  • diff[abs(diff)<13.0]=0
  • gray[np.abs(gray) < 10] = 0
  • fgmask[fgmask>0]=255
Fazz gravatar imageFazz ( 2020-10-18 05:02:56 -0500 )edit

@Fazz, look at Imgproc.threshold() for that

berak gravatar imageberak ( 2020-10-18 11:42:13 -0500 )edit

@Fazz, i am a bit late.. did you solve your problem?

sturkmen gravatar imagesturkmen ( 2020-10-20 12:11:12 -0500 )edit