# The Complete Guide Basic Bitwise Operations OpenCV RaspberryPi

In this tutorial, I will show you the  Basic Bitwise Operations OpenCV RaspberryPi Step By step Complet Process.

Now we will study four bitwise operations: AND, OR, XOR, and NOT. These four operations, while very essential and low level, are paramount to image processing.

Bitwise operations operate in a binary manner and represented as grayscale images. A given pixel turned “off” if it has a value of zero, and it is turned “on” if the pixel has a value greater than zero.

## ≡ Bitwise Operations OpenCV RaspberryPi Code:

This theory is better explained through some code for writing code with raspberry pi, Fast open your command terminal on your Raspberry Pi then write this Commend:

`nano`

you can see like this interface Now we can start writing This code on GNU nano:

```# Import the necessary packages
import numpy as np
import cv2

# First, let's draw a rectangle
rectangle = np.zeros((300, 300), dtype = "uint8")
cv2.rectangle(rectangle, (25, 25), (275, 275), 255, -1)
cv2.imshow("Rectangle", rectangle)

# Secondly, let's draw a circle
circle = np.zeros((300, 300), dtype = "uint8")
cv2.circle(circle, (150, 150), 150, 255, -1)
cv2.imshow("Circle", circle)

bitwiseAnd = cv2.bitwise_and(rectangle, circle)
cv2.imshow("AND", bitwiseAnd)
cv2.waitKey(0)

# A bitwise 'OR' examines every pixel in rectangle
# and circle. If EITHER pixel in rectangle or circle
# is greater than zero, then the output pixel has a
# value of 255, otherwise it is 0.
bitwiseOr = cv2.bitwise_or(rectangle, circle)
cv2.imshow("OR", bitwiseOr)
cv2.waitKey(0)

bitwiseXor = cv2.bitwise_xor(rectangle, circle)
cv2.imshow("XOR", bitwiseXor)
cv2.waitKey(0)

bitwiseNot = cv2.bitwise_not(circle)
cv2.imshow("NOT", bitwiseNot)
cv2.waitKey(0)```

Now save this file named Bitwise .py and exit using ctrl + x, y, enter.  we simply open up a raspberry terminal window and execute the following command:

`python  Bitwise.py`

see like this interface ## ≡ Analysis

```# Import the necessary packages
import numpy as np
import cv2

# First, let's draw a rectangle
rectangle = np.zeros((300, 300), dtype = "uint8")
cv2.rectangle(rectangle, (25, 25), (275, 275), 255, -1)
cv2.imshow("Rectangle", rectangle)

# Secondly, let's draw a circle
circle = np.zeros((300, 300), dtype = "uint8")
cv2.circle(circle, (150, 150), 150, 255, -1)
cv2.imshow("Circle", circle)
```

In This section The first two lines of code import the packages we will need NumPy and cv2. We initialize our rectangle image as a 300 × 300 NumPy array on rectangle = np.zeros((300, 300), dtype = “uint8”)  draw a 250 × 250 white rectangle at the center of the image.

Similarly, circle = np.zeros((300, 300), dtype = “uint8”) we initialize another image to contain our circle, which we draw on cv2.circle(circle, (150, 150), 150, 255, -1), again centered at the center of the image, with a radius of 150 pixels. ```bitwiseAnd = cv2.bitwise_and(rectangle, circle)
cv2.imshow("AND", bitwiseAnd)
cv2.waitKey(0)

bitwiseOr = cv2.bitwise_or(rectangle, circle)
cv2.imshow("OR", bitwiseOr)
cv2.waitKey(0)

bitwiseXor = cv2.bitwise_xor(rectangle, circle)
cv2.imshow("XOR", bitwiseXor)
cv2.waitKey(0)

bitwiseNot = cv2.bitwise_not(circle)
cv2.imshow("NOT", bitwiseNot)
cv2.waitKey(0)```

In this section, the pixel turned “on” if it has a value larger than zero, and it is turned “off” if it has a value of zero. Bitwise functions operate on these binary conditions.

In order to utilize bitwise functions, we assume that we are analyzing two pixels. We’ll analyze each of the pixels and then construct our bitwise representation.

#### Let’s quickly review our binary operations:

1. AND: A bitwise AND is true if and only if both pixels are greater than zero.
2. OR: A bitwise OR is true if either of the two pixels are greater than zero.
3. XOR: A bitwise XOR is true if and only if either of the two pixels are greater than zero, but not both.
4. NOT: A bitwise NOT inverts the “on” and “off” pixels in an image.

bitwiseAnd = cv2.bitwise_and(rectangle, circle) we use a bitwise AND to our rectangle and circle images applying the cv2.bitwise_and function. As the list before mentions, a bitwise AND is true if and only if both pixels are higher than zero. We can see that edges of our square lost – this does sense because our rectangle does not cover as great of an area as the circle, and thus both pixels not “on”.

We then use a bitwise OR on bitwiseOr = cv2.bitwise_or(rectangle, circle) working the cv2.bitwise_or function. A bitwise OR is true if either of the two pixels is greater than zero.

Next up is the bitwise XOR function, applied on bitwiseXor = cv2.bitwise_xor(rectangle, circle) utilizing the cv2.bitwise_xor function. An XOR operation is true if both pixels are greater than zero, but both pixels cannot be larger than zero.

Finally, we apply the NOT function on bitwiseNot = cv2.bitwise_not(circle) using the cv2.bitwise_not function. Actually, the bitwise NOT function flips pixel values. All pixels that are higher than zero are set to zero, and all pixels that are set to zero are set to 255. More Computer vision tutorial on raspberry pi click COMPUTER VISION BEGINNER

Visit My Blog

133 total views,  1 views today