0% found this document useful (0 votes)
3 views4 pages

IP Practicle

Uploaded by

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

IP Practicle

Uploaded by

aryanmaske007
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Practical No 1A & 1B

Aim: a) Write a Python program to read an image in grayscale, convert it to colour image and vice
versa. Display all images using OpenCV and Matplotlib.
Theory: A grayscale image contains a single channel that represents pixel brightness, with values
ranging from 0 (black) to 255 (white).
A colour image uses the RGB model with three channels: Red, Green, and Blue.

Converting a colour image to grayscale reduces image complexity by combining RGB values into one
intensity value.
Converting a grayscale image to a colour image copies the same intensity value into all three RGB
channels.
OpenCV is used to read and process images, while Matplotlib is used to display and visualize them.

Code: import cv2


import [Link] as plt
from [Link] import files
uploaded = [Link]()
color_image = [Link]('[Link]')
gray_image = [Link]('[Link]', cv2.IMREAD_GRAYSCALE)
gray_to_color = [Link](gray_image, cv2.COLOR_GRAY2BGR)
color_to_gray = [Link](color_image, cv2.COLOR_BGR2GRAY)
[Link](figsize=(10, 8))

[Link](2, 2, 1)
[Link]("Original Color Image")
[Link]([Link](color_image, cv2.COLOR_BGR2RGB))
[Link]("off")

[Link](2, 2, 2)
[Link]("Original Grayscale Image")
[Link](gray_image, cmap="gray")
[Link]("off")

[Link](2, 2, 3)
[Link]("Grayscale to Color Image")
[Link]([Link](gray_to_color, cv2.COLOR_BGR2RGB))
[Link]("off")

[Link](2, 2, 4)
[Link]("Color to Grayscale Image")
[Link](color_to_gray, cmap="gray")
[Link]("off")

[Link]()
Output:

Aim: b) Write a program to find the 4-adjacent and 8-adjacent neighbours of a given pixel in a
grayscale image.
Theory: Pixel adjacency describes the neighboring pixels around a given pixel in an image.

4-adjacent neighbours share a common edge with the pixel:


• Up, Down, Left, Right
8-adjacent neighbours include 4-adjacent neighbours and diagonal pixels:
• Up, Down, Left, Right, and 4 diagonals
Adjacency is important for image analysis, segmentation, and object detection
Code: uploaded = [Link]()
img = [Link]('[Link]',
cv2.IMREAD_GRAYSCALE)
rows, cols = [Link]
x = int(input("Enter row index of pixel: "))
y = int(input("Enter column index of pixel: "))
if x < 0 or x >= rows or y < 0 or y >= cols:
print("Invalid pixel location!")
else:
print("\nPixel Value:", img[x, y])

# 4-adjacent neighbors (up, down, left, right)


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

print("\n4-Adjacent Neighbours:")
for dx, dy in four_adj:
nx, ny = x + dx, y + dy
if 0 <= nx < rows and 0 <= ny < cols:
print(f"({nx}, {ny}) → {img[nx, ny]}")

# 8-adjacent neighbors (including diagonals)


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

print("\n8-Adjacent Neighbours:")
for dx, dy in eight_adj:
nx, ny = x + dx, y + dy
if 0 <= nx < rows and 0 <= ny < cols:
print(f"({nx}, {ny}) → {img[nx, ny]}")
Output:

You might also like