

Finally, define the mask variable as the third argument. Since the dimensions of the mask and the query are not the same. The first two arguments to the bitwise_and() function need to be the query image. Define your ROI and apply it to the mask (Making a circle or a rectangle with white color).Define a numpy array (zeros) equal to the shape of the image (on which masking is to be performed).We pass the value of our mask which will then allow us to only show the parts of the image which are active in the mask. This function takes an additional input called the mask.

Masking is generally done by using the bitwise AND function. bitXor = cv2.bitwise_xor(rectangle, circle) XOR operation on rectangle and circle Masking on Images Example Input(1,1) or Input(0,0) results in false(or 0) but Input(1,0) or Input(0,1) is true(or 1). The XOR, or exclusive or operation can be summarised as a function that returns true only if the two operands are different. bitNot = cv2.bitwise_not(circle) NOT Operation on ‘circle’ array iv) XOR Operation In this case, it converts all pixels with value 1 to 0 and vice versa. NOT is a single input operation that reverses the value of the input. bitOr = cv2.bitwise_or(rectangle, circle) OR Operation iii) NOT Operation bitAnd = cv2.bitwise_and(rectangle, circle) AND Operation ii) OR OperationĪ two-input operation that returns true only if one of the inputs is true (or 1). Output: Rectangle Circle i) AND OperationĪ two-input operation that returns true if and only if both the inputs are true (or 1). We will be performing AND, OR, NOT, XOR operations on these two arrays and see their output. We define numpy zero arrays and draw some shapes(rectangle and a circle) on them in white i.e.

These operations are useful to apply masking. OpenCV provides functions to perform arithmetic operations like AND, OR, XOR, and NOT on images. Refresher of Arithmetic Operations on Images in OpenCV
