Skip to content

Instantly share code, notes, and snippets.

@sminogue
Created April 13, 2017 13:44
Show Gist options
  • Save sminogue/473911bc6efca0f89a9f54f762b00b09 to your computer and use it in GitHub Desktop.
Save sminogue/473911bc6efca0f89a9f54f762b00b09 to your computer and use it in GitHub Desktop.
//Detect 4 sided black polygon
ConfigPolygonDetector config = new ConfigPolygonDetector(4, 4);
BinaryPolygonDetector<GrayU8> detector = FactoryShapeDetector.polygon(config, GrayU8.class);
int threshold = GThresholdImageOps.computeOtsu(invert, 0, 255);
GrayU8 binary = new GrayU8(invert.width, invert.height);
ThresholdImageOps.threshold(invert, binary, threshold, true);
detector.process(invert, binary);
FastQueue<Polygon2D_F64> found = detector.getFoundPolygons();
//Go through the found polygons and take the polygon with the largest area.
//This SHOULD be the page since this should be a picture of a page you are scanning
Polygon2D_F64 polygon = null;
double polygonSize = 0;
for (int i = 0; i < found.size; i++) {
Polygon2D_F64 poly = found.get(i);
double size = poly.areaSimple();
if (size > polygonSize) {
polygonSize = size;
polygon = poly;
}
}
//If unable to identify page... Bail.
if (polygon == null) {
throw new Exception("Unable to identify page in image");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment