Advertisement
yclee126

OpenCv Kotlin diff. and int. using 1D kernel

Jun 6th, 2022
1,264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.64 KB | None | 0 0
  1. // OpenCV Kotlin calc differentiation and integration of an image using custom 1D kernels
  2. // note that the first row/col of image is NOT differentiated
  3. // -- this is on purpose to restore the original image
  4.  
  5. // prepare for derivative and integration
  6. val image16 = Mat()
  7. image.convertTo(image16, CvType.CV_16SC3)
  8. image.release()
  9.  
  10. // make derivative kernels
  11. val dxKernel = Mat(1, 2, CvType.CV_16SC1)
  12. val dxAnchor = Point(1.0, 0.0)
  13. dxKernel.put(0, 0, shortArrayOf(-1, 1))
  14.  
  15. val dyKernel = Mat(2, 1, CvType.CV_16SC1)
  16. val dyAnchor = Point(0.0, 1.0)
  17. dyKernel.put(0, 0, shortArrayOf(-1, 1))
  18.  
  19. // differentiate
  20. val dxImage = Mat()
  21. Imgproc.filter2D(image16, dxImage,  -1, dxKernel, dxAnchor, 0.0, Core.BORDER_CONSTANT)
  22.  
  23. val dyImage = Mat()
  24. Imgproc.filter2D(image16, dyImage, -1, dyKernel, dyAnchor, 0.0, Core.BORDER_CONSTANT)
  25.  
  26. // do something with the dxyImage here
  27.  
  28.  
  29. // make integral kernels
  30. val dxSize = dxImage.size().width.toInt()
  31. val ixKernel = Mat(1, dxSize, CvType.CV_16SC1)
  32. val ixAnchor = Point(dxSize.minus(1).toDouble(), 0.0)
  33. ixKernel.put(0, 0, ShortArray(dxSize) {1})
  34.  
  35. val dySize = dyImage.size().height.toInt()
  36. val iyKernel = Mat(dySize, 1, CvType.CV_16SC1)
  37. val iyAnchor = Point(0.0, dySize.minus(1).toDouble())
  38. iyKernel.put(0, 0, ShortArray(dySize) {1})
  39.  
  40. // integrate
  41. val ixImage = Mat()
  42. Imgproc.filter2D(dxImage, ixImage, -1, ixKernel, ixAnchor, 0.0, Core.BORDER_CONSTANT)
  43. dxImage.release()
  44.  
  45. val iyImage = Mat()
  46. Imgproc.filter2D(dyImage, iyImage, -1, iyKernel, iyAnchor, 0.0, Core.BORDER_CONSTANT)
  47. dyImage.release()
  48.  
  49. // do something with the ixyImage here
  50.  
  51.  
  52. // release integral images
  53. ixImage.release()
  54. iyImage.release()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement