Skip to content

pil2cv: allow (and drop) alpha channels on input imgs - #168

Closed
bertsky wants to merge 2 commits into
qurator-spk:mainfrom
bertsky:allow-input-alpha
Closed

pil2cv: allow (and drop) alpha channels on input imgs#168
bertsky wants to merge 2 commits into
qurator-spk:mainfrom
bertsky:allow-input-alpha

Conversation

@bertsky

@bertsky bertsky commented Jun 12, 2025

Copy link
Copy Markdown
Contributor

The error I got by calling cvtColor as it was:

OpenCV(4.8.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<1>; VDcn = cv::impl::{anonymous}::Set<3, 4>; 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 2

Removing the alpha channel in advance avoids the problem.

But perhaps we should not naively drop the channel, but use it (somehow) to mask the image? (I.e. for segmentation: no layout analysis in fully transparent zones, for binarization: pure background)

@cneud
cneud requested a review from vahidrezanezhad June 13, 2025 07:14
@vahidrezanezhad

vahidrezanezhad commented Aug 28, 2025

Copy link
Copy Markdown
Member

@bertsky @kba @cneud Even without any changes, including this PR, Eynollah (which I tested using my latest PR ) works correctly on this image with an alpha channel
. It seems this issue happens with other images with alpha channel. can you provide me images with alpha channel that this issue happens @bertsky ?

@kba

kba commented Sep 26, 2025

Copy link
Copy Markdown
Contributor

If we decide to move ahead with this, we should backport that to OCR-D/core whence I took it originally.

@bertsky

bertsky commented Jul 28, 2026

Copy link
Copy Markdown
Contributor Author

Ok, so to revisit this...

The image @vahidrezanezhad used, which is data/gutachten/data/IMG/IMG_1.tif from OCR-D's test assets repo, actually is a two-page TIFF with a simple RGB up front (which is why it is well-behaved regarding OCR-D and Eynollah) and an RGBA (but actually RGBa, i.e. premultiplied) as second layer. Extracting and running with the latter image in lieu of the original one does work, but produces bad results due to the wrong image metadata (RGBA instead of RGBa):

eynollah-rgba-cli-page eynollah-rgba-ocrd-page

But depending on the base layer (gray or palette or rgb), the OCR-D interface (i.e. pil2cv()) can still fail with the above stacktrace. As it does for this example image.

Hence my latest commit on #222 as a stripped down version of this PR.

The standalone CLI uses cv2.imread with default flag cv2.IMREAD_COLOR_BGR, which does work in many cases already.

I am not so sure anymore what my original example was, but will close for now.

@bertsky bertsky closed this Jul 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants