Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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.

diff=cv2.subtract(img1, img2) diff[abs(diff)<30]=0 gray = cv2.cvtColor(diff.astype(np.uint8), cv2.COLOR_BGR2GRAY) gray[np.abs(gray) < 10] = 0 mask_img = gray.astype(np.uint8) mask_img[mask_img>0]=255 // invert the mask mask_img_inv = cv2.bitwise_not(mask_img) finalImg = cv2.bitwise_and(img1, img1, mask = mask_img)

click to hide/show revision 2
None

updated 2020-10-17 02:26:22 -0600

berak gravatar image

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.

diff=cv2.subtract(img1, img2)
diff[abs(diff)<30]=0
gray = cv2.cvtColor(diff.astype(np.uint8), cv2.COLOR_BGR2GRAY)
gray[np.abs(gray) < 10] = 0
mask_img = gray.astype(np.uint8)
mask_img[mask_img>0]=255 // invert the mask
mask_img_inv = cv2.bitwise_not(mask_img)
finalImg = cv2.bitwise_and(img1, img1, mask = mask_img)mask_img)

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.

diff=cv2.subtract(img1, img2)
diff[abs(diff)<30]=0
gray = cv2.cvtColor(diff.astype(np.uint8), cv2.COLOR_BGR2GRAY)
gray[np.abs(gray) < 10] = 0
mask_img = gray.astype(np.uint8)
mask_img[mask_img>0]=255 // invert the mask
mask_img_inv = cv2.bitwise_not(mask_img)
finalImg = cv2.bitwise_and(img1, img1, mask = mask_img)

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_Ht4P1Gw
  • Ref Source code: https://github.com/misbah4064/backgroundRemoval/blob/d285d90c1f/backgroundRemoval_video.py
  • My code : https://pastebin.com/W32sgbZx

    diff=cv2.subtract(img1, img2) diff[abs(diff)<30]=0 gray = cv2.cvtColor(diff.astype(np.uint8), cv2.COLOR_BGR2GRAY) gray[np.abs(gray) < 10] = 0 mask_img = gray.astype(np.uint8) mask_img[mask_img>0]=255 // invert the mask mask_img_inv = cv2.bitwise_not(mask_img) finalImg = cv2.bitwise_and(img1, img1, mask = mask_img)

mask_img)

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_Ht4P1Gw
  • Ref Source code: https://github.com/misbah4064/backgroundRemoval/blob/d285d90c1f/backgroundRemoval_video.py
  • My code : https://pastebin.com/W32sgbZx

diff=cv2.subtract(img1, img2) diff[abs(diff)<30]=0 img2)

diff[abs(diff)<30]=0

gray = cv2.cvtColor(diff.astype(np.uint8), cv2.COLOR_BGR2GRAY) cv2.COLOR_BGR2GRAY)

gray[np.abs(gray) < 10] = 0 0

mask_img = gray.astype(np.uint8) gray.astype(np.uint8)

mask_img[mask_img>0]=255 // invert the mask mask

mask_img_inv = cv2.bitwise_not(mask_img) cv2.bitwise_not(mask_img)

finalImg = cv2.bitwise_and(img1, img1, mask = mask_img)

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_Ht4P1Gw
  • Ref Source code: https://github.com/misbah4064/backgroundRemoval/blob/d285d90c1f/backgroundRemoval_video.py
  • My code : https://pastebin.com/W32sgbZx

my code so far:

diff=cv2.subtract(img1, img2)

diff[abs(diff)<30]=0

gray = cv2.cvtColor(diff.astype(np.uint8), cv2.COLOR_BGR2GRAY)

gray[np.abs(gray) 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] = 00 //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) }

mask_img = gray.astype(np.uint8)

mask_img[mask_img>0]=255 // invert the mask

mask_img_inv = cv2.bitwise_not(mask_img)

finalImg = cv2.bitwise_and(img1, img1, mask = mask_img)