Convert Python Into Java / Kotlin using these operations
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.
- Youtube: https://www.youtube.com/watch?v=tKA_H...
- Ref Source code: https://github.com/misbah4064/backgro...
- My code : https://pastebin.com/W32sgbZx
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)
//fgmask[fgmask>0]=255
//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)
}
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
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. https://pastebin.com/W32sgbZx Really appreciate if someone could help this T.T
could you paste your code in your question. i can't access pastebin
OK updated!
i will look soon and try to provide a better python code that probably you can convert easily.
Alright! thanks bro. the CV provided methods are understandable. except the set pixels operation like eg.
@Fazz, look at
Imgproc.threshold()
for that@Fazz, i am a bit late.. did you solve your problem?