判断图像中有多少行文本(开发中)
const fs = require("fs")
const getPixels = require("get-pixels")
function getColor(x,y,pixels) {
return [
pixels.data[x*4+y*4*pixels.shape[0]],
pixels.data[x*4+1+y*4*pixels.shape[0]],
pixels.data[x*4+2+y*4*pixels.shape[0]],
pixels.data[x*4+3+y*4*pixels.shape[0]]
]
}
function isNearColor(color1,color2){
if((Math.abs(color1[0]-color2[0])+Math.abs(color1[1]-color2[1])+Math.abs(color1[2]-color2[2])+Math.abs(color1[3]-color2[3]))<50){
return 1;
}
return 0;
}
function isNearLine(line1,line2){
// if(Math.abs(line1[1].x-line2[1].x)<250){
// return 1;
// }
if(Math.abs(line1[1].y-line2[1].y)<380){
return 1;
}
return 0;
}
function getPixelsSync(filename){
return new Promise(function (resolve,reject) {
getPixels(filename, function(err, pixels) {
if(err) {
console.log("Bad image path")
reject(err)
return
}
resolve(pixels)
})
})
}
async function init() {
const pixels=await getPixelsSync('1.jpeg');
console.log(pixels)
const [w,h]=pixels.shape;
let lineCache=[];
let preLine=null;
//x射线
for (let y=0;y<h;y++){
let preColor=null;
const line=[]
for (let x=0;x<w;x++){
const curColor=getColor(x,y,pixels)
if(!preColor){
line[0]={x,y}
}else{
line[1]={x,y};
if(!isNearColor(preColor,curColor)){
break;
}
}
preColor=curColor;
}
if(preLine){
if(!isNearLine(preLine,line)){
if(preLine[1].x<line[1].x){
lineCache.push(line);
}else{
lineCache.push(preLine);
}
}
}
preLine=line;
}
preLine=null;
//x射线
for (let x=0;x<w;x++){
let preColor=null;
const line=[]
for (let y=0;y<h;y++){
const curColor=getColor(x,y,pixels)
if(!preColor){
line[0]={x,y}
}else{
line[1]={x,y};
if(!isNearColor(preColor,curColor)){
break;
}
}
preColor=curColor;
}
if(preLine){
if(!isNearLine(preLine,line)){
if(preLine[1].y<line[1].y){
lineCache.push(line);
}else{
lineCache.push(preLine);
}
}
}
preLine=line;
}
console.log(lineCache)
// scanLine(pixels.shape[0],pixels.shape[1],function (curp,prep) {
// return isNearColor(getColor(curp.x,curp.y,pixels),getColor(prep.x,prep.y,pixels))
// },function (){},pixels)
}
init()
// scanRound(0,0,100,100,function (x,y) {
// return 1;
// },function (x,y) {
// console.log(x,y);
// })
https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1561552141135&di=170679fcee4e69499f94b5167c8e474a&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201208%2F27%2F20120827144059_sijxK.jpeg