sparkfish / augraphy

Augmentation pipeline for rendering synthetic paper printing, faxing, scanning and copy machine processes
https://github.com/sparkfish/augraphy
MIT License
344 stars 45 forks source link

Create `Faxify` Augmentation #68

Closed jboarman closed 3 years ago

jboarman commented 3 years ago

Here are the effects that a faxed document experiences:

The resolution should be assumed to have already been normalized (to 300 DPI) in advance of calling this augmentation, OR the source resolution could be provided as a parameter so that proper scaling can be calcutated to a convert to 200 DPI. Will may need to re-scale it back to the source resolution (after being downsampled) so that other augmentations, that rely on a normalized resolution, can be applied after this augmentation has been applied.

Phaxio has a short and simple article showing the difference in thresholded ("monochrome") vs halftoned faxes.

Thresholding basics are covered in this OpenCV Tutorial.

image

Example PDFs (from Phaxio):

original.pdf monochrome.pdf halftone.pdf

kwcckw commented 3 years ago

I tested several implementations of halftone and i think this is the best and closest to the example above:

image

image

Example of the other implementation:

image

image

What do you guys think?

jboarman commented 3 years ago

@kwcckw I concur with your assessment. Your suggestion looks good.

I’m assuming the Phaxio implementation is tweaked for the low resolution of a fax machine, which is why their output differs.

It seems to favor a straight monochrome effect, instead of halftone, for near black values. You should be able to emulate that effect by forcing the pixels to true black that are within some threshold prior to halftoning the image.

If the halftone effect messes with true black, then you will have to separate pixels that are near black into a separate matrix and overlay them on top of the halftone output.

kwcckw commented 3 years ago

It seems to favor a straight monochrome effect, instead of halftone, for near black values. You should be able to emulate that effect by forcing the pixels to true black that are within some threshold prior to halftoning the image.

Right, but the question would be are we gonna do that in our augmentation too? Although it looks better in that way.

If the halftone effect messes with true black, then you will have to separate pixels that are near black into a separate matrix and overlay them on top of the halftone output.

Yes, i will try that and check again whether it is better, thanks.

proofconstruction commented 3 years ago

We'll probably need it in more places, so I put a binaryThreshold function in augmentations/lib.py that takes an image and a cv2.THRESH_ value (defaults to Otsu), makes the image grayscale, then applies the threshold.

(I also forgot to pre-commit install in my new development container, so I originally pushed a non-PEP8-compliant commit. See how important it is to have pre-commit hooks? :grin: )

kwcckw commented 3 years ago

We'll probably need it in more places, so I put a binaryThreshold function in augmentations/lib.py that takes an image and a cv2.THRESH_ value (defaults to Otsu), makes the image grayscale, then applies the threshold.

Okay, we would need several thresholding methods so i gonna edit that section again if needed.

proofconstruction commented 3 years ago

Completed with PR #87

jboarman commented 2 years ago

Real faxed example with lots of dithering image