I am making document scanner. It is detecting only edges and not corners. [closed]

asked 2017-11-21 03:47:53 -0600

Ajay Gohel gravatar image

I am making Document scanner. It have both option for Capture and Import from gallery.I am using AsynkTask for processing image. Image process flow- Image>Resize>Apply k-means with 2 cluster center>Identify cluster and binarize image>find contours and pick largest one>detect lines and corners>Transform> Result image.

Input Image

Image Result

It is detecting edges and displaying only white edges with black background. I want cropped color image. Edge is detecting and largest contour is also detected , but cannot sort corners properly.

getPage Fucntion

private void getPage() {
     new AsyncTask<Void, Void, Bitmap>() {
        ProgressDialog dialog;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            dialog = ProgressDialog.show(MainActivity.this, "Processing Image", "please wait...");
        }

        @Override
        protected Bitmap doInBackground(Void... params) {
            Mat srcRes = new Mat(src.size(), src.type());
            Mat srcGray = new Mat();

            //Preparing image to perform k-means algo
            Mat samples = new Mat(src.rows() * src.cols(), 3, CvType.CV_32F);
            for (int y = 0; y < src.rows(); y++) {
                for (int x = 0; x < src.cols(); x++) {
                    for (int z = 0; z < 3; z++) {
                        samples.put(x + y * src.cols(), z, src.get(y, x)[z]);
                    }
                }
            }

            //k-means algorithm
            int clusterCount = 2;
            Mat labels = new Mat();
            int attempts = 5;
            Mat centers = new Mat();

            Core.kmeans(samples, clusterCount, labels, new TermCriteria(TermCriteria.MAX_ITER | TermCriteria.EPS, 10000, 0.0001), attempts, Core.KMEANS_PP_CENTERS, centers);
            double dstCenter0 = calcWhiteDist(centers.get(0, 0)[0], centers.get(0, 1)[0], centers.get(0, 2)[0]);
            double dstCenter1 = calcWhiteDist(centers.get(1, 0)[0], centers.get(1, 1)[0], centers.get(1, 2)[0]);

            int paperCluster = (dstCenter0 < dstCenter1) ? 0 : 1;
            for (int y = 0; y < src.rows(); y++) {
                for (int x = 0; x < src.cols(); x++) {
                    int cluster_idx = (int) labels.get(x + y * src.cols(), 0)[0];
                    if (cluster_idx != paperCluster) {
                        srcRes.put(y, x, 0, 0, 0, 255);
                    } else {
                        srcRes.put(y, x, 255, 255, 255, 255);
                    }
                }
            }
            Imgproc.cvtColor(src, srcGray, Imgproc.COLOR_BGR2GRAY);
            Imgproc.Canny(srcGray, srcGray, 50, 150);
            List<MatOfPoint> contours = new ArrayList<>();
            Mat hierarchy = new Mat();

         //Find ontours

                   Imgproc.findContours(srcGray, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE);

            int index = 0;
            double maxim = Imgproc.contourArea(contours.get(0));

            for (int contourIdx = 1; contourIdx < contours.size(); contourIdx++) {
                double temp;
                temp = Imgproc.contourArea(contours.get(contourIdx));
                if (maxim < temp) {
                    maxim = temp;
                    index = contourIdx;
                }
            }

            Mat drawing = Mat.zeros(srcRes.size(), CvType.CV_8UC1);
            Imgproc.drawContours(drawing, contours, index, new Scalar(255), 1);

            Mat lines = new Mat();
            Imgproc.HoughLinesP(drawing, lines, 1, Math.PI / 180, 70, 30, 10);

            ArrayList<Point> corners = new ArrayList<>();
            for (int i = 0; i < lines.cols(); i++) {
                for (int j = i + 1; j < lines.cols(); j++) {
                    double[] line1 = lines.get(0, i);
                    double[] line2 = lines.get(0, j);

                    Point pt = findIntersection(line1, line2);
                    if (pt.x >= 0 && pt.y >= 0 && pt.x <= drawing.cols() && pt.y <= drawing.rows()) {
                        if (!exists(corners, pt)) {
                            corners.add(pt);
                        }
                    }
                }
            }
            if (corners.size() != 4) {
                errorMessage = "Cannot detect perfect corners";
                Bitmap bitmap = Bitmap.createBitmap(drawing.cols(), drawing.rows(), Bitmap.Config.ARGB_8888);
                Utils.matToBitmap(drawing, bitmap);

                return bitmap;
            }

            sortCorners(corners ...
(more)
edit retag flag offensive reopen merge delete

Closed for the following reason question is not relevant or outdated by sturkmen
close date 2020-12-13 10:44:08.283047

Comments

can you please post your full activity code i have already answer of your question.because i am also working on document scanner project.

what is scaleFactor variable?

Akhil Patel gravatar imageAkhil Patel ( 2019-01-04 23:52:17 -0600 )edit

whats is srcOrig?

Akhil Patel gravatar imageAkhil Patel ( 2019-01-05 00:14:18 -0600 )edit

have got answer i am also on same project

Jayapriya gravatar imageJayapriya ( 2019-06-10 00:20:14 -0600 )edit