37 lines
1.9 KiB
JavaScript
37 lines
1.9 KiB
JavaScript
const { Img } = require("./paper-recognizer.js");
|
|
const c = []; process.stdin.on("data", d => c.push(d));
|
|
process.stdin.on("end", () => {
|
|
const b = Buffer.concat(c);
|
|
const w = b.readUInt32LE(0), h = b.readUInt32LE(4);
|
|
const px = new Uint8Array(b.slice(8));
|
|
const bgr = new Uint8Array(w*h*3);
|
|
for (let i=0;i<w*h;i++){const s=i*4,d=i*3;bgr[d]=px[s+2];bgr[d+1]=px[s+1];bgr[d+2]=px[s];}
|
|
const hsv = Img.bgrImageToHsv(bgr,w,h);
|
|
const bm = Img.hsvInRange(hsv,w,h,180,260,30,255,30,220);
|
|
const gm = Img.hsvInRange(hsv,w,h,80,170,30,255,30,220);
|
|
const cm = new Uint8Array(w*h);
|
|
for(let i=0;i<w*h;i++)cm[i]=Math.max(bm[i],gm[i]);
|
|
const cl = Img.morphClose(cm,w,h,5);
|
|
const ctrs = Img.findContours(cl,w,h);
|
|
console.log("Contours: "+ctrs.length);
|
|
const GP={MIN_CONTOUR_AREA:100,MIN_RECTANGULARITY:0.5,BIG_LOCATOR_RATIO_SHORT:2.0,BIG_LOCATOR_RATIO_LONG:3.0,SMALL_LOCATOR_RATIO:1.667,MIN_BIG_LOCATOR_AREA:200,MAX_BIG_LOCATOR_AREA:8000,MIN_SMALL_LOCATOR_AREA:50,MAX_SMALL_LOCATOR_AREA:2000};
|
|
for(const ct of ctrs){
|
|
const area=Img.contourArea(ct);
|
|
if(area<GP.MIN_CONTOUR_AREA)continue;
|
|
const br=Img.boundingRect(ct);
|
|
if(area/(br.w*br.h)<GP.MIN_RECTANGULARITY)continue;
|
|
const mr=Img.minAreaRect(ct);
|
|
if(area/(mr.size.w*mr.size.h)<GP.MIN_RECTANGULARITY)continue;
|
|
const ar=Math.max(mr.size.w,mr.size.h)/Math.max(Math.min(mr.size.w,mr.size.h),1);
|
|
if(ar>=0.9&&ar<=1.1||ar>4)continue;
|
|
const tol=0.35;
|
|
const ib=Math.abs(ar-GP.BIG_LOCATOR_RATIO_SHORT)<tol||Math.abs(ar-GP.BIG_LOCATOR_RATIO_LONG)<tol;
|
|
const is=Math.abs(ar-GP.SMALL_LOCATOR_RATIO)<tol;
|
|
const cx=Math.round(mr.center.x);const cy=Math.round(mr.center.y);
|
|
const j=(cy*w+cx)*3;
|
|
const hh=bgr[j];const gg=bgr[j+1];const rr=bgr[j+2];
|
|
console.log(JSON.stringify({area:area.toFixed(0),br:[br.x,br.y,br.w,br.h],mr:[mr.size.w.toFixed(0),mr.size.h.toFixed(0),mr.angle.toFixed(1)],ar:ar.toFixed(3),isBig:ib,isSmall:is,ctr:[cx,cy],rgb:[rr,gg,hh]}));
|
|
}
|
|
process.exit(0);
|
|
});
|