I think the proper answer is to code it up using libtiff
. I am no expert on libtiff
but this seems to work for the cases I have tried:
#include <iostream>
#include <opencv2/opencv.hpp>
extern "C" {
#define uint64 uint64_hack_
#define int64 int64_hack_
#include "tiffio.h"
#undef uint64
#undef uint64
}
using namespace cv;
bool tiffwrite(const char* name, Mat image){
TIFF* tif = TIFFOpen(name, "w");
if(tif==NULL){
std::cerr << "ERROR: Unable to open output file";
return false;
}
int width = image.cols;
int height = image.rows;
// We expect 4 channels
int channels = image.channels();
assert(channels==4);
// We expect 8-bit data
int bytespersample = image.elemSize1();
assert(bytespersample == 1);
TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, channels);
TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_LZW);
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8*bytespersample);
TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 8);
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_SEPARATED);
// Write to file, 1 scanline at a time
for (int row = 0; row < height; row++) {
// std::cerr << "DEBUG: Writing row " << row << std::endl;
// Get pointer to row
cv::Vec4b* ptr = image.ptr<cv::Vec4b>(row);
int ret = TIFFWriteScanline(tif, (unsigned char*)ptr, row, 0);
if (ret == -1) {
TIFFClose(tif);
return false;
}
}
TIFFClose(tif);
return true;
}
int
main(int argc,char*argv[])
{
// Create a 4-channel CMYK yellow image
Mat4b image(480, 640, Vec4b(0,0,255,0));
// Write as CMYK TIFF
tiffwrite("result.tif",image);
}
I have also come up with a couple of hacks you may feel like trying.
First and simplest hack, save with OpenCV as usual as a JPEG, TIFF or PNG, then use system()
function to shell out to ImageMagick and convert to a CMYK TIFF from there.
cv2::imwrite("intermediate.jpg",image);
system("...");
And inside there you put the equivalent of:
convert intermediate.jpg -colorspace CMYK result.tif
Or if using v7 of ImageMagick
magick intermediate.jpg -colorspace CMYK result.tif
Second hack, if you don't want to install and depend on ImageMagick... there is a tool called raw2tiff that ships with libtiff
. You can pass it a raw file (no headers) and it will convert it to a TIFF for you. So you would basically use OpenCV to write the raw C bytes, then the raw M bytes, then the raw Y bytes and the raw K bytes into a single file and use system()
to call that utility. I used this command line:
raw2tiff -w 1024 -l 768 -b 4 -i band -c lzw -p cmyk CMYK result.tif
So my image was 1024 pixels wide, 768 pixels tall, consisting of 4 bands in file CMYK
and I wanted an LZW-compressed TIFF called result.tif
. That opens to look like this in Photoshop:

I made synthetic C, M, Y and K channels with ImageMagick like this:
#!/bin/bash
# Make raw and viewable C channel
convert -size 1024x768 gradient: -depth 8 -write gray:C C.png
# Make raw and viewable M channel
convert -size 1024x768 xc:black -depth 8 -write gray:M M.png
# Make raw and viewable Y channel
convert -size 768x1024 gradient: -rotate 90 -depth 8 -write gray:Y Y.png
# Make raw and viewable K channel
convert -colorspace RGB -size 400x400 gradient: -gravity center -background black -extent 1024x768 -depth 8 -write gray:K K.png
# Stack side-by-side, just for viewing
convert C.png M.png Y.png K.png +append CMYK.png
# Concatenate them all together into single file "CMYK" as input file for "raw2tiff"
cat C M Y K > CMYK
Here are the 4 images, appended side-by-side with C on the left, then M, then Y and finally with K on the right.

In your case, you would get the data
pointer to your Mat
and simply write height*width bytes for each channel to a binary file, rather than use ImageMagick. You can see how to do that in the libtiff
example at the top of my answer.
Keywords: TIFF, TIF, image processing, libtiff, CMYK, colour separation, color separation, Photoshop.
Looking at the image, I guess I should have negated (inverted) the image first, but that is not too hard.