OpenStitching / stitching

A Python package for fast and robust Image Stitching
Apache License 2.0
2k stars 157 forks source link

Support reading/writing 16-bit images #226

Open mstenta opened 1 month ago

mstenta commented 1 month ago

Problem: Stitching 16-bit TIFF images results in an 8-bit output image.

If stitching is the last step of an editing workflow before saving a file for web/print, then that's OK because 8-bit is enough for web/print. But it means that stitched images lose a lot of the data that is useful during editing. If stitching supported exporting 16-bit images, and maintained the color data of the input images (to the best of its ability), then it could be used as the first step of an editing workflow. As you can imagine, editing a whole stitched image is easier than editing individual pieces of it. :-)

Currently, stitching uses OpenCV's imread() to read input image files.

By default cv2.imread will convert a 16 bit, three channel image in a.tif to 8 bit as shown in the question.

Source: https://stackoverflow.com/a/18461475 (I can't find this officially documented in https://docs.opencv.org/4.10.0/d4/da8/group__imgcodecs.html, but it appears to be true.)

This means that all processing is performed on 8-bit image data, which is then passed to imwrite(), so the output image is 8-bit.

The imread() function accepts an IMREAD_UNCHANGED flag, which ensures that it is not converted to 8-bit: https://docs.opencv.org/4.10.0/d8/d6a/group__imgcodecs__flags.html#gga61d9b0126a3e57d9277ac48327799c80aeddd67043ed0df14f9d9a4e66d2b0708

However, simply adding that flag to stitching's imread() call (by changing line 113 of images.py from img = cv.imread(img_name) to img = cv.imread(img_name, cv.IMREAD_UNCHANGED)`) causes an error.

The error depends on the --detector option that is used:

orb:

stitching.stitching_error.StitchingError: No match exceeds the given confidence threshold.
                                 Do your images have enough overlap and common
                                 features? If yes, you might want to lower the
                                 'confidence_threshold' or try another
                                 'detector'.

sift:

cv2.error: OpenCV(4.10.0) /io/opencv/modules/features2d/src/sift.dispatch.cpp:512: error: (-5:Bad argument) image is empty or has incorrect depth (!=CV_8U) in function 'detectAndCompute'

brisk:

cv2.error: OpenCV(4.10.0) /io/opencv/modules/imgproc/src/color.simd_helpers.hpp:92: error: (-2:Unspecified error) in function 'cv::impl::{anonymous}::CvtHelper<VScn, VDcn, VDepth, sizePolicy>::CvtHelper(cv::InputArray, cv::OutputArray, int) [with VScn = cv::impl::{anonymous}::Set<3, 4>; VDcn = cv::impl::{anonymous}::Set<1>; VDepth = cv::impl::{anonymous}::Set<0, 2, 5>; cv::impl::{anonymous}::SizePolicy sizePolicy = cv::impl::<unnamed>::NONE; cv::InputArray = const cv::_InputArray&; cv::OutputArray = const cv::_OutputArray&]'
> Invalid number of channels in input image:
>     'VScn::contains(scn)'
> where
>     'scn' is 1

akaze:

cv2.error: OpenCV(4.10.0) /io/opencv/modules/stitching/src/exposure_compensate.cpp:564: error: (-215:Assertion failed) _image.type() == CV_8UC3 in function 'apply'

(Originally discovered in this discussion: https://github.com/OpenStitching/stitching/discussions/224)

mstenta commented 1 month ago

This provides a good explanation why 16-bit is important for editing: https://photographylife.com/8-bit-vs-16-bit-images

Even though 16 bits may sound like overkill, we saw here that it finds its use in post-processing images. But 8-bit per channel images take up much less file space, so it’s worth exporting your images, especially for the web, to 8-bit per channel to save space.

lukasalexanderweber commented 1 month ago

Thanks for reporting! This might be a wontfix since I'm not sure if this can be solved within stitching alone but would need changes in the stitching module of opencv as well..