Skip to content

Instantly share code, notes, and snippets.

@Ni55aN
Created December 20, 2016 19:38
Show Gist options
  • Save Ni55aN/1a62e03ca540018e9cd8e7c3277b6078 to your computer and use it in GitHub Desktop.
Save Ni55aN/1a62e03ca540018e9cd8e7c3277b6078 to your computer and use it in GitHub Desktop.
// require jsfeat
var options = {
blur_size: 5,
lap_thres: 30,
eigen_thres: 25
}
var u_max = new Int32Array([15,15,15,15,14,14,14,13,13,12,11,10,9,8,6,3,0]);
function ic_angle(img, px, py) {
var half_k = 15; // half patch size
var m_01 = 0, m_10 = 0;
var src=img.data, step=img.cols;
var u=0, v=0, center_off=(py*step + px)|0;
var v_sum=0,d=0,val_plus=0,val_minus=0;
// Treat the center line differently, v=0
for (u = -half_k; u <= half_k; ++u)
m_10 += u * src[center_off+u];
// Go line by line in the circular patch
for (v = 1; v <= half_k; ++v) {
// Proceed over the two lines
v_sum = 0;
d = u_max[v];
for (u = -d; u <= d; ++u) {
val_plus = src[center_off+u+v*step];
val_minus = src[center_off+u-v*step];
v_sum += (val_plus - val_minus);
m_10 += u * (val_plus + val_minus);
}
m_01 += v * v_sum;
}
return Math.atan2(m_01, m_10);
}
function detect_keypoints(img, corners, max_allowed) {
jsfeat.yape06.laplacian_threshold = options.lap_thres;
jsfeat.yape06.min_eigen_value_threshold = options.eigen_thres;
var count = jsfeat.yape06.detect(img, corners, 17);
if(count > max_allowed) {
jsfeat.math.qsort(corners, 0, count-1, function(a,b){return (b.score<a.score);});
count = max_allowed;
}
for(var i = 0; i < count; ++i) {
corners[i].angle = ic_angle(img, corners[i].x, corners[i].y);
}
return count;
}
function ORBdescriptors(img){
var w = img.width;
var h = img.height;
var canvas = document.createElement('canvas');
canvas.width=w;
canvas.height=h;
var ctx = canvas.getContext('2d');
img_u8 = new jsfeat.matrix_t(w, h, jsfeat.U8_t | jsfeat.C1_t);
img_u8_smooth = new jsfeat.matrix_t(w, h, jsfeat.U8_t | jsfeat.C1_t);
screen_descriptors = new jsfeat.matrix_t(32, 500, jsfeat.U8_t | jsfeat.C1_t);
pattern_descriptors = [];
ctx.drawImage(img, 0, 0, w, h);
var imageData = ctx.getImageData(0, 0, w, h);
jsfeat.imgproc.grayscale(imageData.data, w, h, img_u8);
jsfeat.imgproc.gaussian_blur(img_u8, img_u8_smooth, options.blur_size);
var keypoints = [];
var i = w*h;
while(--i >= 0)
keypoints[i] = new jsfeat.keypoint_t(0,0,0,0,-1);
var num_keypoints = detect_keypoints(img_u8_smooth, keypoints, 1000);
jsfeat.orb.describe(img_u8_smooth, keypoints, num_keypoints, screen_descriptors);
for(var i=0;i<num_keypoints;i++)
ctx.fillRect(keypoints[i].x-2,keypoints[i].y-2,4,4);
var result = {points:[]};
for(var i=0;i<num_keypoints;i++){
var sub = Array.from(screen_descriptors.data.slice(i*32,(i+1)*32));
result.points.push(Object.assign({},keypoints[i],{descriptor:sub}));
}
return {canvas:canvas, keypoints:result};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment