{"record":{"id":"46770d05818fc16b","repo":"opendatalab/MinerU","slug":"the-channel-channel-of-the-img-is-not-in-1-2","errorCode":null,"errorMessage":"The channel({channel}) of the img is not in [1, 2, 3, 4]","messagePattern":"The channel\\((.+?)\\) of the img is not in \\[1, 2, 3, 4\\]","errorType":"exception","errorClass":"LoadImageError","httpStatus":null,"severity":"error","filePath":"mineru/model/table/rec/unet_table/utils.py","lineNumber":131,"sourceCode":"    def convert_img(self, img: np.ndarray):\n        if img.ndim == 2:\n            return cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)\n\n        if img.ndim == 3:\n            channel = img.shape[2]\n            if channel == 1:\n                return cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)\n\n            if channel == 2:\n                return self.cvt_two_to_three(img)\n\n            if channel == 4:\n                return self.cvt_four_to_three(img)\n\n            if channel == 3:\n                return cv2.cvtColor(img, cv2.COLOR_RGB2BGR)\n\n            raise LoadImageError(\n                f\"The channel({channel}) of the img is not in [1, 2, 3, 4]\"\n            )\n\n        raise LoadImageError(f\"The ndim({img.ndim}) of the img is not in [2, 3]\")\n\n    @staticmethod\n    def cvt_four_to_three(img: np.ndarray) -> np.ndarray:\n        \"\"\"RGBA → BGR\"\"\"\n        r, g, b, a = cv2.split(img)\n        new_img = cv2.merge((b, g, r))\n\n        not_a = cv2.bitwise_not(a)\n        not_a = cv2.cvtColor(not_a, cv2.COLOR_GRAY2BGR)\n\n        new_img = cv2.bitwise_and(new_img, new_img, mask=a)\n        new_img = cv2.add(new_img, not_a)\n        return new_img\n","sourceCodeStart":113,"sourceCodeEnd":149,"githubUrl":"https://github.com/opendatalab/MinerU/blob/4fe4bde114a23ee5dd637eae99b767f4669bf58c/mineru/model/table/rec/unet_table/utils.py#L113-L149","documentation":"convert_img handles 3-D arrays only with 1, 2, 3, or 4 channels (gray, gray+alpha, RGB, RGBA conversions to BGR). A 3-D array with any other last dimension (5+, 0) cannot be interpreted as an image and raises LoadImageError naming the offending channel count.","triggerScenarios":"Passing multi-spectral arrays (H, W, 5+), preprocessed float batches with a stray leading axis flattened wrongly, or arrays where channel data was concatenated along the wrong axis.","commonSituations":"Numpy stacking bugs (np.concatenate along axis=2 instead of a new axis), feeding model feature maps instead of images, malformed crops from upstream code.","solutions":["Inspect arr.shape before loading; fix the stacking/concatenation axis upstream.","Slice to the first 3 channels if extra channels are accidental: img = img[..., :3].","Ensure you pass pixel images (H, W, C) not feature maps."],"exampleFix":"# before\nimg = np.concatenate([bgr, alpha, extra], axis=2)  # 5 channels\nimg = loader(img)\n\n# after\nimg = bgr  # keep (H, W, 3); compose alpha separately\nimg = loader(img)","handlingStrategy":"validation","validationCode":"if img.ndim == 3 and img.shape[2] not in (1, 2, 3, 4):\n    img = img[..., :3]  # or raise with context","typeGuard":"def has_valid_channels(img: np.ndarray) -> bool:\n    return img.ndim != 3 or img.shape[2] in (1, 2, 3, 4)","tryCatchPattern":"try:\n    img = loader(raw)\nexcept LoadImageError as e:\n    if 'channel' in str(e):\n        img = loader(raw[..., :3])\n    else:\n        raise","preventionTips":["Assert arr.shape right after every crop/stack operation.","Prefer cv2.merge over np.concatenate for adding channels.","Never feed feature maps into image loaders."],"tags":["image-processing","numpy","validation","table-recognition"],"backgroundTag":null,"analyzedSha":"4fe4bde114a23ee5dd637eae99b767f4669bf58c","analyzedAt":"2026-08-14T21:29:18.456Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}