Last active
August 14, 2018 23:47
-
-
Save sminogue/b20bc6c270e5784d781d3f719c81dec5 to your computer and use it in GitHub Desktop.
using jsfeat find lines in image using canny and hough
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; | |
jsfeat.imgproc.gaussian_blur(img_u8, img_u8, kernel_size, 0); | |
jsfeat.imgproc.canny(img_u8, img_u8, 100, 300); | |
var scaler = pageMeta.width; | |
if (pageMeta.height < scaler) { | |
scaler = pageMeta.height; | |
} | |
scaler = scaler * .4; | |
var h = jsfeat.imgproc.hough_transform(img_u8, 1, (Math.PI / 540), scaler); | |
//At this point h really are the lines we are interested in. But im going to turn them into | |
//points so I can draw them on a canvas... Just for shits and grins. | |
var lines = []; | |
for (var i = 0; i < h.length; i++) { | |
var rho = h[i][0]; | |
var theta = h[i][1]; | |
var a = Math.cos(theta); | |
var b = Math.sin(theta); | |
var x0 = a * rho; | |
var y0 = b * rho; | |
var pt1 = {}; | |
pt1.x = Math.round(x0 + 1000 * (-b)); | |
pt1.y = Math.round(y0 + 1000 * (a)); | |
var pt2 = {}; | |
pt2.x = Math.round(x0 - 1000 * (-b)); | |
pt2.y = Math.round(y0 - 1000 * (a)); | |
var line = {}; | |
line.start = pt1; | |
line.end = pt2; | |
lines.push(line); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment