Ask Your Question
0

How to fill a polygon with image OpenCV (textured polygon)

asked 2014-11-14 19:46:56 -0600

epsilon gravatar image

I have 4 points which will become a polygon, but it is not a rectangle. It is some convex polygon which shape is constantly changing. With Core.fillConvexPoly you can fill it with color. I would like to place there some image. So that it will be textured.

I am using OpenVC for Android. Does anyone have any insight?

Thank you!

-epsilon

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
2

answered 2014-11-15 23:26:47 -0600

rwong gravatar image

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.
edit flag offensive delete link more

Comments

@rwong Thank you for the response. I am using OpenCV for Android. Do you know where are the .cpp CV files stored? (in order to change the fillConvexPoly) I am able to find just the .h and .hpp files in the sdk folder (native subfolder).

epsilon gravatar imageepsilon ( 2014-11-17 13:28:53 -0600 )edit
1

answered 2014-11-15 04:09:28 -0600

updated 2014-11-15 05:23:52 -0600

One idea:

Add a fourth point in the middle of the polygon (how you define the "middle of a polygon" is up to you :) and connect the new point with all others. Your polygon now consists of four triangles. (Do the same for your source image) Now your task is to only map each triangle in your source image to the corresponding triangle in the polygon. This can be done via cv::getAffine and cv::warpAffine. Tutorial is here: http://docs.opencv.org/doc/tutorials/imgproc/imgtrans/warp_affine/warp_affine.html

edit flag offensive delete link more

Question Tools

Stats

Asked: 2014-11-14 19:46:56 -0600

Seen: 3,725 times

Last updated: Nov 15 '14