I am working on a C++ project where I need to stitch together multiple 16-bit images which overlap horizontally and vertically, and then save the stitched image. Is there a library which I can use to accomplish this task?

I have tried to use an OpenCV Stitcher object to no success because the following exception was thrown:

terminate called after throwing an instance of 'cv::Exception'
  what():  OpenCV(4.0.1) /home/rar/opencv_build/opencv/modules/stitching/src/exposure_compensate.cpp:317: error: (-215:Assertion failed) _image.type() == CV_8UC3 in function 'apply'

Here is my code snippet which performs the stitching:

// vector containing 16-bit images
std::vector<cv::UMat> images;

cv::Ptr<cv::Stitcher> stitcher = cv::Stitcher::create();
cv::UMat stitched;
cv::Stitcher::Status status = stitcher->stitch(images, stitched);  // Performs the image stitching and outputs a status

Other people seem to have faced the same problem as I have [https://answers.opencv.org/question/34795/stitching-16bits-greyscale-images/]

1 Answer 1


There are at least 2 types of 16-bit images: Grayscale and RGB.

The company I work for has an imaging SDK that supports both types and can stitch 2 (or more) images using C/C++ code like this:

BITMAPHANDLE smallBmp1 = {0}, smallBmp2 = {0}, bigBmp = {0};
int ByteOrder = ORDER_GRAY; // change to ORDER_BGR if images are color instead of grayscale
//load 2 input images
L_LoadBitmap(imageFile1, &smallBmp1, sizeof BITMAPHANDLE, 16, ByteOrder, 0, 0);
L_LoadBitmap(imageFile2, &smallBmp2, sizeof BITMAPHANDLE, 16, ByteOrder, 0, 0);
//ensure both bitmaps have same view perspective (orientation)
L_ChangeBitmapViewPerspective(0, &smallBmp1, sizeof BITMAPHANDLE, TOP_LEFT);
L_ChangeBitmapViewPerspective(0, &smallBmp2, sizeof BITMAPHANDLE, TOP_LEFT);
//prepare big image to hold result
L_CreateBitmap(&bigBmp, sizeof BITMAPHANDLE, TYPE_CONV, 
   BITMAPWIDTH(&smallBmp1) + BITMAPWIDTH(&smallBmp2),       //width is total width
   max(BITMAPHEIGHT(&smallBmp1), BITMAPHEIGHT(&smallBmp2)), //height is larger of the two
   16, ByteOrder, 0, TOP_LEFT, 0, 0);
//stitch 2 images on big image
L_CombineBitmap(&bigBmp, 0, 0, BITMAPWIDTH(&smallBmp1), BITMAPHEIGHT(&smallBmp1), &smallBmp1, 0, 0, CB_OP_ADD | CB_DST_0 | CB_RAWCOMBINE, 0);
L_CombineBitmap(&bigBmp, BITMAPWIDTH(&smallBmp1), 0, BITMAPWIDTH(&smallBmp2), BITMAPHEIGHT(&smallBmp2), &smallBmp2, 0, 0, CB_OP_ADD | CB_DST_0 | CB_RAWCOMBINE, 0);
//save the result
L_SaveBitmap(outputFile, &bigBmp, FILE_TIFLZW, 16, 0, 0);

The function that performs the actual stitching is documented here.

If you would like to try the toolkit, there’s a free evaluation edition you can download here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.