Created
March 5, 2019 15:41
-
-
Save piercus/ee12d05f99cae58ee1765a875bbe4ad9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const cv = require('opencv4nodejs') | |
const tf = require('@tensorflow/tfjs-node'); | |
const h = require('hasard') | |
const mat = cv.imread('./tmp/lenna.png') | |
const rect = { | |
x: 0, | |
y: 0, | |
w: 400, | |
h: 512 | |
} | |
const getHistAxis = channel => ([ | |
{ | |
channel, | |
bins: 256, | |
ranges: [0, 256] | |
} | |
]); | |
const getDiff = function(rect){ | |
const size = {w: mat.cols, h: mat.rows} | |
const intTensor = tf.tensor4d(mat.getData(), [1, size.h, size.w, 3]) | |
const tensorValues = intTensor.dataSync(); | |
const floatTensor = intTensor.div(255); | |
const cropped = tf.image.cropAndResize(floatTensor, [[rect.y/size.h, rect.x/size.w, (rect.y+rect.h)/size.h, (rect.x+rect.w)/size.w]], [0], [rect.h, rect.w]); | |
const croppedValues = cropped.mul(255).toInt().dataSync(); | |
const d = new Uint8Array(croppedValues); | |
const mat2 = new cv.Mat(d, rect.h, rect.w, cv.CV_8UC3); | |
const mat3 = new cv.Mat(rect.h, rect.w, cv.CV_8UC3); | |
mat.getRegion(new cv.Rect(rect.x, rect.y, rect.w, rect.h)).copyTo(mat3) | |
const diff = mat3.absdiff(mat2) | |
const hist = cv.calcHist(diff, getHistAxis(0)).add(cv.calcHist(diff, getHistAxis(1))).add(cv.calcHist(diff, getHistAxis(2))); | |
const all = rect.w*rect.h*3; | |
return {hist, all} | |
} | |
const xRef = h.reference(h.integer([0, 511])) | |
const yRef = h.reference(h.integer([0, 511])) | |
const min = h.fn(Math.min.bind(Math)) | |
const hasardRect = h.object({ | |
x: xRef, | |
y: yRef, | |
w: min(h.substract(512, xRef), h.integer([1, 10])), | |
h: min(h.substract(512, yRef), h.integer([1, 10])) | |
}) | |
const rects = hasardRect.run(1000); | |
let max = 0; | |
let rectMax; | |
const hists = [] | |
let count = 0 | |
rects.map(r => { | |
const {hist, all } = getDiff(r) | |
hists.push(hist); | |
count += all; | |
}) | |
const sum = hists.reduce((a,b) => a.add(b)); | |
console.log(sum.getDataAsArray().map(a => a[0]).join(',')) | |
console.log('count', count) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment