White paper on a white background not working

asked 2019-07-19 00:29:25 -0500

updated 2019-07-19 03:21:34 -0500

berak gravatar image

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<MatOfPoint> {

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<MatOfPoint>()
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) }

return contours
edit retag flag offensive close merge delete


try adaptiveThreshold instead of the standard one

Witek gravatar imageWitek ( 2019-07-19 03:48:37 -0500 )edit