Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // OpenCV Kotlin calc differentiation and integration of an image using custom 1D kernels
- // note that the first row/col of image is NOT differentiated
- // -- this is on purpose to restore the original image
- // prepare for derivative and integration
- val image16 = Mat()
- image.convertTo(image16, CvType.CV_16SC3)
- image.release()
- // make derivative kernels
- val dxKernel = Mat(1, 2, CvType.CV_16SC1)
- val dxAnchor = Point(1.0, 0.0)
- dxKernel.put(0, 0, shortArrayOf(-1, 1))
- val dyKernel = Mat(2, 1, CvType.CV_16SC1)
- val dyAnchor = Point(0.0, 1.0)
- dyKernel.put(0, 0, shortArrayOf(-1, 1))
- // differentiate
- val dxImage = Mat()
- Imgproc.filter2D(image16, dxImage, -1, dxKernel, dxAnchor, 0.0, Core.BORDER_CONSTANT)
- val dyImage = Mat()
- Imgproc.filter2D(image16, dyImage, -1, dyKernel, dyAnchor, 0.0, Core.BORDER_CONSTANT)
- // do something with the dxyImage here
- // make integral kernels
- val dxSize = dxImage.size().width.toInt()
- val ixKernel = Mat(1, dxSize, CvType.CV_16SC1)
- val ixAnchor = Point(dxSize.minus(1).toDouble(), 0.0)
- ixKernel.put(0, 0, ShortArray(dxSize) {1})
- val dySize = dyImage.size().height.toInt()
- val iyKernel = Mat(dySize, 1, CvType.CV_16SC1)
- val iyAnchor = Point(0.0, dySize.minus(1).toDouble())
- iyKernel.put(0, 0, ShortArray(dySize) {1})
- // integrate
- val ixImage = Mat()
- Imgproc.filter2D(dxImage, ixImage, -1, ixKernel, ixAnchor, 0.0, Core.BORDER_CONSTANT)
- dxImage.release()
- val iyImage = Mat()
- Imgproc.filter2D(dyImage, iyImage, -1, iyKernel, iyAnchor, 0.0, Core.BORDER_CONSTANT)
- dyImage.release()
- // do something with the ixyImage here
- // release integral images
- ixImage.release()
- iyImage.release()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement