This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ListDisplayPanel panel = new ListDisplayPanel(); | |
ShowImages.showWindow(panel,"Document Scanning", true); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
NewerOlder