Hey, guys, welcome to the next video on open. CBT tool for beginners using Python in this video, we will talk about Bitwise operations on images using Python and OpenCV so bitwise operations can be very useful when working with masks, masks are binary images that indicates the pixel in which an operation is to be performed. So let’s see how we can perform bitwise operations on images so to start with. I have here one image, which is image, underscore one dot PNG file. And let me show you the image. Also, so this image is half black and half white. Now the second image I’m creating using numpy so first of all, I have used N P dot zeroes, and I’m just creating this image with the same dimension. As this image underscore. One is having so 250 comma 500 is the dimension of this image and the number of channels are 3 and this code is going to create a black image as you might already know from our previous videos now. This code is just creating a white rectangle inside the black image, which we got from numbers, zeroes array. Okay, so this is the dimension of the rectangle inside your black image and the color of the rectangle will be white because this is 255 comma 255 comma 255 and we are taking thickness as minus 1 That means your rectangle will be filled with white color now here. I’m just showing both the images using imshow method and this code. You might already know what this is doing from our previous videos. So let me run this code and let’s see what happens first of all. So when we run this code, you will see first image. Is this one which we have created using the numpy is zero, so this is. I am g1 And this is the black image, and we are just creating a white rectangle inside this number zeroe’s image, and this is the second image, which is half black and half white. Now we want to perform some bit wise operations on these two images, so let’s see how we can perform these bitwise operations on these two images so to perform these bitwise operations. We have some methods inside the Opencv library, so the first method will be bit and so I am going to just create a variable called a bit and with let’s say like this. And the method inside Opencv is CB 2 dot bit wise underscore, and so this bitwise underscore and takes several arguments as you might see here. The source of the first image, the source of the second image and the destination, which is none by default and the mask, which is also optional, so we are going to just provide our images here, so I’m going to provide the I am g2 here first of all as the first argument and the second image will be, I am g1 And once we perform this bitwise and operation on these two images, we are going to show the result in the form of window, so I’m going to create one more window, Which is CB 2 Dot. I am show and I’m going to name this window. As let’s say bit and and the second argument will be our variable bit and which we got from the operation bitwise and on these two images. So let’s run this code once again and let’s see what happens so now. As a result, we have the third image, so let me just open all the images. So this is our first image. This is the second image, and the last one is the result, which is the bit and operation on these two images. So now you might already know how the logical end works, but those of you who might not know how the logical end works. Let me show you the truth table of logical end. So this is the truth table of logical end, and if the input a and input B B is 0 then we get the result 0 okay. If input a and input B, either of them is 0 then also we get zeroes, right, The result 1 we will only get when both the sources are 1 so a and B are 1 then only we will get a 1 in case of end logic, so same and logic will work here. So this is the zeroe’s array, right, so we have created this black region from the zeros so here in these images, black is performing as zeroes, and the white part is performing as one, so when 0 and 0 the result will be 0 right. So from this truth table we have seen when the input is 0 and 0 The result is 0 same here. We are seeing, so when the image is black and black, we get the result black. When the input is white and black, This means 0 and 1 The result will be once again 0 using the logical end, but when the input will be white and white, that means 1 one, the result will be wide. That means the one. Okay, so the only reason region white here is the result of this white and this white and the resulting image you can see here and all the other part is black because the end operation on zero and zero is zero and zero and one is also zero. So this is how a bitwise end works. Let’s see how bitwise or and other operation works so. I’m going to just comment this code. And now we are going to just create the bitwise or operation, so for that, I’m going to just instead of writing bit and I’m going to just write bit or and instead of bitwise and we are going to just write bit wise or here, and then we will simply call this image using. I am sure method, so we are just calling here bit or now. Let’s run the code once again, and let’s see the result so you can see the result here, so let’s see the truth table so in the logical or if only one input is 1 then the result will be 1 so either. A or B is 1 or both are 1 then the result will be Bar 1 and if both inputs are 0 then the result will be 0 so same you will be able to see here. So when the first source and the second source is 0 the result is 0 but when the first source and second source is 1 or white, the result is white when the first source and the second source is 0 and 1 or black or white, The result is once again 1 or white here. OK, so this is how the logical or work on the image. Now, let’s see how the! XR operation work on those images. I’m going to once again. Comment this code and at this time. I’m going to just perform the XOR operation on these two images, and now we are going to run this code once again, and you will see this kind of result so once again, let’s see how the XOR logic works. So when both the inputs are 0 or both, the inputs are 1 then we will get the 0 and if either a or B is 1 then only we will get the result 1 so same you will be able to see here. So when both first and second source is 0 the result is 0 when both the first and second source is 1 You can see here and here. The result is once again 0 here, right, But when the input is 0 and one result will be 1 and in this case, also, the black and white will result in the white image, which is the logical. Xor operation. So once again, lets. Close this and now let me show you how the not operation work so. I’m going to just comment this code, and then I’m going to just use the bitwise, not so here bit not. Let’s say we will perform the bit knot on the first image and the second image, so I’m going to just write bit not on the first image because it only takes one argument bit not is just the opposite of the source, So if you get the input 0 then the result will be 1 If you have the input 1 the result will be 0 so the opposite of the input. So let’s perform this operation on Image 1 an image 2 and let’s comment this code and we are going to just show these two result. Windows using the. I am show method and also I need to change this name. Otherwise, we will face problems and here also. I haven’t changed the name of these. I am sure windows, so let’s change the name of these windows and let’s run the code once again. And now you will get these results so we will get the first result, so the first bit not one is the not of the first image and bit not two is the not operation on the second image, so you can see wherever we have white, we got black and wherever we have black, we got white, so just the opposite of the input here, also wherever we have the black region, we got the white image here and wherever we have the white pixel. We got the black pixel. So this is how bitwise not operation works on the images. So these are some of the bitwise operations, which you can perform on your images, and as I said bit. Wise operations can be very useful when working with masks, which we will see in the later videos. So stay tuned and keep watching these videos. And I will see you in the next video you.