Skip to content

Crop image by polygon area #2770

Answered by akuzyagin
akuzyagin asked this question in Q&A
Discussion options

You must be logged in to vote

I found the answer to my question, the following code allows you to cut out a polygon from an image:

// create polygon with coordinates for mask
MatOfPoint points = new MatOfPoint(
      new Point(100, 400),
      new Point(500, 350),
      new Point(700, 300),
      new Point(1000, 500),
      new Point(1000, 800),
      new Point(700, 800),
      new Point(500, 800),
      new Point(100, 400)
);

// draw shape with size of frame from original video and black fill
final Scalar colorBlack = new Scalar(0, 0, 0);
Mat mask = new Mat(frame.rows(), frame.cols(), CvType.CV_8UC3, colorBlack);

// fill polygon of white color on the black fill
final Scalar colorWhite = new Scalar(255, 255, 255);
…

Replies: 3 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@akuzyagin
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by akuzyagin
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants