从openCV中的boundingRect创建遮罩 [英] Create a mask from a boundingRect in openCV

查看:201
本文介绍了从openCV中的boundingRect创建遮罩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我基于某些点获得了boundingRect并将其存储到Rect对象中.

Lets say I was given a boundingRect based on some points and stored it into a Rect object.

如何在openCV中使用这些点并创建遮罩?也就是说,边界矩形之外的所有内容都会被屏蔽(或设置为白色)

How can I use those points and create a mask in openCV? that is, everything outside the bounding rectangle is masked (or set white)

我尝试了几种不同的方法,并且能够使用凸面壳和多边形填充圆点来使其工作,但似乎无法使其与boundingRect一起使用

I've tried several different methods and was able to get it to work using a convexHull and fillign with a polygon but can't seem to get it to work with the boundingRect

推荐答案

您可以调用

You can call fillConvexPoly() by passing the four end points of the bounding Rect.

// assume all four end points are stored in "vector<Point> roi_vertices" already
// the order of the vertices don't matter

Mat mask = Mat(height, width, CV_8UC1, Scalar(0));

// Create Polygon from vertices
vector<Point> roi_poly;
approxPolyDP(roi_vertices, roi_poly, 1.0, true);

// Fill polygon white
fillConvexPoly(mask, &roi_poly[0], (int)roi_poly.size(), 255, 8, 0);


PS :以上方法也可以为任何(凸)多边形生成蒙版.


P.S.: the above method will also work for generating masks for any (convex) polygons.

这篇关于从openCV中的boundingRect创建遮罩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆