/
NoiseRemove.java
74 lines (74 loc) · 2.53 KB
/
NoiseRemove.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/* NoiseRemove.java
* =========================================================================
* This file is originally part of the MathOCR Project
*
* Copyright (C) 2014,2015 Chan Chung Kwong
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
*/
package com.github.chungkwong.mathocr.preprocess;
import com.github.chungkwong.mathocr.common.ComponentPool;
import java.awt.image.*;
/**
* A preprocessor that apply post-processing step of binarized image described
* in Adaptive degraded document image binarization by B. Gatos , I. Pratikakis,
* S.J. Perantonis
*/
public final class NoiseRemove extends SimplePreprocessor{
/**
* Construct a NoiseRemove
*/
public NoiseRemove(){
}
@Override
public boolean isApplicable(BufferedImage image){
return image.getType()==BufferedImage.TYPE_BYTE_BINARY;
}
@Override
public int[] preprocess(int[] pixels,int width,int height){
int lh=new ComponentPool(pixels,width,height).getAverageHeight();
//System.out.println(lh);
int n=lh*3/20, ksh=n*n/10, ksw=n*n/20, dx=n/4, dy=n/4, ksw1=n*n*7/20;
int dl=(n+1)/2, dr=n/2, len=width*height;
for(int i=0;i<len;i++){
pixels[i]=1-(pixels[i]&0x1);
}
long[][] intImg=ImageUtil.getIntegralImage(pixels,width,height);
for(int i=0, ind=0;i<height;i++){
for(int j=0;j<width;j++,ind++){
if(pixels[ind]==1&&ImageUtil.windowValue(intImg,width,height,i,j,dl,dr)<ksh){
pixels[ind]=0;
}
}
}
intImg=ImageUtil.getIntegralImage(pixels,width,height);
for(int i=0, ind=0;i<height;i++){
for(int j=0;j<width;j++,ind++){
if(pixels[ind]==0&&ImageUtil.windowValue(intImg,width,height,i,j,dl,dr)>ksw
&&Math.abs(ImageUtil.averageX(intImg,width,height,i,j,dl,dr)-j)<dx&&Math.abs(ImageUtil.averageY(intImg,width,height,i,j,dl,dr)-i)<dy){
pixels[ind]=1;
}
}
}
intImg=ImageUtil.getIntegralImage(pixels,width,height);
for(int i=0, ind=0;i<height;i++){
for(int j=0;j<width;j++,ind++){
if(pixels[ind]==1||ImageUtil.windowValue(intImg,width,height,i,j,dl,dr)>ksw1){
pixels[ind]=0xff000000;
}else{
pixels[ind]=0xffffffff;
}
}
}
return pixels;
}
}