# Bitwise\_and fails on smaller starting Mats

**URL:** <https://forum.opencv.org/t/bitwise-and-fails-on-smaller-starting-mats/10845>\
**Category:** Uncategorized\
**Created:** [November 6, 2022, 6:12pm UTC](https://forum.opencv.org/t/bitwise-and-fails-on-smaller-starting-mats/10845 "2022-11-06T18:12:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jordan](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@Jordan](https://forum.opencv.org/u/Jordan)\
**Post date:** [November 6, 2022, 6:12pm UTC](https://forum.opencv.org/t/bitwise-and-fails-on-smaller-starting-mats/10845/1 "2022-11-06T18:12:52Z")

</div>

Hi,  
I’m having trouble with bitwise\_and, I’m getting some strange artefacts in the output image from the following code:

```auto
// remove the green background from the piece so we can overlay pieces without overlap
Mat transparentPieceMat = new Mat(piece.pieceMat.rows(), piece.pieceMat.cols(), CvType.CV_8UC4);
Imgproc.cvtColor(piece.pieceMat, piece.pieceMat, Imgproc.COLOR_BGR2BGRA);

Core.bitwise_and(piece.pieceMat, piece.pieceMat, transparentPieceMat, piece.pieceChromaMat);

```

I’m getting results like the following:

 ![example](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/7/7ef85dbfb7104f1658903edbd48246aff341097a.jpeg)  
As you can see, smaller images, like the one along the top, do not work, they have this strange artefact left behind after the operation, whereas larger images, like the one below, work perfectly fine.

Both of these images undergo exactly the same processes, the only difference being that one of them is rotated more than another, giving it a larger image size.

What could be going wrong here? Cheers.

---

<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:** [November 6, 2022, 7:49pm UTC](https://forum.opencv.org/t/bitwise-and-fails-on-smaller-starting-mats/10845/2 "2022-11-06T19:49:06Z")

</div>

> [@Jordan](#):
>
> `pieceChromaMat`

[MRE](https://stackoverflow.com/help/minimal-reproducible-example) required. none of these variables mean anything to me. I prefer MREs in python because that doesn’t require an IDE or a build system or any of that excrement. if you’re dealing with C#, be prepared for some friction. C# is not officially supported by OpenCV. primary support should be given by whatever project gives you the bindings.

think about what you’re doing. think about which arguments you’re passing. think about what a “mask” does.

you put your question in the python subforum. that’s not python code. best leave it uncategorized.

[undeclared crosspost](https://meta.stackoverflow.com/a/266159):

[https://stackoverflow.com/questions/74338880/opencvc-artefacts-with-bitwise-and-with-small-images](https://stackoverflow.com/questions/74338880/opencvc-artefacts-with-bitwise-and-with-small-images)

---

<div class="post-metadata">

**Author:** ![Jordan](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@Jordan](https://forum.opencv.org/u/Jordan)\
**Post date:** [November 6, 2022, 10:47pm UTC](https://forum.opencv.org/t/bitwise-and-fails-on-smaller-starting-mats/10845/3 "2022-11-06T22:47:55Z")

</div>

Okay this is definitely a bug with the C# port, I’ve just compiled my app for Android to test and it’s working perfectly fine. I’ll file a bug report.

---

<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:** [November 6, 2022, 11:11pm UTC](https://forum.opencv.org/t/bitwise-and-fails-on-smaller-starting-mats/10845/4 "2022-11-06T23:11:32Z")

</div>

not necessarily. that could just be uninitialized memory (you did not initialize it to a specific value), and those pixels aren’t touched by the API call because you specify a mask argument.
