How to cut an image using a list of points using opencv -
i have image, , selection given list of points (a poly). goal image except selection transparent.
there 2 parts question: * how create mask list of points? * what's best way of creating new image using mask? thinking of cvcopy how can handle alpha channel?
i had simillar issue , solved in strange (not fast) way:
- extracted red, green , blue channels of image (using
extractchannel
). - do
bitwise_and
operation between each of channels , poygon mask image. - merged them (using
merge
function).
note if have one-channel image need bitwise_and
.
c++ code:
void createpoi(const mat& src, const mat& polymat, mat& dst) { vector<mat> array(3); (int i=0; i<3; i++) { extractchannel(src, array[i], i); bitwise_and(array[i], polymat, array[i]); } merge(array, dst); }
you can create polymat
mask using fillpoly
/fillconvexpoly
functions (depends on polygon). example:
mat polymat = mat::zeros(size, cv_8uc1); fillconvexpoly(polymat, polygon, scalar(255));
Comments
Post a Comment