# Bitwise operation

**URL:** <https://forum.opencv.org/t/bitwise-operation/10007>\
**Category:** Python\
**Created:** [August 30, 2022, 11:21am UTC](https://forum.opencv.org/t/bitwise-operation/10007 "2022-08-30T11:21:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sayal](https://avatars.discourse-cdn.com/v4/letter/s/3bc359/32.png) [@sayal](https://forum.opencv.org/u/sayal)\
**Post date:** [August 30, 2022, 11:21am UTC](https://forum.opencv.org/t/bitwise-operation/10007/1 "2022-08-30T11:21:02Z")

</div>

hello every one  
I want to extract one piece of a shape in a picture.  
the solution for this is to make a black image and white rectangle in black image.  
then the mask image and original image get in and operator to get out the piece of image.  
but I get get Error “cv2.bitwise\_and(img, blk\_copy)”.

my code:  
np\_img = np.array(img)  
black = np.zeros(img.shape)  
white = np.ones(img.shape)  
blk\_copy = black.copy()  
cv2.rectangle(blk\_copy , (301 , 350) , (60, 110) , (255,255,255) , -1)  
new\_img = cv2.bitwise\_and(img, img, mask = blk\_copy)

cv2.imshow(“rec” , blk\_copy)  
cv2.imshow(“image” , new\_img)  
cv2.waitKey(0)

can anyone help me.  
thanks.

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [August 30, 2022, 11:23am UTC](https://forum.opencv.org/t/bitwise-operation/10007/2 "2022-08-30T11:23:48Z")

</div>

> [@sayal](#):
>
> Error “cv2.bitwise\_and(img, blk\_copy)”.

**what** error specifically? why don’t you say?

---

<div class="post-metadata">

**Author:** ![sayal](https://avatars.discourse-cdn.com/v4/letter/s/3bc359/32.png) [@sayal](https://forum.opencv.org/u/sayal)\
**Post date:** [August 30, 2022, 11:35am UTC](https://forum.opencv.org/t/bitwise-operation/10007/3 "2022-08-30T11:35:08Z")

</div>

cv2.error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\core\src\arithm.cpp:230: error: (-215:Assertion failed) (mtype == CV\_8U || mtype == CV\_8S) && \_mask.sameSize(\*psrc1) in function 'cv::binary\_op

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [August 30, 2022, 12:03pm UTC](https://forum.opencv.org/t/bitwise-operation/10007/4 "2022-08-30T12:03:52Z")

</div>

> [@sayal](#):
>
> black = np.zeros(img.shape)  
> white = np.ones(img.shape)

this will produce `int32` arrays ! you have to specify the dtype explicitly:

```
black = np.zeros(img.shape, img.dtype)

```

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [August 30, 2022, 2:24pm UTC](https://forum.opencv.org/t/bitwise-operation/10007/5 "2022-08-30T14:24:25Z")

</div>

and it will (probably) produce 3-dimensional, 3-channel arrays (`img = imread()?`). that is unsuitable as a mask.
