Create a mask from a boundingRect in openCV

后端 未结 2 532
梦谈多话
梦谈多话 2021-01-03 17:10

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

How can I use those points and create a mask in openCV? that is, everythin

2条回答
  •  感动是毒
    2021-01-03 18:06

    Draw your rectangle with CV_FILLED option and invert it, like

    Rect boundRect(x,y,W,H);
    Mat mask(rows,cols,CV_8UC1,Scalar(0));
    rectangle(mask,boundRect,Scalar(255),CV_FILLED,8,0);
    bitwise_not(mask,mask);
    

    or in another way without using invert, just create a white image and then draw rectangle using CV_FILLED option but with black color(Scalar(0)).

    That is

    Rect boundRect(x,y,W,H);
    Mat mask(rows,cols,CV_8UC1,Scalar(255));
    rectangle(mask,boundRect,Scalar(255),CV_FILLED,8,0);
    

提交回复
热议问题