OpenCV supports RGBA images, which you can create with mixchannels or split and merge to combine images with your grayscale mask. Hope this is what you are looking for!
Using this method, you can combine a grayscale mask with your image as follows:
cv::Mat gray_image, mask, rgba_image; std::vector<cv::Mat> result; cv::Mat image = cv::imread(image_path); cv::split(image, result); cv::cvtColor(image, gray_image, CV_BGR2GRAY); cv::threshold(gray_image, mask, 128, 255, CV_THRESH_BINARY); result.push_back(mask); cv::merge(result, rgba_image); imwrite("rgba.png", rgba_image);
Keep in mind that you cannot view RGBA images using cv::imshow as described in read-rgba-image-opencv , and you cannot save the image as jpeg, because this format does not support transparency. It seems that you can combine channels using cv::cvtcolor , as shown in opencv-2-3-convert-mat-to-rgba-pixel-array
cwadding
source share