4

I am trying to find the bounding boxes of text in an image. I've found a c++ implementation here.

Now I've tried to convert it to a Java code like this:

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

import org.opencv.core.*;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.*;

public class Scritte {
    public static void main(String[] args) {

    // Load the native library.
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        new Nuova().analizza();
    }
}

class Nuova{
    public void analizza(){

        Mat Main = Highgui.imread("/.../Ole.png");
        Mat rgb = new Mat();

        Imgproc.pyrDown(Main, rgb);

        Mat small = new Mat();

        Imgproc.cvtColor(rgb, small, Imgproc.COLOR_RGB2GRAY);

        Mat grad = new Mat();

        Mat morphKernel = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(3,3));

        Imgproc.morphologyEx(small, grad, Imgproc.MORPH_GRADIENT , morphKernel);

        Mat bw = new Mat();

        Imgproc.threshold(grad, bw, 0.0, 255.0, Imgproc.THRESH_BINARY | Imgproc.THRESH_OTSU);

        Mat connected = new Mat();

        morphKernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(1,9));

        Imgproc.morphologyEx(bw, connected, Imgproc.MORPH_CLOSE  , morphKernel);


        Mat mask = Mat.zeros(bw.size(), CvType.CV_8UC1);

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

        Mat hierarchy = new Mat();

        Imgproc.findContours(connected, contours, hierarchy, Imgproc.RETR_CCOMP, Imgproc.CHAIN_APPROX_SIMPLE, new Point(0, 0));

        for(int idx = 0; idx < contours.size(); idx++)
        {
            Rect rect = Imgproc.boundingRect(contours.get(idx));

            Mat maskROI = new Mat(mask, rect);

            Imgproc.drawContours(mask, contours, idx, new Scalar(255, 255, 255), Core.FILLED);

            double r = (double)Core.countNonZero(maskROI)/(rect.width*rect.height);

            if (r > .45 && (rect.height > 8 && rect.width > 8))
            {
                Core.rectangle(rgb, rect.br() , new Point( rect.br().x-rect.width ,rect.br().y-rect.height),  new Scalar(0, 255, 0));
            }

            String outputfile = "trovato.png";
            Highgui.imwrite(outputfile,rgb);
       }        
    }
}

I think the error is in the FOR cycle but I'm not able to resolve it. Can anyone help me?

(Below I post the result with the method in C++ and Java)

With C++: cpp

With Java: java

Community
  • 1
  • 1
Fifth
  • 49
  • 1
  • 3

1 Answers1

3

Below are the two major differences I see in your Java code compared to the C++ implementation.

  1. Your morphological kernel is 9 x 1 (h x w), whereas in the original code it's 1 x 9. So change it from

    morphKernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(1,9));
    

to

morphKernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(9, 1));
  1. You are not setting the maskROI to zero.

    Mat maskROI = new Mat(mask, rect);
    

Add the following line of code before calling drawContours.

maskROI.setTo(new Scalar(0, 0, 0));
dhanushka
  • 10,492
  • 2
  • 37
  • 47