I’m writing a project in C++ using OpenCV.
I have coordinates of 4 vertices of tetragon stored in vector of Point2f.
I want to access all the pixels of this tetragon (to check what is the percentage of black pixels in its area).
Any ideas how to do it?
P.S. This tetragon is NOT a rectangle-that would be piece of cake.
EDIT:
To make things clear: I would like to iterate through every pixel inside tetragon (given by 4 vertices) to access its RGB value.
This kind of operation is akin to rasterizing, such as used in OpenGL to draw actual polygons, so FYI any information on rasterization can also help you. Using OpenGL to rasterize would be more efficient since graphics card have hardware just for that, but since your question is about OpenCV, let’s use functions available in OpenCV.
First, we would call cvFillConvexPoly() or cvFillPoly() with your vertices to create a mask image, then we could call whatever function you are interested to use on your original image plus the mask. This way, when looping over the pixels, we only need to consider pixels whose corresponding mask values are not zero.