Java OpenCV - Rectangle Detection with Hough Transform
<p>I'm developing a program to detect rectangular shape and draw bounding box to the detected area.</p>

<p>For edge detection, I used Canny Edge Detection.
Then, I use Hough Transform to extract lines.</p>

<p>This is the original image
<a href="http://i.stack.imgur.com/tWSxY.jpg" rel="noreferrer">enter image description here</a></p>

<p>This is the result image
<a href="http://i.stack.imgur.com/Hq8fw.jpg" rel="noreferrer">enter image description here</a></p>

<p>My problem is that I can't draw a bounding box to the detected area.
It seems that my program can only detect a single horizontal line.
How can I detect rectangle shape and draw rectangle line to the detected shape?</p>

<p>I've read similar problems and it is required to find the 4 corner points of the rectangle, check if the point is 90 degree, and find the intersection. I'm really confused how to code it in Java opencv. Other methods to detect the shape and draw bounding box to the detected would be okay too.</p>

<p>Here's the code</p>

<pre><code>import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgcodecs.*;
import org.opencv.imgproc.Imgproc;

public class HoughTransformCV2 {

    public static void main(String[] args) {
        try {
            System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
            Mat source = Imgcodecs.imread("rectangle.jpg", Imgcodecs.CV_LOAD_IMAGE_ANYCOLOR);
            Mat destination = new Mat(source.rows(), source.cols(), source.type());

            Imgproc.cvtColor(source, destination, Imgproc.COLOR_RGB2GRAY);
            Imgproc.equalizeHist(destination, destination);
            Imgproc.GaussianBlur(destination, destination, new Size(5, 5), 0, 0, Core.BORDER_DEFAULT);

            Imgproc.Canny(destination, destination, 50, 100);
            //Imgproc.adaptiveThreshold(destination, destination, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 15, 40);
            Imgproc.threshold(destination, destination, 0, 255, Imgproc.THRESH_BINARY);

            if (destination != null) {
                Mat lines = new Mat();
                Imgproc.HoughLinesP(destination, lines, 1, Math.PI / 180, 50, 30, 10);
                Mat houghLines = new Mat();
                houghLines.create(destination.rows(), destination.cols(), CvType.CV_8UC1);

                //Drawing lines on the image
                for (int i = 0; i &lt; lines.cols(); i++) {
                    double[] points = lines.get(0, i);
                    double x1, y1, x2, y2;
                    x1 = points[0];
                    y1 = points[1];
                    x2 = points[2];
                    y2 = points[3];

                    Point pt1 = new Point(x1, y1);
                    Point pt2 = new Point(x2, y2);

                    //Drawing lines on an image
                    Imgproc.line(source, pt1, pt2, new Scalar(0, 0, 255), 4);
                }

            }

            Imgcodecs.imwrite("rectangle_houghtransform.jpg", source);

        } catch (Exception e) {
            System.out.println("error: " + e.getMessage());
        }
    }
}
</code></pre>

<p>Any help in Java would be appreciated :)
Thank you very much!</p>