How to “zero” everything within a masked part of an image in OpenCV

前端 未结 3 1371
春和景丽
春和景丽 2021-02-19 13:27

If I have an image (IplImage 8-bit) and a binary mask (which is also an 8-bit IplImage of the same size, where every pixel has a value of either 0 or 255), how can I make every

相关标签:
3条回答
  • 2021-02-19 13:36

    You can simply use bitwise_and() function.

    Check the documentation.

    0 讨论(0)
  • 2021-02-19 13:37

    Simplest way, with 'Mat img' (image to be masked, input) and 'Mat masked' (masked image, output):

      img.copyTo(masked, mask)
    

    where 'Mat mask' is a matrix not necessarily binary (copyTo considers elements with zero value). Masked can be of any size and type; it is reallocated if needed.

    See the doc.

    0 讨论(0)
  • 2021-02-19 13:39

    Multiply or bit-and the mask with the image. There are some OpenCV functions for that, but I do not know their names for the C interface.

    in C++:

    Mat image, mask;
    
    image = image * mask;
    // or 
    image = image & mask;
    
    0 讨论(0)
提交回复
热议问题