Search code examples
opencvtiffbinary-image

OpenCV Save a Mat as Binary (1-bit depth) TIFF


Suppose we have a Mat after applying the OpenCv's Imgproc.adaptiveThreshold:

    Mat srcImage = ...;
    Mat binaryImage = new Mat();

    Imgproc.adaptiveThreshold(srcImage, binaryImage, 255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,
        Imgproc.THRESH_BINARY, THRESHOLD_BLOCK_SIZE, 10);

When I save the binaryImage using Highgui.imwrite:

boolean isOk = Highgui.imwrite("sample.tiff", binaryImage);

Everything is ok except that output TIFF is not actually a binary TIFF - it's a 8-bit depth image: enter image description here

What I'm trying to achieve is a similar to the below TIFF format with the 1-bit depth:

enter image description here

The question is how to do so with OpenCV?


Solution

  • Only 8-bit (or 16-bit unsigned) single-channel or 3-channel images can be saved by imwrite.