Skip to content

Instantly share code, notes, and snippets.

View sminogue's full-sized avatar

Seamus Minogue sminogue

View GitHub Profile
@sminogue
sminogue / gist:b20bc6c270e5784d781d3f719c81dec5
Last active August 14, 2018 23:47
using jsfeat find lines in image using canny and hough
var canvas = document.createElement('canvas');
var context2d = canvas.getContext('2d');
context2d.drawImage(img, 0, 0, width, height);
var imageData = context2d.getImageData(0, 0, pageMeta.width, pageMeta.height);
var img_u8 = new jsfeat.matrix_t(pageMeta.width, pageMeta.height, jsfeat.U8C1_t);
jsfeat.imgproc.grayscale(imageData.data, pageMeta.width, pageMeta.height, img_u8);
var r = 2;
var kernel_size = (r + 1) << 1;
//Turn page into a grayscale image
GrayF32 f32 = ConvertBufferedImage.convertFromSingle(flat, null, GrayF32.class);
//Apply filter to give us that pure black and white paper look. This should also
//Remove any artifacts like shadows or discolorations in the paper.
GrayU8 bw = new GrayU8(f32.width, f32.height);
GThresholdImageOps.localSauvola(f32, bw, 15, 0.2f, true);
//Turn the pure B&W image into a bufferedimage. Also invert the colors as the filter
//Turns the page black with white text.
//Convert the ORIGINAL image
Planar<GrayF32> input = ConvertBufferedImage.convertFromMulti(image, null, true, GrayF32.class);
//Based on the four corners get the width and height of the page
Double width = topRight.x - topLeft.x;
Double height = bottomLeft.y - topLeft.y;
//Setup to make the page top down
RemovePerspectiveDistortion<Planar<GrayF32>> removePerspective = new RemovePerspectiveDistortion<>(width.intValue(), height.intValue(), ImageType.pl(3, GrayF32.class));
//Get the four corners and add them to a list fot sorting.
List<Point2D_F64> points = new ArrayList<>();
points.add(polygon.get(2));
points.add(polygon.get(1));
points.add(polygon.get(0));
points.add(polygon.get(3));
//Sort the points so that the array is in TL, TR, BR, BL order
points = sortPoints(points);
public static List<Point2D_F64> sortPoints( List<Point2D_F64> pts ) {
//Copy points into a working list
List<Point2D_F64> points = new ArrayList<Point2D_F64>();
points.addAll(pts);
//Create list of points to be returned.
List<Point2D_F64> returns = new ArrayList<Point2D_F64>();
//Sort the points by Y value
//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();
//Load test image from disk
BufferedImage image = UtilImageIO.loadImage("test.jpg");
//Convert to gray scale image
GrayU8 gray = ConvertBufferedImage.convertFromSingle(image, null, GrayU8.class);
//Brighten the image to wash out light colors in the background
GrayU8 brighter = GrayImageOps.brighten(gray, 100, 255, null);
//Reverse the black and white to make the paper page solid black for detection
ListDisplayPanel panel = new ListDisplayPanel();
ShowImages.showWindow(panel,"Document Scanning", true);
<dependency>
<groupId>org.boofcv</groupId>
<artifactId>ip</artifactId>
<version>0.26</version>
</dependency>
<dependency>
<groupId>org.boofcv</groupId>
<artifactId>visualize</artifactId>
<version>0.26</version>
</dependency>
@sminogue
sminogue / Scan.java
Created April 8, 2017 17:15
Document scanner written in java using boofcv
package imagery;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.ddogleg.struct.FastQueue;