Suppose you already have the fillConvexPoly
code working.
Change the code that calls fillConvexPoly
to instead create a "mask image", where each pixel contain an "opacity value".
The next step is to create a "texture image" of the same size as the bounding rectangle of the polygon you're planning to draw.
Then, use alpha blending to compose the texture image to the original image, using the mask image as opacity value.
New Output = (original input) * (1.0 - opacity) + (texture) * (opacity)
Read more about alpha blending at Wikipedia, or at OpenCV tutorial.
There are many tips and tricks needed to apply alpha blending - too many to be explained here. If you encounter a problem, try search around and ask questions if you're stuck.
One thing to remember is that the opacity value needs to be normalized to a maximum value of 1.0. If you are not sure, here are two "golden rules" you can use to verify the correctness of your implementation:
- When opacity is zero, the output value should be identical to the original input value.
- When opacity is at the maximum, the output value should be identical to the texture (or, "the other input") value.