Reputation: 952
I'm trying to do something I've seen done before and I thought would be very simple, but you know how that goes.
Consider the code below:
vector<Point> polypoints{topleft,topright,bottomright,bottomleft};
vector<vector<Point>> arrayofpolypoints{polypoints};
fillPoly(frame, arrayofpolypoints, color3, LINE_8 ,0,Point(0,0));
I take 4 given points and draw/fill a polygon on a Mat image. Simple, right? Now I want this polygon to have transparency, or shade, the area, not make it solid (or shaded with a solid border would be nice). But it seems there's no function in OpenCV that already does this, so now I have to invent something. Any ideas?
I'm thinking it might be better to draw the polygon on another blank Mat image, and then step through every pixel and black out every other one (or some sort of ratio). Then I can somehow combine the initial frame and the new Mat. Does that make sense?
Upvotes: 1
Views: 766
Reputation: 952
I ended up doing the following:
vector<Point> polypoints{topleft,topright,bottomright,bottomleft};
vector<vector<Point>> arrayofpolypoints{polypoints};
destimage = Scalar::all(0);
fillPoly(destimage, arrayofpolypoints, color3, LINE_8 ,0,Point(0,0));
double transparency = 0.5;
for (int i = 0; i < frame.cols; i++) {
for (int j = 0; j < frame.rows; j++) {
Vec3b &intensity = frame.at<Vec3b>(j, i);
Vec3b &intensityoverlay = destimage.at<Vec3b>(j, i);
for(int k = 0; k < frame.channels(); k++) {
uchar col = intensityoverlay.val[k];
if (col != 0){
intensity.val[k] = (intensity.val[k]*(1-transparency) + (transparency*col));
}
}
}
}
Upvotes: 1