0% found this document useful (0 votes)
12 views

ip3

The document provides code examples for performing logical operations (NOT, OR, AND, XOR) on images using Python, demonstrating their applications in image masking, edge detection, and noise removal. It also includes geometric operations such as scaling, rotating, and translating images, along with pixel relationship implementations for 4-way and 8-way connectivity. Visual outputs are generated using matplotlib to illustrate the results of these operations.

Uploaded by

hudsonnnnn16
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

ip3

The document provides code examples for performing logical operations (NOT, OR, AND, XOR) on images using Python, demonstrating their applications in image masking, edge detection, and noise removal. It also includes geometric operations such as scaling, rotating, and translating images, along with pixel relationship implementations for 4-way and 8-way connectivity. Visual outputs are generated using matplotlib to illustrate the results of these operations.

Uploaded by

hudsonnnnn16
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

NAME : P KOUSHIK REDDY

ROLL NO : 12212161

4) Logical operations such as NOT, OR, AND, and XOR on images:

Code:

import numpy as np

import matplotlib.pyplot as plt

from PIL import Image, ImageDraw

img1_array = np.zeros((200, 200), dtype=np.uint8)

img2_array = np.zeros((200, 200), dtype=np.uint8)

img1 = Image.fromarray(img1_array)

draw1 = ImageDraw.Draw(img1)

draw1.ellipse((50, 50, 150, 150), fill=255)

img1_array = np.array(img1)

img2 = Image.fromarray(img2_array)

draw2 = ImageDraw.Draw(img2)

draw2.rectangle((60, 60, 160, 160), fill=255)

img2_array = np.array(img2)

not_img1 = np.invert(img1_array)

or_img = np.bitwise_or(img1_array, img2_array)

and_img = np.bitwise_and(img1_array, img2_array)

xor_img = np.bitwise_xor(img1_array, img2_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))

fig, axs = plt.subplots(2, 3, figsize=(12, 8))

axs[0, 0].imshow(img1, cmap="gray")

axs[0, 0].set_title("Binary Image 1 (Circle)")

axs[0, 1].imshow(img2, cmap="gray")

axs[0, 1].set_title("Binary Image 2 (Rectangle)")

axs[0, 2].imshow(not_image, cmap="gray")

axs[0, 2].set_title("NOT Operation on Image 1")

axs[1, 0].imshow(or_image, cmap="gray")

axs[1, 0].set_title("OR Operation")

axs[1, 1].imshow(and_image, cmap="gray")

axs[1, 1].set_title("AND Operation")

axs[1, 2].imshow(xor_image, cmap="gray")

axs[1, 2].set_title("XOR Operation")

for ax in axs.flat:

ax.axis("off")

plt.show()

output screenshot :
4b) Understand the usefulness of logical operations:

Image Masking & Object Extraction

✅ 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.

Edge Detection & Feature Enhancement

✅ XOR operation helps detect differences between two images, highlighting edges and changes.
✅ Example: Detecting moving objects in CCTV footage by comparing two frames.

Image Correction & Noise Removal

✅ NOT operation helps in inverting images, which can enhance contrast for better visibility.
✅ Example: Inverting a scanned document to make faded text clearer.

5) Implement geometric operations on images in MATLAB:

import numpy as np

import matplotlib.pyplot as plt

from PIL import Image, ImageDraw

img_size = (200, 200)

img = Image.new("RGB", img_size, (0, 0, 0))

draw = ImageDraw.Draw(img)
draw.rectangle((50, 50, 150, 150), fill=(255, 255, 255))

scaled_img = img.resize((img.width // 2, img.height // 2))

rotated_img = img.rotate(45)

translated_img = Image.new("RGB", (img.width + 50, img.height + 50), (0, 0, 0))

translated_img.paste(img, (50, 50))

fig, axs = plt.subplots(2, 2, figsize=(12, 10))

axs[0, 0].imshow(img)

axs[0, 0].set_title("Original Image")

axs[0, 1].imshow(scaled_img)

axs[0, 1].set_title("Scaled Image (50%)")

axs[1, 0].imshow(rotated_img)

axs[1, 0].set_title("Rotated Image (45°)")

axs[1, 1].imshow(translated_img)

axs[1, 1].set_title("Translated Image (+50 px)")

for ax in axs.flat:

ax.axis("off")

plt.show()

OUTPUT Screenshot:
6. Implementation of Relationships between Pixels:

CODE :

import numpy as np

import matplotlib.pyplot as plt

def get_neighbors_4_way(image, x, y):

rows, cols = image.shape

neighbors = []

directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]

for dx, dy in directions:

nx, ny = x + dx, y + dy
if 0 <= nx < rows and 0 <= ny < cols:

neighbors.append((nx, ny))

return neighbors

def get_neighbors_8_way(image, x, y):

rows, cols = image.shape

neighbors = []

directions = [(-1, 0), (1, 0), (0, -1), (0, 1), (-1, -1), (-1, 1), (1, -1), (1, 1)]

for dx, dy in directions:

nx, ny = x + dx, y + dy

if 0 <= nx < rows and 0 <= ny < cols:

neighbors.append((nx, ny))

return neighbors

image = np.zeros((10, 10), dtype=np.uint8)

x, y = 5, 5

neighbors_4 = get_neighbors_4_way(image, x, y)

neighbors_8 = get_neighbors_8_way(image, x, y)

fig, axes = plt.subplots(1, 2, figsize=(10, 5))

image_4 = image.copy()

for nx, ny in neighbors_4:

image_4[nx, ny] = 255

image_4[x, y] = 128

axes[0].imshow(image_4, cmap="gray", vmin=0, vmax=255)


axes[0].set_title("4-Way Connectivity")

axes[0].scatter(y, x, color="red", marker="o")

image_8 = image.copy()

for nx, ny in neighbors_8:

image_8[nx, ny] = 255

image_8[x, y] = 128

axes[1].imshow(image_8, cmap="gray", vmin=0, vmax=255)

axes[1].set_title("8-Way Connectivity")

axes[1].scatter(y, x, color="red", marker="o")

plt.show()

OUTPUT SCREENSHOTS:

You might also like