如何从扫描图像中去除灰色背景
How to remove gray background from scanned image
我使用 jfreesane 库为 Windows 编写了一个小型 Java SANE 客户端,它在一段时间内运行良好,但昨天我发现了一个问题扫描仪 CanoScan LiDE 60 - 当使用 "grayscale" 模式时,扫描图像有灰色背景,我不知道如何摆脱它。这就是我要说的:
我运行用其他一些扫描仪解决了这个问题,并通过调整对比度参数解决了这个问题,但是CanoScan LiDE 60没有。这是 "scanimage -A" 输出的完整选项列表(我尝试了所有与颜色相关的选项,除了 gamma-table ):
All options specific to device `genesys:libusb:003:002':
Scan Mode:
--mode Color|Gray|Lineart [Gray]
Selects the scan mode (e.g., lineart, monochrome, or color).
--source Flatbed|Transparency Adapter [inactive]
Selects the scan source (such as a document-feeder).
--preview[=(yes|no)] [no]
Request a preview-quality scan.
--depth 8|16 [8]
Number of bits per sample, typical values are 1 for "line-art" and 8
for multibit scans.
--resolution 1200|600|300|150|75dpi [75]
Sets the resolution of the scanned image.
Geometry:
-l 0..218mm [0]
Top-left x position of scan area.
-t 0..299mm [0]
Top-left y position of scan area.
-x 0..218mm [218]
Width of scan-area.
-y 0..299mm [299]
Height of scan-area.
Enhancement:
--custom-gamma[=(yes|no)] [no]
Determines whether a builtin or a custom gamma-table should be used.
--gamma-table 0..65535,... [inactive]
Gamma-correction table. In color mode this option equally affects the
red, green, and blue channels simultaneously (i.e., it is an intensity
gamma table).
--red-gamma-table 0..65535,... [inactive]
Gamma-correction table for the red band.
--green-gamma-table 0..65535,... [inactive]
Gamma-correction table for the green band.
--blue-gamma-table 0..65535,... [inactive]
Gamma-correction table for the blue band.
--swdeskew[=(yes|no)] [no]
Request backend to rotate skewed pages digitally
--swcrop[=(yes|no)] [no]
Request backend to remove border from pages digitally
--swdespeck[=(yes|no)] [no]
Request backend to remove lone dots digitally
--despeck 1..9 (in steps of 1) [1]
Maximum diameter of lone dots to remove from scan
--swskip 0..100% (in steps of 1) [0]
Request driver to discard pages with low numbers of dark pixels
--swderotate[=(yes|no)] [no]
Request driver to detect and correct 90 degree image rotation
Extras:
--lamp-off-time 0..60 [15]
The lamp will be turned off after the given time (in minutes). A value
of 0 means, that the lamp won't be turned off.
--lamp-off-scan[=(yes|no)] [no]
The lamp will be turned off during scan.
--threshold 0..100% (in steps of 1) [50]
Select minimum-brightness to get a white point
--threshold-curve 0..127 (in steps of 1) [50]
Dynamic threshold curve, from light to dark, normally 50-65
--disable-dynamic-lineart[=(yes|no)] [no]
Disable use of a software adaptive algorithm to generate lineart
relying instead on hardware lineart.
--disable-interpolation[=(yes|no)] [no]
When using high resolutions where the horizontal resolution is smaller
than the vertical resolution this disables horizontal interpolation.
--color-filter Red|Green|Blue|None [None]
When using gray or lineart this option selects the used color.
Sensors:
Buttons:
--clear-calibration
Clear calibration cache
还有一件有趣的事情:简单扫描(Linux 扫描程序)以某种方式设法使用此扫描仪从图像中去除灰色:
我不确定它是否使用了一些 post-扫描处理,或者它是否知道如何告诉扫描仪有关对比度的信息。如果是前者,是否有任何 Java 库可以消除这种灰色噪声?
经过一些测试,我确实得到了这个函数(对比公式取自here):
public void processImage(BufferedImage bimg, int brightness, int contrast, int t_black, int t_white) {
Color c;
int r, g, b;
float factor = (259f * (contrast + 255f)) / (255f * (259f - contrast));
for (int x = 0; x < bimg.getWidth(); x++)
for (int y = 0; y < bimg.getHeight(); y++) {
c = new Color(bimg.getRGB(x, y));
// apply brightness and contrast
r = Math.round(factor * (c.getRed() - 128) + 128) + brightness;
g = Math.round(factor * (c.getGreen() - 128) + 128) + brightness;
b = Math.round(factor * (c.getBlue() - 128) + 128) + brightness;
// limit to [0, 255] range
r = Math.min(255, Math.max(0, r));
g = Math.min(255, Math.max(0, g));
b = Math.min(255, Math.max(0, b));
// apply black and white thresholds
if (r < t_black && g < t_black && b < t_black)
bimg.setRGB(x, y, 0);
else if (r > t_white && g > t_white && b > t_white)
bimg.setRGB(x, y, 255 << 16 | 255 << 8 | 255);
else
bimg.setRGB(x, y, r << 16 | g << 8 | b);
}
}
我使用 jfreesane 库为 Windows 编写了一个小型 Java SANE 客户端,它在一段时间内运行良好,但昨天我发现了一个问题扫描仪 CanoScan LiDE 60 - 当使用 "grayscale" 模式时,扫描图像有灰色背景,我不知道如何摆脱它。这就是我要说的:
我运行用其他一些扫描仪解决了这个问题,并通过调整对比度参数解决了这个问题,但是CanoScan LiDE 60没有。这是 "scanimage -A" 输出的完整选项列表(我尝试了所有与颜色相关的选项,除了 gamma-table ):
All options specific to device `genesys:libusb:003:002':
Scan Mode:
--mode Color|Gray|Lineart [Gray]
Selects the scan mode (e.g., lineart, monochrome, or color).
--source Flatbed|Transparency Adapter [inactive]
Selects the scan source (such as a document-feeder).
--preview[=(yes|no)] [no]
Request a preview-quality scan.
--depth 8|16 [8]
Number of bits per sample, typical values are 1 for "line-art" and 8
for multibit scans.
--resolution 1200|600|300|150|75dpi [75]
Sets the resolution of the scanned image.
Geometry:
-l 0..218mm [0]
Top-left x position of scan area.
-t 0..299mm [0]
Top-left y position of scan area.
-x 0..218mm [218]
Width of scan-area.
-y 0..299mm [299]
Height of scan-area.
Enhancement:
--custom-gamma[=(yes|no)] [no]
Determines whether a builtin or a custom gamma-table should be used.
--gamma-table 0..65535,... [inactive]
Gamma-correction table. In color mode this option equally affects the
red, green, and blue channels simultaneously (i.e., it is an intensity
gamma table).
--red-gamma-table 0..65535,... [inactive]
Gamma-correction table for the red band.
--green-gamma-table 0..65535,... [inactive]
Gamma-correction table for the green band.
--blue-gamma-table 0..65535,... [inactive]
Gamma-correction table for the blue band.
--swdeskew[=(yes|no)] [no]
Request backend to rotate skewed pages digitally
--swcrop[=(yes|no)] [no]
Request backend to remove border from pages digitally
--swdespeck[=(yes|no)] [no]
Request backend to remove lone dots digitally
--despeck 1..9 (in steps of 1) [1]
Maximum diameter of lone dots to remove from scan
--swskip 0..100% (in steps of 1) [0]
Request driver to discard pages with low numbers of dark pixels
--swderotate[=(yes|no)] [no]
Request driver to detect and correct 90 degree image rotation
Extras:
--lamp-off-time 0..60 [15]
The lamp will be turned off after the given time (in minutes). A value
of 0 means, that the lamp won't be turned off.
--lamp-off-scan[=(yes|no)] [no]
The lamp will be turned off during scan.
--threshold 0..100% (in steps of 1) [50]
Select minimum-brightness to get a white point
--threshold-curve 0..127 (in steps of 1) [50]
Dynamic threshold curve, from light to dark, normally 50-65
--disable-dynamic-lineart[=(yes|no)] [no]
Disable use of a software adaptive algorithm to generate lineart
relying instead on hardware lineart.
--disable-interpolation[=(yes|no)] [no]
When using high resolutions where the horizontal resolution is smaller
than the vertical resolution this disables horizontal interpolation.
--color-filter Red|Green|Blue|None [None]
When using gray or lineart this option selects the used color.
Sensors:
Buttons:
--clear-calibration
Clear calibration cache
还有一件有趣的事情:简单扫描(Linux 扫描程序)以某种方式设法使用此扫描仪从图像中去除灰色:
我不确定它是否使用了一些 post-扫描处理,或者它是否知道如何告诉扫描仪有关对比度的信息。如果是前者,是否有任何 Java 库可以消除这种灰色噪声?
经过一些测试,我确实得到了这个函数(对比公式取自here):
public void processImage(BufferedImage bimg, int brightness, int contrast, int t_black, int t_white) {
Color c;
int r, g, b;
float factor = (259f * (contrast + 255f)) / (255f * (259f - contrast));
for (int x = 0; x < bimg.getWidth(); x++)
for (int y = 0; y < bimg.getHeight(); y++) {
c = new Color(bimg.getRGB(x, y));
// apply brightness and contrast
r = Math.round(factor * (c.getRed() - 128) + 128) + brightness;
g = Math.round(factor * (c.getGreen() - 128) + 128) + brightness;
b = Math.round(factor * (c.getBlue() - 128) + 128) + brightness;
// limit to [0, 255] range
r = Math.min(255, Math.max(0, r));
g = Math.min(255, Math.max(0, g));
b = Math.min(255, Math.max(0, b));
// apply black and white thresholds
if (r < t_black && g < t_black && b < t_black)
bimg.setRGB(x, y, 0);
else if (r > t_white && g > t_white && b > t_white)
bimg.setRGB(x, y, 255 << 16 | 255 << 8 | 255);
else
bimg.setRGB(x, y, r << 16 | g << 8 | b);
}
}