UNPKG

jsfeat

Version:

JavaScript Computer Vision library

233 lines (202 loc) 7.25 kB
/** * @author Eugene Zatepyakin / http://inspirit.ru/ */ (function(global) { "use strict"; // // CONSTANTS var EPSILON = 0.0000001192092896; var FLT_MIN = 1E-37; // implementation from CCV project // currently working only with u8,s32,f32 var U8_t = 0x0100, S32_t = 0x0200, F32_t = 0x0400, S64_t = 0x0800, F64_t = 0x1000; var C1_t = 0x01, C2_t = 0x02, C3_t = 0x03, C4_t = 0x04; var _data_type_size = new Int32Array([ -1, 1, 4, -1, 4, -1, -1, -1, 8, -1, -1, -1, -1, -1, -1, -1, 8 ]); var get_data_type = (function () { return function(type) { return (type & 0xFF00); } })(); var get_channel = (function () { return function(type) { return (type & 0xFF); } })(); var get_data_type_size = (function () { return function(type) { return _data_type_size[(type & 0xFF00) >> 8]; } })(); // color conversion var COLOR_RGBA2GRAY = 0; var COLOR_RGB2GRAY = 1; var COLOR_BGRA2GRAY = 2; var COLOR_BGR2GRAY = 3; // box blur option var BOX_BLUR_NOSCALE = 0x01; // svd options var SVD_U_T = 0x01; var SVD_V_T = 0x02; var data_t = (function () { function data_t(size_in_bytes, buffer) { // we need align size to multiple of 8 this.size = ((size_in_bytes + 7) | 0) & -8; if (typeof buffer === "undefined") { this.buffer = new ArrayBuffer(this.size); } else { this.buffer = buffer; this.size = buffer.length; } this.u8 = new Uint8Array(this.buffer); this.i32 = new Int32Array(this.buffer); this.f32 = new Float32Array(this.buffer); this.f64 = new Float64Array(this.buffer); } return data_t; })(); var matrix_t = (function () { // columns, rows, data_type function matrix_t(c, r, data_type, data_buffer) { this.type = get_data_type(data_type)|0; this.channel = get_channel(data_type)|0; this.cols = c|0; this.rows = r|0; if (typeof data_buffer === "undefined") { this.allocate(); } else { this.buffer = data_buffer; // data user asked for this.data = this.type&U8_t ? this.buffer.u8 : (this.type&S32_t ? this.buffer.i32 : (this.type&F32_t ? this.buffer.f32 : this.buffer.f64)); } } matrix_t.prototype.allocate = function() { // clear references delete this.data; delete this.buffer; // this.buffer = new data_t((this.cols * get_data_type_size(this.type) * this.channel) * this.rows); this.data = this.type&U8_t ? this.buffer.u8 : (this.type&S32_t ? this.buffer.i32 : (this.type&F32_t ? this.buffer.f32 : this.buffer.f64)); } matrix_t.prototype.copy_to = function(other) { var od = other.data, td = this.data; var i = 0, n = (this.cols*this.rows*this.channel)|0; for(; i < n-4; i+=4) { od[i] = td[i]; od[i+1] = td[i+1]; od[i+2] = td[i+2]; od[i+3] = td[i+3]; } for(; i < n; ++i) { od[i] = td[i]; } } matrix_t.prototype.resize = function(c, r, ch) { if (typeof ch === "undefined") { ch = this.channel; } // relocate buffer only if new size doesnt fit var new_size = (c * get_data_type_size(this.type) * ch) * r; if(new_size > this.buffer.size) { this.cols = c; this.rows = r; this.channel = ch; this.allocate(); } else { this.cols = c; this.rows = r; this.channel = ch; } } return matrix_t; })(); var pyramid_t = (function () { function pyramid_t(levels) { this.levels = levels|0; this.data = new Array(levels); this.pyrdown = jsfeat.imgproc.pyrdown; } pyramid_t.prototype.allocate = function(start_w, start_h, data_type) { var i = this.levels; while(--i >= 0) { this.data[i] = new matrix_t(start_w >> i, start_h >> i, data_type); } } pyramid_t.prototype.build = function(input, skip_first_level) { if (typeof skip_first_level === "undefined") { skip_first_level = true; } // just copy data to first level var i = 2, a = input, b = this.data[0]; if(!skip_first_level) { var j=input.cols*input.rows; while(--j >= 0) { b.data[j] = input.data[j]; } } b = this.data[1]; this.pyrdown(a, b); for(; i < this.levels; ++i) { a = b; b = this.data[i]; this.pyrdown(a, b); } } return pyramid_t; })(); var keypoint_t = (function () { function keypoint_t(x,y,score,level,angle) { if (typeof x === "undefined") { x=0; } if (typeof y === "undefined") { y=0; } if (typeof score === "undefined") { score=0; } if (typeof level === "undefined") { level=0; } if (typeof angle === "undefined") { angle=-1.0; } this.x = x; this.y = y; this.score = score; this.level = level; this.angle = angle; } return keypoint_t; })(); // data types global.U8_t = U8_t; global.S32_t = S32_t; global.F32_t = F32_t; global.S64_t = S64_t; global.F64_t = F64_t; // data channels global.C1_t = C1_t; global.C2_t = C2_t; global.C3_t = C3_t; global.C4_t = C4_t; // popular formats global.U8C1_t = U8_t | C1_t; global.U8C3_t = U8_t | C3_t; global.U8C4_t = U8_t | C4_t; global.F32C1_t = F32_t | C1_t; global.F32C2_t = F32_t | C2_t; global.S32C1_t = S32_t | C1_t; global.S32C2_t = S32_t | C2_t; // constants global.EPSILON = EPSILON; global.FLT_MIN = FLT_MIN; // color convert global.COLOR_RGBA2GRAY = COLOR_RGBA2GRAY; global.COLOR_RGB2GRAY = COLOR_RGB2GRAY; global.COLOR_BGRA2GRAY = COLOR_BGRA2GRAY; global.COLOR_BGR2GRAY = COLOR_BGR2GRAY; // options global.BOX_BLUR_NOSCALE = BOX_BLUR_NOSCALE; global.SVD_U_T = SVD_U_T; global.SVD_V_T = SVD_V_T; global.get_data_type = get_data_type; global.get_channel = get_channel; global.get_data_type_size = get_data_type_size; global.data_t = data_t; global.matrix_t = matrix_t; global.pyramid_t = pyramid_t; global.keypoint_t = keypoint_t; })(jsfeat);