I have jpg image and polygon which I want to use as mask in this way: image inside this polygon should be displayed, and all outside this polygon should be 100% transparent. Now I achieved only first goal - I can display image inside polygon, but all outside of it is black:
cv::Mat image;
//load image
image.convertTo(image, CV_8UC3, 255.0);
std::vector<cv::Point> contour;
//load polygon
const cv::Point* elementPoints[1] = { &contour[0] };
int numberOfPoints = (int)contour.size();
cv::Mat mask = cv::Mat::zeros(image.size(), image.type());
cv::fillPoly(mask, elementPoints, &numberOfPoints, 1, cv::Scalar( 255, 255, 255), 8);
cv::Mat dstImage = cv::Mat::zeros(image.size(), image.type());
cv::bitwise_and(image, mask, dstImage);
imwrite("test.jpg", dstImage);
I know that I need to use alpha channel, but it's unclear what I need to do next and how to implement this.
How can I get transparent background outside the mask?