Smoothly denoise an image of a scanned page

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
pchun
Posts: 12
Joined: 2017-11-02T22:53:16-07:00
Authentication code: 1152

Smoothly denoise an image of a scanned page

Post by pchun » 2017-12-12T08:54:09-07:00

Here is a fragment of a scanned page image. The paper is rather grainy so I am trying to smoothly reduce background noise while keeping foreground text as much intact as possible.
https://drive.google.com/file/d/1ts2IK7 ... sp=sharing

This task appears to be rather straightforward in various image editors, say with Photoshop "smart blur" or with Paint.NET "denoise filter" (below is the page fragment processed with Paint.NET filter).

https://drive.google.com/file/d/1PACXoi ... sp=sharing

But I struggle to find appropriate equivalent in IM. I tried -enhance, -noise and -selective-blur but results are much cruder whith most of noisy flecks on the background just slightly reduced (say with -noise 5), and if I increase operators intensity the text begins to suffer as well. Is there an IM operator capable of doing that? Combination of several operators?

I am on Wndows 8, IM 7.0.3

snibgo
Posts: 10004
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Smoothly denoise an image of a scanned page

Post by snibgo » 2017-12-12T09:53:09-07:00

That "denoise.jpg" result contains lots of noise.

If we define "noise" as slopes less than 10%, we can remove noise by finding the slope, zeroing values within 10% of zero, then reconstructing by finding the div and Poisson-pasting.

Image

http://snibgo.com/imforums/fragment_out2.jpg

The process has created a slight halo. Reduce the 10% for less halo, but more noise.

The Windows BAT file that does this is:

Code: Select all

rem Remove low-amplitude noise

set SRC=fragment.jpg

call %PICTBAT%slopeXY ^
  %SRC% spm_sxy.miff

%IM32f%convert ^
  spm_sxy.miff ^
  -fuzz 10%% -fill Black -opaque Black ^
  -define quantum:format=floating-point ^
  -depth 32 ^
  spm_sxy2.miff

call %PICTBAT%slopeXYdiv ^
  spm_sxy2.miff spm_div2.miff

set MASK=spm_mask.png

%IM%convert ^
  %SRC% ^
  -fill White -colorize 100 ^
  -gravity Center ^
  -shave 1x1 ^
  -bordercolor Black -border 1 ^
  %MASK%

call %PICTBAT%poissonPaste ^
  %SRC% . %MASK% ^
  fragment_out2.png ^
  . spm_div2.miff
snibgo's IM pages: im.snibgo.com

pchun
Posts: 12
Joined: 2017-11-02T22:53:16-07:00
Authentication code: 1152

Re: Smoothly denoise an image of a scanned page

Post by pchun » 2017-12-12T10:22:39-07:00

Thank you, I will try it. Though what is spm_sxy.miff spm_sxy2.miff in your code?
I am not a professional so I didn't think of noise reduction in terms of it's scientific definition. May be what I am trying to get at shouldn't be called noise reduction from scientific point of view at all. My goal was just to smooth the background a bit to make it appear more uniform and visually pleasing. The problem is I couldn't get to how denoise.jpg looks with respect to initial fragment using -noise or -enhance operators.
Last edited by pchun on 2017-12-12T10:36:46-07:00, edited 1 time in total.

User avatar
fmw42
Posts: 22951
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Smoothly denoise an image of a scanned page

Post by fmw42 » 2017-12-12T10:33:12-07:00

This is not nearly as good as snibgo's poisson noise removal, but try

Code: Select all

convert fragment.jpg -enhance -enhance -enhance -enhance -enhance -enhance -enhance -enhance -enhance -enhance result1.jpg
Image

or if on Unix-like system, try my textcleaner script

Code: Select all

textcleaner -f 20 -o 10 -b image -S 100 -f 10 fragment.jpg resul2.jpg
Image

pchun
Posts: 12
Joined: 2017-11-02T22:53:16-07:00
Authentication code: 1152

Re: Smoothly denoise an image of a scanned page

Post by pchun » 2017-12-12T10:42:00-07:00

Thank you Fred, actually I did try to roughshod it using several iterations of -enhance in a row, but it's awfully slow (at least on my system) if I process a whole page not just a fragment.

User avatar
fmw42
Posts: 22951
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Smoothly denoise an image of a scanned page

Post by fmw42 » 2017-12-12T11:06:04-07:00

FYI. Miff is an internal IM image format that people often use as intermediate image.

snibgo
Posts: 10004
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Smoothly denoise an image of a scanned page

Post by snibgo » 2017-12-12T11:10:01-07:00

Noise is like weeds in a garden -- weeds are simply plants we don't want. My point was to define what I was going to remove from the image, and it works because we want a "flat" (EDIT: I mean, "smooth", where the second derivative is mostly zero) background, but other definitions of "noise" are possible.

The *.miff files in my script are temporary files, eg spm_sxy.miff contains two images, for the x- and y-components of slope. More details are at Seamless photomontage, see the image of the boy.
snibgo's IM pages: im.snibgo.com

pchun
Posts: 12
Joined: 2017-11-02T22:53:16-07:00
Authentication code: 1152

Re: Smoothly denoise an image of a scanned page

Post by pchun » 2017-12-14T07:48:54-07:00

I would like to ask Snibgo's help to clarify his XY approach. How do I actually go about it? I processed the page with slopeXY.bat, then put the .miff file through the transformations in the code snippet and produced the second .miff file. How do I get filtered page image from the second .miff then?

Update:
Sorry, I was too stupid to scroll through the whole code snippet and didn't see the full length of it. I understand what to do now.

pchun
Posts: 12
Joined: 2017-11-02T22:53:16-07:00
Authentication code: 1152

Re: Smoothly denoise an image of a scanned page

Post by pchun » 2017-12-16T05:27:59-07:00

One more point. I sought more information about Paint.Net denoise filter my sample fragment had been processed with. Turns out it's just a simplified wrapper for Greycstoration algorithm. Is it by any chance available in IM?

snibgo
Posts: 10004
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Smoothly denoise an image of a scanned page

Post by snibgo » 2017-12-16T09:37:01-07:00

pchun wrote:... Greycstoration algorithm. Is it by any chance available in IM?
No. I have IM scripts that do similar work (eg Adaptive contour blur and sharpen, the Modifying texture part of Seamless photomontage).
snibgo's IM pages: im.snibgo.com

User avatar
fmw42
Posts: 22951
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Smoothly denoise an image of a scanned page

Post by fmw42 » 2017-12-16T10:51:33-07:00

G'MIC contains Graycstoration code and is available on GIMP

snibgo
Posts: 10004
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Smoothly denoise an image of a scanned page

Post by snibgo » 2017-12-20T21:54:17-07:00

A different approach:

1. Create a Laplacian pyramid.
2. At each level:
2a. Calculate the standard deviation of the level multiplied by a user-supplied parameter.
2b. Where the pixel value is within that fuzz of zero, make it zero.
3. Reconstruct the image from the pyramid.

The result isn't as good as the Poisson-smooth, but it is much faster: 8 seconds for this image. (It could be optimised to be much faster, probably under 1 second.)

For example, where the parameter in 2a is 0.5:
Image
snibgo's IM pages: im.snibgo.com

Post Reply