page_dewarp icon indicating copy to clipboard operation
page_dewarp copied to clipboard

Page dewarp cuts text

Open IonutQo2 opened this issue 5 years ago • 8 comments

I have been using this using the command line python page_dewarp.py image.jpg with the included images successfully but when using an image of my own I could see that the text is cropped and cut.

The image was a 2MB jpeg with the resolution of 3400x4600px

This is the original and the resulted image: image

Do you know what might be the problem? Thanks

IonutQo2 avatar Apr 28 '19 18:04 IonutQo2

Try setting PAGE_MARGIN_X and PAGE_MARGIN_Y to zeros.

opennota avatar May 28 '19 03:05 opennota

I personally would prefer other defaults:

  • no cropping
  • no binarization (black/white)
  • no subsampling (full resolution)
  • = dewarping only

jbarth-ubhd avatar Mar 12 '20 16:03 jbarth-ubhd

I personally would prefer other defaults:

  • no cropping
  • no binarization (black/white)
  • no subsampling (full resolution)
  • = dewarping only

how to set in code?

KyleWang-Hunter avatar Oct 11 '22 01:10 KyleWang-Hunter

diff --git a/page_dewarp.py b/page_dewarp.py
index 6ef5b33..d095244 100755
--- a/page_dewarp.py
+++ b/page_dewarp.py
@@ -20,8 +20,8 @@ import scipy.optimize
 # for some reason pylint complains about cv2 members being undefined :(
 # pylint: disable=E1101
 
-PAGE_MARGIN_X = 50       # reduced px to ignore near L/R edge
-PAGE_MARGIN_Y = 20       # reduced px to ignore near T/B edge
+PAGE_MARGIN_X = 0       # reduced px to ignore near L/R edge
+PAGE_MARGIN_Y = 0       # reduced px to ignore near T/B edge
 
 OUTPUT_ZOOM = 1.0        # how much to zoom output relative to *original* image
 OUTPUT_DPI = 300         # just affects stated DPI of PNG, not appearance
@@ -813,17 +813,13 @@ def remap_image(name, img, small, page_dims, params):
     image_y_coords = cv2.resize(image_y_coords, (width, height),
                                 interpolation=cv2.INTER_CUBIC)
 
-    img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
-
-    remapped = cv2.remap(img_gray, image_x_coords, image_y_coords,
+    remapped = cv2.remap(img, image_x_coords, image_y_coords,
                          cv2.INTER_CUBIC,
                          None, cv2.BORDER_REPLICATE)
 
-    thresh = cv2.adaptiveThreshold(remapped, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
-                                   cv2.THRESH_BINARY, ADAPTIVE_WINSZ, 25)
+    thresh = remapped
 
     pil_image = Image.fromarray(thresh)
-    pil_image = pil_image.convert('1')
 
     threshfile = name + '_thresh.png'
     pil_image.save(threshfile, dpi=(OUTPUT_DPI, OUTPUT_DPI))

jbarth-ubhd avatar Oct 11 '22 07:10 jbarth-ubhd

diff --git a/page_dewarp.py b/page_dewarp.py
index 6ef5b33..d095244 100755
--- a/page_dewarp.py
+++ b/page_dewarp.py
@@ -20,8 +20,8 @@ import scipy.optimize
 # for some reason pylint complains about cv2 members being undefined :(
 # pylint: disable=E1101
 
-PAGE_MARGIN_X = 50       # reduced px to ignore near L/R edge
-PAGE_MARGIN_Y = 20       # reduced px to ignore near T/B edge
+PAGE_MARGIN_X = 0       # reduced px to ignore near L/R edge
+PAGE_MARGIN_Y = 0       # reduced px to ignore near T/B edge
 
 OUTPUT_ZOOM = 1.0        # how much to zoom output relative to *original* image
 OUTPUT_DPI = 300         # just affects stated DPI of PNG, not appearance
@@ -813,17 +813,13 @@ def remap_image(name, img, small, page_dims, params):
     image_y_coords = cv2.resize(image_y_coords, (width, height),
                                 interpolation=cv2.INTER_CUBIC)
 
-    img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
-
-    remapped = cv2.remap(img_gray, image_x_coords, image_y_coords,
+    remapped = cv2.remap(img, image_x_coords, image_y_coords,
                          cv2.INTER_CUBIC,
                          None, cv2.BORDER_REPLICATE)
 
-    thresh = cv2.adaptiveThreshold(remapped, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
-                                   cv2.THRESH_BINARY, ADAPTIVE_WINSZ, 25)
+    thresh = remapped
 
     pil_image = Image.fromarray(thresh)
-    pil_image = pil_image.convert('1')
 
     threshfile = name + '_thresh.png'
     pil_image.save(threshfile, dpi=(OUTPUT_DPI, OUTPUT_DPI))

thank you very much

KyleWang-Hunter avatar Oct 11 '22 07:10 KyleWang-Hunter

PS: did a simulation of the warping of pages of an open book. Approximations of this pages (x, y) look like x^4, not like x³, but I don't know how this maps to "text line curves":

image

jbarth-ubhd avatar Oct 11 '22 07:10 jbarth-ubhd