ip3
ip3
ROLL NO : 12212161
Code:
import numpy as np
img1 = Image.fromarray(img1_array)
draw1 = ImageDraw.Draw(img1)
img1_array = np.array(img1)
img2 = Image.fromarray(img2_array)
draw2 = ImageDraw.Draw(img2)
img2_array = np.array(img2)
not_img1 = np.invert(img1_array)
not_image = Image.fromarray(not_img1.astype(np.uint8))
or_image = Image.fromarray(or_img.astype(np.uint8))
and_image = Image.fromarray(and_img.astype(np.uint8))
xor_image = Image.fromarray(xor_img.astype(np.uint8))
for ax in axs.flat:
ax.axis("off")
plt.show()
output screenshot :
4b) Understand the usefulness of logical operations:
✅ AND operation helps extract specific regions of an image by applying a binary mask.
✅ Example: Extracting a person's face from a green screen background.
✅ XOR operation helps detect differences between two images, highlighting edges and changes.
✅ Example: Detecting moving objects in CCTV footage by comparing two frames.
✅ NOT operation helps in inverting images, which can enhance contrast for better visibility.
✅ Example: Inverting a scanned document to make faded text clearer.
import numpy as np
draw = ImageDraw.Draw(img)
draw.rectangle((50, 50, 150, 150), fill=(255, 255, 255))
rotated_img = img.rotate(45)
axs[0, 0].imshow(img)
axs[0, 1].imshow(scaled_img)
axs[1, 0].imshow(rotated_img)
axs[1, 1].imshow(translated_img)
for ax in axs.flat:
ax.axis("off")
plt.show()
OUTPUT Screenshot:
6. Implementation of Relationships between Pixels:
CODE :
import numpy as np
neighbors = []
nx, ny = x + dx, y + dy
if 0 <= nx < rows and 0 <= ny < cols:
neighbors.append((nx, ny))
return neighbors
neighbors = []
directions = [(-1, 0), (1, 0), (0, -1), (0, 1), (-1, -1), (-1, 1), (1, -1), (1, 1)]
nx, ny = x + dx, y + dy
neighbors.append((nx, ny))
return neighbors
x, y = 5, 5
neighbors_4 = get_neighbors_4_way(image, x, y)
neighbors_8 = get_neighbors_8_way(image, x, y)
image_4 = image.copy()
image_4[x, y] = 128
image_8 = image.copy()
image_8[x, y] = 128
axes[1].set_title("8-Way Connectivity")
plt.show()
OUTPUT SCREENSHOTS: