title: OpenCV bitwise_and
body: <p>I'm very newbie in the OpenCV programming.
  I'm learning about the image bitwise operation.
   I'm following this example <a href="http://docs.opencv.org/trunk/d0/d86/tutorial_py_image_arithmetics.html" rel="nofollow noreferrer">http://docs.opencv.org/trunk/d0/d86/tutorial_py_image_arithmetics.html</a>, but I had a very strange result.<br>
  These are the input images:<br>
  <a href="https://i.stack.imgur.com/5wJ2I.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5wJ2I.png" alt="enter image description here"></a><br>
  <a href="https://i.stack.imgur.com/2lbOe.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2lbOe.jpg" alt="enter image description here"></a></p>
  
  <p>This is the python code:</p>
  
  <pre><code># Load two images
  img1 = cv2.imread('messi5.jpg')
  img2 = cv2.imread('opencv-logo.png')
  # I want to put logo on top-left corner, So I create a ROI
  rows,cols,channels = img2.shape
  roi = img1[0:rows, 0:cols ]
  # Now create a mask of logo and create its inverse mask also
  img2gray = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)
  ret, mask = cv2.threshold(img2gray, 10, 255, cv2.THRESH_BINARY)
  mask_inv = cv2.bitwise_not(mask)
  # Now black-out the area of logo in ROI
  img1_bg = cv2.bitwise_and(roi,roi,mask = mask_inv)
  # Take only region of logo from logo image.
  img2_fg = cv2.bitwise_and(img2,img2,mask = mask)
  # Put logo in ROI and modify the main image
  dst = cv2.add(img1_bg,img2_fg)
  img1[0:rows, 0:cols ] = dst
  cv2.imshow('res',img1)
  cv2.waitKey(0)
  cv2.destroyAllWindows()
  </code></pre>
  
  <p>This is the expected result:  </p>
  
  <p><a href="https://i.stack.imgur.com/Yslp6.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Yslp6.jpg" alt="enter image description here"></a></p>
  
  <p>And this is the result of the code on my PC:<br>
  <a href="https://i.stack.imgur.com/Gk6cd.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Gk6cd.jpg" alt="enter image description here"></a></p>
  
  <p>How is it possible? Same pitcure, same code and so different result.
  Thank you to all</p>
  
  <p>These are the intermediate image:<br>
  <a href="https://i.stack.imgur.com/ZvHMP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ZvHMP.png" alt="mask"></a>  </p>
  
  <p><a href="https://i.stack.imgur.com/tYeOV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tYeOV.png" alt="inverse mask"></a></p>
  
  <p><a href="https://i.stack.imgur.com/B0CAF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/B0CAF.png" alt="img1_bg"></a><br>
  <a href="https://i.stack.imgur.com/QkyW7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QkyW7.png" alt="im2_fg"></a></p>