Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

opencv java How to sort contours from left to right

I want to sort contours from left to right.PLease help me on this.

Code: Mat image = Imgcodecs.imread("D:/OCR Images/letters/crop/Raju-NewOCR.png", Imgproc.COLOR_BGR2GRAY); image.size(); Mat resizedImage = new Mat();

    Size size = new Size(1024,600);

    Imgproc.resize( image, resizedImage, size);
    Imgcodecs.imwrite("D:/OCR Images/letters/crop/PAN6_resize.jpg",resizedImage);

    Mat imageHSV = new Mat(resizedImage.size(), CvType.CV_8UC4);        
    Mat imageBlurr = new Mat(image.size(), CvType.CV_8UC4);
    Mat imageA = new Mat(image.size(), CvType.CV_32F);

    Imgproc.cvtColor(image, imageHSV, Imgproc.COLOR_BGR2GRAY);
    Imgproc.GaussianBlur(imageHSV, imageBlurr, new Size(5,5), 0);
    Imgproc.adaptiveThreshold(imageBlurr, imageA, 255,Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY,7, 5);

    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();    
    Imgproc.findContours(imageA, contours, new Mat(), Imgproc.RETR_LIST,Imgproc.CHAIN_APPROX_SIMPLE);
    Imgproc.drawContours(imageBlurr, contours, 5, new Scalar(0,0,255));
    for(int i=0; i< contours.size();i++){
        /*if (Imgproc.contourArea(contours.get(i)) > 50 ){*/
        Rect rect = Imgproc.boundingRect(contours.get(i));

        if ((rect.width > 14 && rect.width < 31) && (rect.height > 27 && rect.height < 31)) {
            Imgcodecs.imwrite("D:/OCR Images/find/pan_" + i + ".jpg", new Mat(image, rect));

        }
            //if (rect.height < 50 && rect.height > 30 && rect.width > 250 && rect.width < 310){ //Kotak
            /*if (rect.height < 40 && rect.height > 20 && rect.width > 150 && rect.width < 200){ // HDFC
                Imgcodecs.imwrite("hdfcAccount.jpg",new Mat(image, rect));
            }*/
        //}
    }
click to hide/show revision 2
None

updated 2020-08-18 13:38:10 -0600

berak gravatar image

opencv java How to sort contours from left to right

I want to sort contours from left to right.PLease help me on this.

Code: Code:

Mat image = Imgcodecs.imread("D:/OCR Images/letters/crop/Raju-NewOCR.png", Imgproc.COLOR_BGR2GRAY);
        image.size();
        Mat resizedImage = new Mat();

Mat();

        Size size = new Size(1024,600);

     Imgproc.resize( image, resizedImage, size);
     Imgcodecs.imwrite("D:/OCR Images/letters/crop/PAN6_resize.jpg",resizedImage);

     Mat imageHSV = new Mat(resizedImage.size(), CvType.CV_8UC4);        
     Mat imageBlurr = new Mat(image.size(), CvType.CV_8UC4);
     Mat imageA = new Mat(image.size(), CvType.CV_32F);

     Imgproc.cvtColor(image, imageHSV, Imgproc.COLOR_BGR2GRAY);
     Imgproc.GaussianBlur(imageHSV, imageBlurr, new Size(5,5), 0);
     Imgproc.adaptiveThreshold(imageBlurr, imageA, 255,Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY,7, 5);

     List<MatOfPoint> contours = new ArrayList<MatOfPoint>();    
     Imgproc.findContours(imageA, contours, new Mat(), Imgproc.RETR_LIST,Imgproc.CHAIN_APPROX_SIMPLE);
     Imgproc.drawContours(imageBlurr, contours, 5, new Scalar(0,0,255));
     for(int i=0; i< contours.size();i++){
         /*if (Imgproc.contourArea(contours.get(i)) > 50 ){*/
         Rect rect = Imgproc.boundingRect(contours.get(i));

         if ((rect.width > 14 && rect.width < 31) && (rect.height > 27 && rect.height < 31)) {
             Imgcodecs.imwrite("D:/OCR Images/find/pan_" + i + ".jpg", new Mat(image, rect));

         }
             //if (rect.height < 50 && rect.height > 30 && rect.width > 250 && rect.width < 310){ //Kotak
             /*if (rect.height < 40 && rect.height > 20 && rect.width > 150 && rect.width < 200){ // HDFC
                 Imgcodecs.imwrite("hdfcAccount.jpg",new Mat(image, rect));
             }*/
         //}
     }

opencv java How to sort contours from left to right

I want to sort contours from left to right.PLease right.Please help me on this.

Code:

Mat image = Imgcodecs.imread("D:/OCR Images/letters/crop/Raju-NewOCR.png", Images/letters/crop/PAN.png", Imgproc.COLOR_BGR2GRAY);
        image.size();
        Mat resizedImage = new Mat();

        Size size = new Size(1024,600);

        Imgproc.resize( image, resizedImage, size);
        Imgcodecs.imwrite("D:/OCR Images/letters/crop/PAN6_resize.jpg",resizedImage);

        Mat imageHSV = new Mat(resizedImage.size(), CvType.CV_8UC4);        
        Mat imageBlurr = new Mat(image.size(), CvType.CV_8UC4);
        Mat imageA = new Mat(image.size(), CvType.CV_32F);

        Imgproc.cvtColor(image, imageHSV, Imgproc.COLOR_BGR2GRAY);
        Imgproc.GaussianBlur(imageHSV, imageBlurr, new Size(5,5), 0);
        Imgproc.adaptiveThreshold(imageBlurr, imageA, 255,Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY,7, 5);

        List<MatOfPoint> contours = new ArrayList<MatOfPoint>();    
        Imgproc.findContours(imageA, contours, new Mat(), Imgproc.RETR_LIST,Imgproc.CHAIN_APPROX_SIMPLE);
        Imgproc.drawContours(imageBlurr, contours, 5, new Scalar(0,0,255));
        for(int i=0; i< contours.size();i++){
            /*if (Imgproc.contourArea(contours.get(i)) > 50 ){*/
             Rect rect = Imgproc.boundingRect(contours.get(i));

            if ((rect.width > 14 && rect.width < 31) && (rect.height > 27 && rect.height < 31)) {
                Imgcodecs.imwrite("D:/OCR Images/find/pan_" + i + ".jpg", new Mat(image, rect));

            }
                //if (rect.height < 50 && rect.height > 30 && rect.width > 250 && rect.width < 310){ //Kotak
                /*if (rect.height < 40 && rect.height > 20 && rect.width > 150 && rect.width < 200){ // HDFC
                    Imgcodecs.imwrite("hdfcAccount.jpg",new Mat(image, rect));
                }*/
            //}
        }