lmmx / page-dewarp

Document image dewarping library using a cubic sheet model
MIT License
97 stars 17 forks source link

Dewarping fails due to cv2.error in OpenCV: -215:Assertion failed #22

Open Fifis opened 1 month ago

Fifis commented 1 month ago

System: Linux UL0012159 6.9.6-arch1-1 #1 SMP PREEMPT_DYNAMIC Fri, 21 Jun 2024 19:49:19 +0000 x86_64 GNU/Linux

How to reproduce: page-dewarp -d 3 warp.png (the image is attached below)

Error:

Loaded warp.png at size='2368x3507' --> resized='395x584'
  got 30 spans with 209 points.
  initial objective is 0.005677001376868241
  optimizing 247 parameters...
  optimization took 2.63 sec.
  final objective is 0.0052910268330635755
  got page dims 1.0137815190503339 x 1.8654023926694414
  output will be 1792x3280
Traceback (most recent call last):
  File "/home/avk/.local/bin/page-dewarp", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/home/avk/.local/share/pipx/venvs/page-dewarp/lib/python3.12/site-packages/page_dewarp/__main__.py", line 21, in main
    processed_img = WarpedImage(imgfile)
                    ^^^^^^^^^^^^^^^^^^^^
  File "/home/avk/.local/share/pipx/venvs/page-dewarp/lib/python3.12/site-packages/page_dewarp/image.py", line 85, in __init__
    self.threshold(page_dims, params)
  File "/home/avk/.local/share/pipx/venvs/page-dewarp/lib/python3.12/site-packages/page_dewarp/image.py", line 89, in threshold
    remap = RemappedImage(self.stem, self.cv2_img, self.small, page_dims, params)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/avk/.local/share/pipx/venvs/page-dewarp/lib/python3.12/site-packages/page_dewarp/dewarp.py", line 62, in __init__
    remapped = remap(
               ^^^^^^
cv2.error: OpenCV(4.10.0) /io/opencv/modules/imgproc/src/imgwarp.cpp:1911: error: (-215:Assertion failed) ((map1.type() == CV_32FC2 || map1.type() == CV_16SC2) && map2.empty()) || (map1.type() == CV_32FC1 && map2.type() == CV_32FC1) in function 'remap'

Input image: warp

Fifis commented 1 month ago

Hard-coding a specific version of opencv in requirements.txt helps:

numpy
matplotlib
scipy
sympy
opencv-python-headless==4.7.0.72
tomlkit
toml

This version was the latest when the latest release was made.

raudashl commented 1 month ago

cv2.remap requires np.float32, which now it is apparently asserting for, the cv2.resize a few lines above delivers np.unit8 (numpy dtypes). The solution that worked for me is to add ".astype(np.float32)" at line 57 and 60 at the end of the resize command to get the right dtype for image_x_coords and image_y_coords.