public RectangleLocationResult locateRectangles(Mat grayImage){
  Mat gray=grayImage.clone();
  Filter.downsample(gray,2);
  Filter.upsample(gray,2);
  Mat cacheHierarchy=new Mat();
  Mat grayTemp=new Mat();
  List<Rectangle> rectangles=new ArrayList<>();
  List<Contour> contours=new ArrayList<>();
  Imgproc.Canny(gray,grayTemp,0,THRESHOLD_CANNY,APERTURE_CANNY,true);
  Filter.dilate(gray,2);
  List<MatOfPoint> contoursTemp=new ArrayList<>();
  Imgproc.findContours(grayTemp,contoursTemp,cacheHierarchy,Imgproc.CV_RETR_LIST,Imgproc.CHAIN_APPROX_SIMPLE);
  MatOfPoint2f approx=new MatOfPoint2f();
  for (  MatOfPoint co : contoursTemp) {
    MatOfPoint2f matOfPoint2f=new MatOfPoint2f(co.toArray());
    Contour c=new Contour(co);
    Imgproc.approxPolyDP(matOfPoint2f,approx,c.arcLength(true) * EPLISON_APPROX_TOLERANCE_FACTOR,true);
    Contour approxContour=new Contour(approx);
    if (approx.toArray().length == 4 && Math.abs(approxContour.area()) > 1000 && approxContour.isClosed()) {
      contours.add(approxContour);
      double maxCosine=0;
      for (int j=2; j < 5; j++) {
        double cosine=Math.abs(MathUtil.angle(approx.toArray()[j % 4],approx.toArray()[j - 2],approx.toArray()[j - 1]));
        maxCosine=Math.max(maxCosine,cosine);
      }
      if (maxCosine < MAX_COSINE_VALUE) {
        rectangles.add(new Rectangle(approx.toArray()));
      }
    }
  }
  return new RectangleLocationResult(contours,rectangles);
}

public RectangleLocationResult locateRectangles ( Mat grayImage ) { Mat gray = grayImage . clone ( ) ; Filter . downsample ( gray , 2 ) ; Filter . upsample ( gray , 2 ) ; Mat cacheHierarchy = new Mat ( ) ; Mat grayTemp = new Mat ( ) ; List < Rectangle > rectangles = new ArrayList < > ( ) ; List < Contour > contours = new ArrayList < > ( ) ; Imgproc . Canny ( gray , grayTemp , 0 , THRESHOLD_CANNY , APERTURE_CANNY , true ) ; Filter . dilate ( gray , 2 ) ; List < MatOfPoint > contoursTemp = new ArrayList < > ( ) ; Imgproc . findContours ( grayTemp , contoursTemp , cacheHierarchy , Imgproc . CV_RETR_LIST , Imgproc . CHAIN_APPROX_SIMPLE ) ; MatOfPoint2f approx = new MatOfPoint2f ( ) ; for ( MatOfPoint co : contoursTemp ) { MatOfPoint2f matOfPoint2f = new MatOfPoint2f ( co . toArray ( ) ) ; Contour c = new Contour ( co ) ; Imgproc . approxPolyDP ( matOfPoint2f , approx , c . arcLength ( true ) * EPLISON_APPROX_TOLERANCE_FACTOR , true ) ; Contour approxContour = new Contour ( approx ) ; if ( approx . toArray ( ) . length == 4 && Math . abs ( approxContour . area ( ) ) > 1000 && approxContour . isClosed ( ) ) { contours . add ( approxContour ) ; double maxCosine = 0 ; for ( int j = 2 ; j < 5 ; j ++ ) { double cosine = Math . abs ( MathUtil . angle ( approx . toArray ( ) [ j % 4 ] , approx . toArray ( ) [ j - 2 ] , approx . toArray ( ) [ j - 1 ] ) ) ; maxCosine = Math . max ( maxCosine , cosine ) ; } if ( maxCosine < MAX_COSINE_VALUE ) { rectangles . add ( new Rectangle ( approx . toArray ( ) ) ) ; } } } return new RectangleLocationResult ( contours , rectangles ) ; }
locate rectangles in an image
