Draw Filled Shape from Four Points

≯℡__Kan透↙ 提交于 2019-12-12 18:12:43

问题


I've got four points (taken from a playing card after image processing) and I want to cover the surface of that card with a green mask. So I'm searching for a way to draw a shape that essentially has straight lines between those four points and then fills the middle of the shape with green. I was trying to use OpenCV for the task, but the rectangle method takes only two points (so unless the card is perfectly aligned the mask covers surrounding area or not enough of the card), and the fillPoly method had no effect (although I can post the code if need be). Any suggestions for a method to do this job?


回答1:


an addition to @Miki 's answer:

you can also use fillConvexPoly to get same result

Mat img = Mat::zeros( 200, 200, CV_8UC3 );
Point pts[4] = {Point(10,20),Point(60,20),Point(180,130),Point(60,150)};
fillConvexPoly( img, pts, 4, Scalar(0,255,0) );




回答2:


You can use drawContours, but you need to take care of the order of points. You can do this computing the convex hull on your points.

With convex hull:

Without convex hull:

Here the C++ code, but you can easily port to Java since it's all OpenCV function calls:

#include <opencv2\opencv.hpp>
#include <vector>
using namespace cv;

int main(void)
{
    // Some image
    Mat3b img(200, 200, Vec3b(0, 0, 0));

    vector<Point> pts{Point(10,20), Point(60,20), Point(60,150), Point(180, 130)};
    vector<Point> hull;
    convexHull(pts, hull);

    vector<vector<Point>> dummy(1, pts);
    drawContours(img, dummy, 0, Scalar(0,255,0), CV_FILLED);

    imshow("Result", img);
    waitKey();

    return 0;
}



回答3:


Since it's a rectangle you can find the length and breadth from the points and angle of tilt, then draw a rectangle with those specific length and breadth and tilt the rectangle.



来源:https://stackoverflow.com/questions/32997224/draw-filled-shape-from-four-points

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!