How to crop particular rectangle

asked 2019-02-04 07:42:32 -0500

Hi,

I wanted to crop a segment on real time and process the image Tessaract Api any one help me?

/*This is my code */

List<matofpoint> squares = new LinkedList(); Mat frame = inputFrame.rgba().clone();

    if (Math.random()>0.80) {

        Mat smallerImg = new Mat(new Size(frame.width()/2, frame.height()/2),frame.type());

        Log.e(TAG,"Width : "+frame.width()/2);
        Log.e(TAG,"Width : "+frame.height()/2);
        Log.e(TAG,"Type : "+frame.type());

        Mat gray = new Mat(frame.size(),frame.type());

        Mat gray0 = new Mat(frame.size(), CvType.CV_8U);

        // down-scale and upscale the image to filter out the noise
        Imgproc.pyrDown(frame, smallerImg, smallerImg.size());
        Imgproc.pyrUp(smallerImg, frame, frame.size());

        for( int c = 0; c < 3; c++ )
        {
            List<Mat> sourceChannels = new ArrayList<Mat>();
            List<Mat> outChannel = new ArrayList<Mat>();

            Core.split(frame, sourceChannels);
            Log.e(TAG, " : "+frame);

            outChannel.add(new Mat(sourceChannels.get(0).size(),sourceChannels.get(0).type()));

            Log.e(TAG, " : "+sourceChannels.get(0).size());

            Core.mixChannels(sourceChannels, outChannel, new MatOfInt(c,0));

            Core.merge(outChannel, gray);

            for( int l = 1; l < N; l++ ) {
                Imgproc.threshold(gray, gray0, (l + 1) * 255 / N, 255, Imgproc.THRESH_BINARY);


                List<MatOfPoint> contours = new ArrayList<MatOfPoint>();

                // find contours and store them all as a list
                Imgproc.findContours(gray0, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);

                MatOfPoint approx = new MatOfPoint();

                for( int i = 0; i < contours.size(); i++ )
                {

                    approx = approxPolyDP((MatOfPoint) contours.get(i), Imgproc.arcLength(new MatOfPoint2f(((MatOfPoint) contours.get(i)).toArray()), true) * 0.02d, true);
                    if( approx.toArray().length == 4 &&
                            Math.abs(Imgproc.contourArea(approx)) > 1000 &&
                            Imgproc.isContourConvex(approx) )
                    {
                        Log.e(TAG,"Inside Four Vertices");
                        double maxCosine = 0;

                        for( int j = 2; j < 5; j++ )
                        {
                            // find the maximum cosine of the angle between joint edges
                            double cosine = Math.abs(angle(approx.toArray()[j%4], approx.toArray()[j-2], approx.toArray()[j-1]));
                            maxCosine = Math.max(maxCosine, cosine);
                        }

                        // if cosines of all angles are small
                        // (all angles are ~90 degree) then write quandrange
                        // vertices to resultant sequence
                        if( maxCosine < 0.3 )
                            squares.add(approx);
                    }
                }
            }
        }

    }

    Mat image = inputFrame.rgba();

    Imgproc.drawContours(image, squares, -1, new Scalar(0,0,255));

    Bitmap bitmap = Bitmap.createBitmap(image.width(), image.height(), Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(image, bitmap);
    try {
        OutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(new File(Environment.getExternalStorageDirectory().toString() + "/ocr.png")));
        bitmap.compress(Bitmap.CompressFormat.PNG, 0, bufferedOutputStream);
        bufferedOutputStream.close();
        Log.e("TAG","OCR.PNG Saved successfully...");
    } catch (IOException e) {
        e.printStackTrace();
    }
    return image;
edit retag flag offensive close merge delete