Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support reading/writing 16-bit images #226

Open
mstenta opened this issue Aug 7, 2024 · 2 comments
Open

Support reading/writing 16-bit images #226

mstenta opened this issue Aug 7, 2024 · 2 comments
Labels
enhancement New feature or request OpenCV bindings A bug or something that wontfix if the OpenCV bindings are not modified

Comments

@mstenta
Copy link
Contributor

mstenta commented Aug 7, 2024

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: #224)

@mstenta
Copy link
Contributor Author

mstenta commented Aug 7, 2024

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 lukasalexanderweber added wontfix This will not be worked on needs investigation Collect and attach more details (build flags, stacktraces, input dumps, etc) OpenCV bindings A bug or something that wontfix if the OpenCV bindings are not modified labels Aug 7, 2024
@lukasalexanderweber
Copy link
Member

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..

@lukasalexanderweber lukasalexanderweber added enhancement New feature or request and removed wontfix This will not be worked on labels Aug 7, 2024
@lukasalexanderweber lukasalexanderweber removed the needs investigation Collect and attach more details (build flags, stacktraces, input dumps, etc) label Nov 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request OpenCV bindings A bug or something that wontfix if the OpenCV bindings are not modified
Projects
None yet
Development

No branches or pull requests

2 participants