I use below code for detecting a page and show the rectangle on paper and its work perfectly while background is black but if the background is white and paper also white then not able to draw the rectangle on paper
private fun findContours(src: Mat): ArrayList {
val grayImage: Mat
val cannedImage: Mat
val kernel: Mat = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, Size(9.0, 9.0))
val dilate: Mat
val size = Size(src.size().width, src.size().height)
grayImage = Mat(size, CvType.CV_8UC4)
cannedImage = Mat(size, CvType.CV_8UC1)
dilate = Mat(size, CvType.CV_8UC1)
Imgproc.cvtColor(src, grayImage, Imgproc.COLOR_RGBA2GRAY)
Imgproc.GaussianBlur(grayImage, grayImage, Size(5.0, 5.0), 0.0)
Imgproc.threshold(grayImage, grayImage, 20.0, 255.0, Imgproc.THRESH_TRIANGLE)
Imgproc.Canny(grayImage, cannedImage, 75.0, 200.0)
Imgproc.dilate(cannedImage, dilate, kernel)
val contours = ArrayList()
val hierarchy = Mat()
// Imgproc.findContours(dilate, contours, hierarchy, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE)
Imgproc.findContours(dilate, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE)
contours.sortByDescending { p: MatOfPoint -> Imgproc.contourArea(p) }
hierarchy.release()
grayImage.release()
cannedImage.release()
kernel.release()
dilate.release()
return contours
}
↧