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

DIP Lab Codes

Uploaded by

joss man
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

DIP Lab Codes

Uploaded by

joss man
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 65

1.

Image input/output both skimage and OpenCV

(1) Using skimage (scikit-image)


from skimage import io, img_as_ubyte
import matplotlib.pyplot as plt

# Image input (read image)


image = io.imread('input_image.jpg')

# Display the image using matplotlib


plt.imshow(image)
plt.axis('off') # Hide axes
plt.show()

# Image output (save image)


io.imsave('output_image.jpg', img_as_ubyte(image)) # Convert to byte and save

(2) Using OpenCV


import cv2

# Image input (read image)


image = cv2.imread('input_image.jpg')

# Display the image using OpenCV


cv2.imshow('Image', image)
cv2.waitKey(0) # Wait for a key press to close the window
cv2.destroyAllWindows()
# Image output (save image)
cv2.imwrite('output_image.jpg', image)

# For skimage
image = io.imread('C:/Users/YourUsername/Pictures/input_image.jpg')

# For OpenCV
image = cv2.imread('C:/Users/YourUsername/Pictures/input_image.jpg')

# Using skimage
image = io.imread('/home/user/images/input_image.jpg')

# Using OpenCV
image = cv2.imread('/home/user/images/input_image.jpg')

Notes:
In skimage, the image is read as a NumPy array, and you need to use img_as_ubyte to convert
the image data to 8-bit format before saving it.
In OpenCV, the image is directly read as a NumPy array, and the cv2.imwrite method saves the
image in the specified format.
Both libraries handle images as NumPy arrays, but skimage tends to focus on scientific and
analytical image processing, while OpenCV is more widely used for general-purpose computer
vision tasks.
3. Loading an image from a local dataset (folder of images) Example using os and
skimage
import os
from skimage import io
import matplotlib.pyplot as plt

# Define the dataset directory


dataset_dir = 'path_to_your_dataset/'

# List all image files in the dataset


image_files = os.listdir(dataset_dir)

# Select an image (e.g., first image)


image_path = os.path.join(dataset_dir, image_files[0])

# Load the image


image = io.imread(image_path)

# Display the image


plt.imshow(image)
plt.axis('off')
plt.show()
4. Loading an image from a local dataset (folder of images) Example using os and
opencv
import os
import cv2

# Define the dataset directory


dataset_dir = 'path_to_your_dataset/'

# List all image files in the dataset


image_files = os.listdir(dataset_dir)

# Select an image (e.g., first image)


image_path = os.path.join(dataset_dir, image_files[0])

# Load the image


image = cv2.imread(image_path)

# Display the image using OpenCV


cv2.imshow('Image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

2. Color channel conversion

(1) Using skimage for Color Channel Conversion


from skimage import io, color
# Read the input image
image = io.imread('input_image.jpg')

# Convert RGB to Grayscale


gray_image = color.rgb2gray(image)

# Convert RGB to HSV


hsv_image = color.rgb2hsv(image)

# Save the grayscale and HSV images


io.imsave('grayscale_image.jpg', gray_image)
io.imsave('hsv_image.jpg', hsv_image)

(2) Using OpenCV for Color Channel Conversion


import cv2

# Read the input image


image = cv2.imread('input_image.jpg')

# Convert BGR to Grayscale (OpenCV uses BGR by default)


gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Convert BGR to HSV


hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

# Save the grayscale and HSV images


cv2.imwrite('grayscale_image.jpg', gray_image)
cv2.imwrite('hsv_image.jpg', hsv_image)

(3) Using skimage Convert RGB to Grayscale


from skimage import io, color
import matplotlib.pyplot as plt

# Read the image


image = io.imread('input_image.jpg')

# Convert RGB to Grayscale


gray_image = color.rgb2gray(image)

# Display grayscale image


plt.imshow(gray_image, cmap='gray')
plt.axis('off')
plt.show()

# Save the grayscale image


io.imsave('output_gray_image.jpg', gray_image)
# Convert RGB to HSV
hsv_image = color.rgb2hsv(image)

# Display HSV image


plt.imshow(hsv_image)
plt.axis('off')
plt.show()
# Save the HSV image
io.imsave('output_hsv_image.jpg', hsv_image)

(4) Using OpenCV Convert RGB to Grayscale


import cv2

# Read the image


image = cv2.imread('input_image.jpg')

# Convert RGB to Grayscale


gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Display the grayscale image


cv2.imshow('Grayscale Image', gray_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

# Save the grayscale image


cv2.imwrite('output_gray_image.jpg', gray_image)

# Convert RGB to HSV


hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

# Display the HSV image


cv2.imshow('HSV Image', hsv_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Save the HSV image
cv2.imwrite('output_hsv_image.jpg', hsv_image)

Color Conversion Codes in OpenCV


cv2.COLOR_BGR2GRAY: Convert from BGR (default in OpenCV) to Grayscale.
cv2.COLOR_BGR2HSV: Convert from BGR to HSV.

Notes:
In skimage, the functions rgb2gray and rgb2hsv are used for conversions.
In OpenCV, use cv2.cvtColor() with the appropriate conversion code.
These conversions demonstrate how to change from one color space to another, specifically
RGB/BGR to Grayscale and HSV. The saved images reflect the changes in color channels.

(5) RGB to Grayscale (Manual Implementation)


import numpy as np
from skimage import io
import matplotlib.pyplot as plt

# 1. Read the image (We only use skimage to read and save the image, processing will be
manual)
image = io.imread('input_image.jpg')

# Get image dimensions (height, width, channels)


height, width, channels = image.shape
print(f"Image dimensions: {height}x{width}, Channels: {channels}")

# Initialize an empty array for the grayscale image


grayscale_image = np.zeros((height, width), dtype=np.uint8)

# 2. Manually apply the grayscale conversion formula


for y in range(height):
for x in range(width):
# Get the RGB values
R, G, B = image[y, x]

# Compute the grayscale value using the formula


gray_value = 0.2989 * R + 0.5870 * G + 0.1140 * B

# Assign the computed grayscale value to the new image


grayscale_image[y, x] = int(gray_value)

# 3. Save and display the grayscale image


io.imsave('grayscale_manual.jpg', grayscale_image)

plt.imshow(grayscale_image, cmap='gray')
plt.axis('off')
plt.show()

(6) RGB to HSV (Manual Implementation)


The conversion from RGB to HSV involves:
Hue (H): Represents the color type (angle in the color wheel).
Saturation (S): Represents the intensity of the color.
Value (V): Represents the brightness of the color.
The steps to convert from RGB to HSV are:
Normalize RGB values to the range [0, 1].
Compute intermediate values:

## Code for RGB to HSV Conversion (Manual)


import numpy as np
from skimage import io
import matplotlib.pyplot as plt

# 1. Read the image


image = io.imread('input_image.jpg')

# Get image dimensions (height, width, channels)


height, width, channels = image.shape
print(f"Image dimensions: {height}x{width}, Channels: {channels}")
# Initialize an empty array for the HSV image
hsv_image = np.zeros((height, width, 3), dtype=np.float32)

# 2. Manually apply the RGB to HSV conversion formula


for y in range(height):
for x in range(width):
# Get the RGB values and normalize them
R, G, B = image[y, x] / 255.0

# Calculate the max and min RGB values


C_max = max(R, G, B)
C_min = min(R, G, B)
delta = C_max - C_min

# Calculate Hue (H)


if delta == 0:
H=0
elif C_max == R:
H = 60 * (((G - B) / delta) % 6)
elif C_max == G:
H = 60 * (((B - R) / delta) + 2)
elif C_max == B:
H = 60 * (((R - G) / delta) + 4)

# Calculate Saturation (S)


S = 0 if C_max == 0 else delta / C_max
# Calculate Value (V)
V = C_max

# Assign the computed HSV values to the new image


hsv_image[y, x] = [H, S, V]

# Normalize the hue to [0, 360] and save the HSV image
hsv_image[..., 0] = hsv_image[..., 0] / 360.0 # Scale Hue to [0, 1] for visualization

# 3. Save and display the HSV image


io.imsave('hsv_manual.jpg', (hsv_image * 255).astype(np.uint8))

# Display the HSV image


plt.imshow(hsv_image)
plt.axis('off')
plt.show()

3. Update an image with specific pixel coords

(1) Using skimage


from skimage import io
import numpy as np

# Read the input image


image = io.imread('input_image.jpg')
# Modify a specific pixel (for example, at coordinates (x, y))
x, y = 100, 150 # Example coordinates
new_color = [255, 0, 0] # RGB for red

# Update the pixel at (x, y) to red


image[y, x] = new_color

# Modify a region of pixels (for example, a 10x10 region starting at (x, y))
image[y:y+10, x:x+10] = [0, 255, 0] # Change the region to green

# Save the updated image


io.imsave('updated_image.jpg', image)

(2) Using OpenCV


import cv2

# Read the input image


image = cv2.imread('input_image.jpg')
# Modify a specific pixel (for example, at coordinates (x, y))
x, y = 100, 150 # Example coordinates
new_color = [0, 0, 255] # BGR for red (OpenCV uses BGR format)

# Update the pixel at (x, y) to red


image[y, x] = new_color

# Modify a region of pixels (for example, a 10x10 region starting at (x, y))
image[y:y+10, x:x+10] = [0, 255, 0] # Change the region to green (BGR format)
# Save the updated image
cv2.imwrite('updated_image.jpg', image)

Explanation:
image[y, x] = new_color updates the pixel at (x, y) with the new color value.
You can also modify a block of pixels by specifying a region, such as image[y:y+10, x:x+10],
which changes a 10x10 region of the image.

Make sure that:


For skimage, color format is RGB.
For OpenCV, color format is BGR (blue, green, red).

(3) Using skimage Manual Logic


import numpy as np
from skimage import io
import matplotlib.pyplot as plt

# 1. Read the image


image = io.imread('input_image.jpg')

# Get image dimensions (height, width, channels)


height, width, channels = image.shape
print(f"Image dimensions: {height}x{width}, Channels: {channels}")

# 2. Function to manually update a pixel value


def update_pixel(image_array, x, y, new_color):
# Check if the coordinates are within the image bounds
if 0 <= x < width and 0 <= y < height:
# Manually update the pixel at (x, y) to new color
image_array[y, x] = new_color
else:
print(f"Coordinates ({x}, {y}) are out of bounds!")

# 3. Manually update a pixel at specific coordinates


x, y = 50, 100 # Example coordinates
new_color = [255, 0, 0] # New color in RGB (red)

# Update the pixel manually


update_pixel(image, x, y, new_color)

# 4. Function to update a region of pixels


def update_region(image_array, start_x, start_y, region_width, region_height, new_color):
for i in range(start_y, start_y + region_height):
for j in range(start_x, start_x + region_width):
update_pixel(image_array, j, i, new_color)

# 5. Update a region (e.g., 10x10 square at coordinates (x, y))


update_region(image, x, y, 10, 10, [0, 255, 0]) # Green color

# 6. Save the updated image


io.imsave('updated_image_skimage.jpg', image)
# Optional: Display the updated image
plt.imshow(image)
plt.axis('off')
plt.show()

Explanation:
Reading the Image: io.imread() from skimage is used to load the image into a NumPy array.
Updating Pixels: We use the function update_pixel to change the color of a single pixel and
update_region to change the color of a rectangular region.
Manual Pixel Access: Pixels are accessed and modified using image[y, x], where (x, y) are the
coordinates, and the color is represented as an array like [255, 0, 0] (RGB format for red).
Saving the Image: After modifying the pixel values, the updated image is saved using
io.imsave().

Summary of Key Differences:


Color Format: In skimage, the color format is RGB (whereas in OpenCV, it’s BGR).
Image Reading and Writing: We use io.imread() and io.imsave() for reading and saving images.
This solution allows you to work with the image as a NumPy array and modify pixel values
manually, providing greater control and understanding of image processing.

(4) Using OpenCV Manual Logic


import numpy as np
import cv2 # We use OpenCV for reading and saving but handle pixels manually

# 1. Read the image


image = cv2.imread('input_image.jpg')

# Get image dimensions (height, width, channels)


height, width, channels = image.shape
print(f"Image dimensions: {height}x{width}, Channels: {channels}")
# 2. Function to manually update a pixel value
def update_pixel(image_array, x, y, new_color):
# Check if the coordinates are within the image bounds
if 0 <= x < width and 0 <= y < height:
# Manually update the pixel at (x, y) to new color
image_array[y, x] = new_color
else:
print(f"Coordinates ({x}, {y}) are out of bounds!")

# 3. Manually update a pixel at specific coordinates


x, y = 50, 100 # Example coordinates
new_color = [0, 0, 255] # New color in BGR (red in OpenCV's format)

# Update the pixel manually


update_pixel(image, x, y, new_color)

# 4. Function to update a region of pixels


def update_region(image_array, start_x, start_y, region_width, region_height, new_color):
for i in range(start_y, start_y + region_height):
for j in range(start_x, start_x + region_width):
update_pixel(image_array, j, i, new_color)

# 5. Update a region (e.g., 10x10 square at coordinates (x, y))


update_region(image, x, y, 10, 10, [0, 255, 0]) # Green color

# 6. Save the updated image


cv2.imwrite('updated_image.jpg', image)

Explanation:
Reading the Image: We use OpenCV to read the image, but the pixel updates are done
manually.
Updating Pixel Values: The function update_pixel manually changes the pixel at (x, y) to the
new_color. The color in OpenCV is in BGR format (blue, green, red).
Region Update: The update_region function uses loops to modify a rectangular region of pixels,
applying the same logic as single pixel modification.
Saving the Image: After the modification, the updated image is saved using cv2.imwrite.

Manual Logic:
Pixel Access: image[y, x] accesses the pixel at coordinates (x, y) in the 2D array. We manually
set it to the desired color.
Pixel Bounds Check: Before updating, we check that the pixel coordinates are within the image
bounds to prevent out-of-bounds errors.
This approach allows you to understand how images are essentially arrays of pixel values and
how to manipulate them directly.

4. Image plotting with matplotlib and opencv


(1) Image Plotting Using matplotlib
import matplotlib.pyplot as plt
import cv2

# Read the image using OpenCV (in BGR format)


image = cv2.imread('input_image.jpg')

# Convert the image from BGR (OpenCV's default) to RGB for correct display in matplotlib
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Plot the image using matplotlib
plt.imshow(image_rgb)
plt.axis('off') # Turn off axis labels
plt.title('Image Display with Matplotlib')
plt.show()

Explanation:
Image Reading: We use cv2.imread() to read the image in BGR format (the default for OpenCV).
Color Conversion: Since matplotlib expects images in RGB format, we convert the image from
BGR to RGB using cv2.cvtColor().
Plotting: plt.imshow() is used to display the image, and plt.axis('off') removes the axis labels for
a cleaner display.

(2) Image Display Using OpenCV


import cv2

# Read the image using OpenCV (in BGR format)


image = cv2.imread('input_image.jpg')

# Display the image using OpenCV


cv2.imshow('Image Display with OpenCV', image)

# Wait until a key is pressed


cv2.waitKey(0)

# Close all windows


cv2.destroyAllWindows()
Explanation:
cv2.imshow() displays the image in a window.
cv2.waitKey(0) waits for any key press before closing the window.
cv2.destroyAllWindows() ensures all windows opened by OpenCV are closed after the display.

Comparing matplotlib and OpenCV


Color Format: OpenCV loads images in BGR format, whereas matplotlib expects RGB format.
You need to convert from BGR to RGB when using matplotlib if you load the image with
OpenCV.
Interaction: OpenCV windows are interactive (allow zooming, resizing), while matplotlib images
are static unless you're embedding them into a GUI or Jupyter notebook.
Plotting Overlays: matplotlib is excellent for adding graphs, overlays, and titles to images.
OpenCV is more commonly used for real-time image processing and displaying results quickly.

5. Arithmetic operation on an image

(1) Code for Arithmetic Operations on an Image


import numpy as np
from skimage import io
import matplotlib.pyplot as plt

# 1. Read the image (Use skimage for reading, but arithmetic will be done manually)
image = io.imread('input_image.jpg')

# Ensure the image is in uint8 format (values between 0 and 255)


image = image.astype(np.uint8)

# Get image dimensions (height, width, channels)


height, width, channels = image.shape
print(f"Image dimensions: {height}x{width}, Channels: {channels}")

# 2. Function to manually add a scalar value to each pixel


def add_scalar_to_image(image_array, scalar):
# Initialize an empty array for the result
result_image = np.zeros_like(image_array)

# Manually add scalar to each pixel value


for y in range(height):
for x in range(width):
for c in range(channels):
# Add the scalar and clip values to stay within [0, 255]
result_image[y, x, c] = min(image_array[y, x, c] + scalar, 255)

return result_image

# 3. Function to manually subtract a scalar from each pixel


def subtract_scalar_from_image(image_array, scalar):
# Initialize an empty array for the result
result_image = np.zeros_like(image_array)

# Manually subtract scalar from each pixel value


for y in range(height):
for x in range(width):
for c in range(channels):
# Subtract the scalar and clip values to stay within [0, 255]
result_image[y, x, c] = max(image_array[y, x, c] - scalar, 0)

return result_image

# 4. Function to manually multiply each pixel by a scalar


def multiply_scalar_to_image(image_array, scalar):
# Initialize an empty array for the result
result_image = np.zeros_like(image_array)

# Manually multiply each pixel value by the scalar


for y in range(height):
for x in range(width):
for c in range(channels):
# Multiply the pixel value by the scalar and clip it to [0, 255]
result_image[y, x, c] = min(image_array[y, x, c] * scalar, 255)

return result_image

# 5. Perform addition, subtraction, and multiplication on the image


scalar = 50 # Example scalar value

# Add scalar to image


added_image = add_scalar_to_image(image, scalar)

# Subtract scalar from image


subtracted_image = subtract_scalar_from_image(image, scalar)
# Multiply scalar to image
multiplied_image = multiply_scalar_to_image(image, 1.2) # Scale by 1.2

# 6. Save and display the results


io.imsave('added_image.jpg', added_image)
io.imsave('subtracted_image.jpg', subtracted_image)
io.imsave('multiplied_image.jpg', multiplied_image)

# Display the original and modified images


plt.figure(figsize=(15, 5))

plt.subplot(1, 4, 1)
plt.imshow(image)
plt.title('Original Image')
plt.axis('off')

plt.subplot(1, 4, 2)
plt.imshow(added_image)
plt.title('Added Scalar')
plt.axis('off')

plt.subplot(1, 4, 3)
plt.imshow(subtracted_image)
plt.title('Subtracted Scalar')
plt.axis('off')

plt.subplot(1, 4, 4)
plt.imshow(multiplied_image)
plt.title('Multiplied by Scalar')
plt.axis('off')

plt.show()

Explanation:
Manual Addition: We manually add a scalar to each pixel’s RGB value. If the result exceeds 255,
we clip it to 255.
Manual Subtraction: We manually subtract a scalar from each pixel’s RGB value. If the result
goes below 0, we clip it to 0.
Manual Multiplication: We multiply each pixel’s RGB value by a scalar, making sure the values
stay within the range of 0 to 255.
Clipping: Clipping ensures that the pixel values don’t exceed the valid range (0-255) for an 8-bit
image.

Summary:
Addition: Brightens the image by increasing the intensity of each pixel.
Subtraction: Darkens the image by decreasing the intensity of each pixel.
Multiplication: Adjusts the contrast of the image, where values greater than 1 increase
contrast, and values between 0 and 1 reduce contrast.
(2) Arithmetic Operations Using skimage (built-in functions)
from skimage import io, img_as_ubyte
import numpy as np
import matplotlib.pyplot as plt

# Read the image


image = io.imread('input_image.jpg')

# Convert image to 8-bit (0-255 range) if necessary


image = img_as_ubyte(image)

# 1. Addition of a scalar
scalar = 50
added_image = np.clip(image + scalar, 0, 255).astype(np.uint8)

# 2. Subtraction of a scalar
subtracted_image = np.clip(image - scalar, 0, 255).astype(np.uint8)

# 3. Multiplication by a scalar
multiplier = 1.2
multiplied_image = np.clip(image * multiplier, 0, 255).astype(np.uint8)

# Display the results


plt.figure(figsize=(12, 6))

plt.subplot(1, 4, 1)
plt.imshow(image)
plt.title('Original Image')
plt.axis('off')

plt.subplot(1, 4, 2)
plt.imshow(added_image)
plt.title('Added Scalar')
plt.axis('off')

plt.subplot(1, 4, 3)
plt.imshow(subtracted_image)
plt.title('Subtracted Scalar')
plt.axis('off')

plt.subplot(1, 4, 4)
plt.imshow(multiplied_image)
plt.title('Multiplied by Scalar')
plt.axis('off')

plt.show()

Explanation:
Addition: np.clip(image + scalar, 0, 255) adds a scalar to the image and clips the result to ensure
values are within the 0-255 range.
Subtraction: np.clip(image - scalar, 0, 255) subtracts a scalar from the image and clips the
result.
Multiplication: np.clip(image * multiplier, 0, 255) multiplies the image pixel values by a scalar
and clips the result.
(3) Arithmetic Operations Using OpenCV (built-in functions)
import cv2
import numpy as np

# Read the image (in BGR format)


image = cv2.imread('input_image.jpg')

# 1. Addition of a scalar
scalar = 50
added_image = cv2.add(image, scalar) # OpenCV handles clipping automatically

# 2. Subtraction of a scalar
subtracted_image = cv2.subtract(image, scalar) # OpenCV handles clipping automatically

# 3. Multiplication by a scalar

multiplier = 1.2
multiplied_image = cv2.multiply(image, np.array([multiplier])) # OpenCV handles clipping
automatically

# Display the results


cv2.imshow('Original Image', image)
cv2.imshow('Added Scalar', added_image)
cv2.imshow('Subtracted Scalar', subtracted_image)
cv2.imshow('Multiplied by Scalar', multiplied_image)

# Wait until a key is pressed


cv2.waitKey(0)
# Close all windows
cv2.destroyAllWindows()

Explanation:
Addition: cv2.add(image, scalar) adds a scalar value to each pixel and handles clipping
internally.
Subtraction: cv2.subtract(image, scalar) subtracts a scalar from each pixel and handles clipping.
Multiplication: cv2.multiply(image, np.array([multiplier])) multiplies pixel values by a scalar and
handles clipping.

Summary:
skimage and OpenCV simplify arithmetic operations on images by providing functions that
handle these operations efficiently and automatically clip the results to ensure pixel values
remain within valid ranges.
skimage uses NumPy operations and requires manual clipping, while OpenCV provides
convenient functions (add, subtract, multiply) that handle clipping and other edge cases
internally.
6. Point processing(negative, gamma, logarithmic,
exponential) based on scenario

(1) Implementation Using NumPy


import numpy as np
from skimage import io, img_as_ubyte
import matplotlib.pyplot as plt

# Read the image


image = io.imread('input_image.jpg')

# Convert image to 8-bit (0-255 range) if necessary


image = img_as_ubyte(image)
# Get image dimensions (height, width, channels)
height, width, channels = image.shape

# 1. Negative Transformation
def negative_transformation(image_array):
return 255 - image_array

# 2. Gamma Correction
def gamma_correction(image_array, gamma):
normalized = image_array / 255.0
corrected = 255 * np.power(normalized, gamma)
return corrected.astype(np.uint8)

# 3. Logarithmic Transformation
def logarithmic_transformation(image_array, c):
normalized = image_array / 255.0
transformed = c * np.log1p(normalized)
return np.clip(transformed * 255, 0, 255).astype(np.uint8)

# 4. Exponential Transformation
def exponential_transformation(image_array, c):
normalized = image_array / 255.0
transformed = c * (np.exp(normalized) - 1)
return np.clip(transformed * 255, 0, 255).astype(np.uint8)

# Parameters for transformations


gamma_value = 0.5 # Example gamma value
log_constant = 1 # Example constant for logarithmic transformation
exp_constant = 1 # Example constant for exponential transformation

# Apply transformations
negative_image = negative_transformation(image)
gamma_corrected_image = gamma_correction(image, gamma_value)
logarithmic_image = logarithmic_transformation(image, log_constant)
exponential_image = exponential_transformation(image, exp_constant)

# Display the results


plt.figure(figsize=(15, 10))

plt.subplot(2, 3, 1)
plt.imshow(image, cmap='gray')
plt.title('Original Image')
plt.axis('off')

plt.subplot(2, 3, 2)
plt.imshow(negative_image, cmap='gray')
plt.title('Negative Transformation')
plt.axis('off')

plt.subplot(2, 3, 3)
plt.imshow(gamma_corrected_image, cmap='gray')
plt.title('Gamma Correction')
plt.axis('off')
plt.subplot(2, 3, 4)
plt.imshow(logarithmic_image, cmap='gray')
plt.title('Logarithmic Transformation')
plt.axis('off')

plt.subplot(2, 3, 5)
plt.imshow(exponential_image, cmap='gray')
plt.title('Exponential Transformation')
plt.axis('off')

plt.show()

Explanation:
Negative Transformation:
Subtracts pixel values from 255 to invert the image.
255 - image_array inverts the pixel values.
Gamma Correction:
Adjusts image brightness based on gamma.
Normalizes pixel values to [0, 1], applies gamma correction, and scales back to [0, 255].
Logarithmic Transformation:
Enhances details in darker regions.
Normalizes pixel values, applies logarithmic transformation, and scales to [0, 255].
Exponential Transformation:
Enhances lighter regions.
Normalizes pixel values, applies exponential transformation, and scales to [0, 255].
Summary:
Negative Transformation: Inverts the image colors.
Gamma Correction: Adjusts brightness.
Logarithmic Transformation: Enhances dark regions.
Exponential Transformation: Enhances light regions.

(2) Manual Implementation


import numpy as np
from skimage import io, img_as_ubyte
import matplotlib.pyplot as plt

# Read and prepare the image


image = io.imread('input_image.jpg')
image = img_as_ubyte(image)

# 1. Negative Transformation
def negative_transformation(image_array):
height, width, channels = image_array.shape
result_image = np.zeros_like(image_array)
for y in range(height):
for x in range(width):
for c in range(channels):
result_image[y, x, c] = 255 - image_array[y, x, c]
return result_image

# 2. Gamma Correction
def gamma_correction(image_array, gamma):
height, width, channels = image_array.shape
result_image = np.zeros_like(image_array)
for y in range(height):
for x in range(width):
for c in range(channels):
normalized = image_array[y, x, c] / 255.0
corrected = 255 * (normalized ** gamma)
result_image[y, x, c] = min(max(int(corrected), 0), 255)
return result_image

# 3. Logarithmic Transformation
def logarithmic_transformation(image_array, c):
height, width, channels = image_array.shape
result_image = np.zeros_like(image_array)
for y in range(height):
for x in range(width):
for c in range(channels):
normalized = image_array[y, x, c] / 255.0
transformed = c * np.log1p(normalized)
result_image[y, x, c] = min(max(int(transformed * 255), 0), 255)
return result_image

# 4. Exponential Transformation
def exponential_transformation(image_array, c):
height, width, channels = image_array.shape
result_image = np.zeros_like(image_array)
for y in range(height):
for x in range(width):
for c in range(channels):
normalized = image_array[y, x, c] / 255.0
transformed = c * (np.exp(normalized) - 1)
result_image[y, x, c] = min(max(int(transformed * 255), 0), 255)
return result_image

# Parameters for transformations


gamma_value = 0.5 # Example gamma value
log_constant = 1 # Example constant for logarithmic transformation
exp_constant = 1 # Example constant for exponential transformation

# Apply transformations
negative_image = negative_transformation(image)
gamma_corrected_image = gamma_correction(image, gamma_value)
logarithmic_image = logarithmic_transformation(image, log_constant)
exponential_image = exponential_transformation(image, exp_constant)

# Display the results


plt.figure(figsize=(15, 10))

plt.subplot(2, 3, 1)
plt.imshow(image)
plt.title('Original Image')
plt.axis('off')

plt.subplot(2, 3, 2)
plt.imshow(negative_image)
plt.title('Negative Transformation')
plt.axis('off')

plt.subplot(2, 3, 3)
plt.imshow(gamma_corrected_image)
plt.title('Gamma Correction')
plt.axis('off')

plt.subplot(2, 3, 4)
plt.imshow(logarithmic_image)
plt.title('Logarithmic Transformation')
plt.axis('off')

plt.subplot(2, 3, 5)
plt.imshow(exponential_image)
plt.title('Exponential Transformation')
plt.axis('off')

plt.show()

Explanation:
Image Reading: The image is read and converted to 8-bit format.
Negative Transformation: Subtracts each pixel value from 255.
Gamma Correction: Adjusts brightness using gamma correction formula.
Logarithmic Transformation: Enhances dark regions using logarithmic scaling.
Exponential Transformation: Enhances light regions using exponential scaling.
Display: Uses matplotlib to display the original and transformed images in a grid layout.
7. Image enhancement with histogram equalization

(1) Manual Histogram Equalization


import numpy as np
from skimage import io, img_as_ubyte
import matplotlib.pyplot as plt

# Read and prepare the image


image = io.imread('input_image.jpg')
image = img_as_ubyte(image)

# Convert image to grayscale if it is not already


if len(image.shape) == 3:
image = np.mean(image, axis=2).astype(np.uint8)

# 1. Manual Histogram Equalization


def histogram_equalization(image_array):
# Compute histogram
hist, bins = np.histogram(image_array.flatten(), bins=256, range=[0, 256])

# Compute cumulative distribution function (CDF)


cdf = hist.cumsum()
cdf_normalized = cdf * 255 / cdf[-1] # Normalize

# Use CDF to map pixel values


equalized_image = np.interp(image_array.flatten(), bins[:-1], cdf_normalized)
return equalized_image.reshape(image_array.shape).astype(np.uint8)
# Apply histogram equalization
equalized_image = histogram_equalization(image)

# Display the results


plt.figure(figsize=(12, 6))

plt.subplot(1, 2, 1)
plt.imshow(image, cmap='gray')
plt.title('Original Image')
plt.axis('off')

plt.subplot(1, 2, 2)
plt.imshow(equalized_image, cmap='gray')
plt.title('Equalized Image (Manual)')
plt.axis('off')

plt.show()

Note
Manual histogram equalization involves calculating the histogram of the image, computing the
cumulative distribution function (CDF), and then mapping the pixel values to their new values
based on the CDF.
(2) skimage Built-in Function Implementation
from skimage import exposure

# Apply histogram equalization using skimage


equalized_image_skimage = exposure.equalize_hist(image)

# Display the results


plt.figure(figsize=(12, 6))

plt.subplot(1, 2, 1)
plt.imshow(image, cmap='gray')
plt.title('Original Image')
plt.axis('off')

plt.subplot(1, 2, 2)
plt.imshow(equalized_image_skimage, cmap='gray')
plt.title('Equalized Image (skimage)')
plt.axis('off')

plt.show()
(3) opencv Built-in Function Implementation
import cv2

# Convert image to grayscale if it is not already


if len(image.shape) == 3:
image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)

# Apply histogram equalization using OpenCV


equalized_image_cv2 = cv2.equalizeHist(image)

# Display the results


plt.figure(figsize=(12, 6))

plt.subplot(1, 2, 1)
plt.imshow(image, cmap='gray')
plt.title('Original Image')
plt.axis('off')

plt.subplot(1, 2, 2)
plt.imshow(equalized_image_cv2, cmap='gray')
plt.title('Equalized Image (OpenCV)')
plt.axis('off')

plt.show()
8. Filtering(Smoothening, Sharpening) [with or without given
kernel]

(1) Smoothening (Blurring) Manual Implementation (Using a 3x3 Mean Filter)


import numpy as np
from skimage import io, img_as_float, img_as_ubyte
import matplotlib.pyplot as plt

# Read and prepare the image


image = io.imread('input_image.jpg')
image = img_as_float(image)

# Convert image to grayscale if it is not already


if len(image.shape) == 3:
image = np.mean(image, axis=2)

# Manual smoothening (blurring) with a 3x3 mean filter


def apply_filter(image_array, kernel):
height, width = image_array.shape
k_height, k_width = kernel.shape
pad_height = k_height // 2
pad_width = k_width // 2

padded_image = np.pad(image_array, ((pad_height, pad_height), (pad_width, pad_width)),


mode='constant')
result_image = np.zeros_like(image_array)
for y in range(height):
for x in range(width):
region = padded_image[y:y+k_height, x:x+k_width]
result_image[y, x] = np.sum(region * kernel)

return result_image

# Define a 3x3 mean filter kernel


mean_filter_kernel = np.ones((3, 3)) / 9.0

# Apply the filter


blurred_image = apply_filter(image, mean_filter_kernel)

# Display the results


plt.figure(figsize=(12, 6))

plt.subplot(1, 2, 1)
plt.imshow(image, cmap='gray')
plt.title('Original Image')
plt.axis('off')

plt.subplot(1, 2, 2)
plt.imshow(blurred_image, cmap='gray')
plt.title('Blurred Image (Manual)')
plt.axis('off')

plt.show()
(2) Smoothening (Blurring) Using skimage Built-in Functions
from skimage import filters

# Apply smoothing using skimage's Gaussian filter


blurred_image_skimage = filters.gaussian(image, sigma=1)

# Display the results


plt.figure(figsize=(12, 6))

plt.subplot(1, 2, 1)
plt.imshow(image, cmap='gray')
plt.title('Original Image')
plt.axis('off')

plt.subplot(1, 2, 2)
plt.imshow(blurred_image_skimage, cmap='gray')
plt.title('Blurred Image (skimage)')
plt.axis('off')

plt.show()
(3) Smoothening (Blurring) Using openCV Built-in Functions
import cv2

# Convert image to grayscale if it is not already


if len(image.shape) == 3:
image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)

# Apply smoothing using OpenCV's Gaussian filter


blurred_image_cv2 = cv2.GaussianBlur(image, (3, 3), sigmaX=1)

# Display the results


plt.figure(figsize=(12, 6))

plt.subplot(1, 2, 1)
plt.imshow(image, cmap='gray')
plt.title('Original Image')
plt.axis('off')

plt.subplot(1, 2, 2)
plt.imshow(blurred_image_cv2, cmap='gray')
plt.title('Blurred Image (OpenCV)')
plt.axis('off')

plt.show()
(4) Sharpening Manual Implementation (Using a 3x3 Sharpening Filter)
# Manual sharpening with a 3x3 sharpening filter
def apply_sharpening_filter(image_array, kernel):
height, width = image_array.shape
k_height, k_width = kernel.shape
pad_height = k_height // 2
pad_width = k_width // 2

padded_image = np.pad(image_array, ((pad_height, pad_height), (pad_width, pad_width)),


mode='constant')
result_image = np.zeros_like(image_array)

for y in range(height):
for x in range(width):
region = padded_image[y:y+k_height, x:x+k_width]
result_image[y, x] = np.sum(region * kernel)

return np.clip(result_image, 0, 1) # Ensure the pixel values are in range [0, 1]

# Define a 3x3 sharpening filter kernel


sharpening_filter_kernel = np.array([[ 0, -1, 0],
[-1, 5, -1],
[ 0, -1, 0]])

# Apply the sharpening filter


sharpened_image = apply_sharpening_filter(image, sharpening_filter_kernel)
# Display the results
plt.figure(figsize=(12, 6))

plt.subplot(1, 2, 1)
plt.imshow(image, cmap='gray')
plt.title('Original Image')
plt.axis('off')

plt.subplot(1, 2, 2)
plt.imshow(sharpened_image, cmap='gray')
plt.title('Sharpened Image (Manual)')
plt.axis('off')

plt.show()
(5) Sharpening Using skimage Built-in Functions
from skimage import restoration

# Apply sharpening using skimage's unsharp mask (inverse of Gaussian blur)


sharpened_image_skimage = restoration.unsharp_mask(image, radius=1, amount=1)

# Display the results


plt.figure(figsize=(12, 6))

plt.subplot(1, 2, 1)
plt.imshow(image, cmap='gray')
plt.title('Original Image')
plt.axis('off')

plt.subplot(1, 2, 2)
plt.imshow(sharpened_image_skimage, cmap='gray')
plt.title('Sharpened Image (skimage)')
plt.axis('off')

plt.show()
(6) Sharpening Using openCV Built-in Functions
# Define a 3x3 sharpening filter kernel
sharpening_filter_kernel_cv2 = np.array([[ 0, -1, 0],
[-1, 5, -1],
[ 0, -1, 0]])

# Apply sharpening using OpenCV's filter2D function


sharpened_image_cv2 = cv2.filter2D(image, -1, sharpening_filter_kernel_cv2)

# Display the results


plt.figure(figsize=(12, 6))

plt.subplot(1, 2, 1)
plt.imshow(image, cmap='gray')
plt.title('Original Image')
plt.axis('off')

plt.subplot(1, 2, 2)
plt.imshow(sharpened_image_cv2, cmap='gray')
plt.title('Sharpened Image (OpenCV)')
plt.axis('off')

plt.show()
9. Noise reduction with given image using filtering tech

(1) Manual Implementation (Using Gaussian Filter)


import numpy as np
from skimage import io, img_as_float, img_as_ubyte
import matplotlib.pyplot as plt
from scipy.ndimage import gaussian_filter

# Read and prepare the image


image = io.imread('input_image.jpg')
image = img_as_float(image)

# Convert image to grayscale if it is not already


if len(image.shape) == 3:
image = np.mean(image, axis=2)

# Manual Gaussian filter implementation


def gaussian_kernel(size, sigma):
"""Create a Gaussian kernel."""
kernel = np.fromfunction(
lambda x, y: (1 / (2 * np.pi * sigma ** 2)) *
np.exp(- ((x - (size - 1) / 2) ** 2 + (y - (size - 1) / 2) ** 2) / (2 * sigma ** 2)),
(size, size)
)
return kernel / np.sum(kernel)

def apply_filter(image_array, kernel):


"""Apply a given kernel to an image."""
height, width = image_array.shape
k_height, k_width = kernel.shape
pad_height = k_height // 2
pad_width = k_width // 2

padded_image = np.pad(image_array, ((pad_height, pad_height), (pad_width, pad_width)),


mode='constant')
result_image = np.zeros_like(image_array)

for y in range(height):
for x in range(width):
region = padded_image[y:y+k_height, x:x+k_width]
result_image[y, x] = np.sum(region * kernel)

return np.clip(result_image, 0, 1) # Ensure the pixel values are in range [0, 1]

# Create a Gaussian kernel


gaussian_kernel_size = 5
sigma = 1.0
gaussian_kernel_matrix = gaussian_kernel(gaussian_kernel_size, sigma)

# Apply the Gaussian filter


filtered_image = apply_filter(image, gaussian_kernel_matrix)

# Display the results


plt.figure(figsize=(12, 6))
plt.subplot(1, 2, 1)
plt.imshow(image, cmap='gray')
plt.title('Original Image')
plt.axis('off')

plt.subplot(1, 2, 2)
plt.imshow(filtered_image, cmap='gray')
plt.title('Filtered Image (Manual)')
plt.axis('off')

plt.show()
(2) Built-in Function Implementation Using skimage
from skimage import filters

# Apply Gaussian filter using skimage


filtered_image_skimage = filters.gaussian(image, sigma=sigma)

# Display the results


plt.figure(figsize=(12, 6))

plt.subplot(1, 2, 1)
plt.imshow(image, cmap='gray')
plt.title('Original Image')
plt.axis('off')

plt.subplot(1, 2, 2)
plt.imshow(filtered_image_skimage, cmap='gray')
plt.title('Filtered Image (skimage)')
plt.axis('off')

plt.show()
(3) Built-in Function Implementation Using OpenCV
import cv2

# Convert image to grayscale if it is not already


if len(image.shape) == 3:
image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)

# Apply Gaussian filter using OpenCV


filtered_image_cv2 = cv2.GaussianBlur(image, (gaussian_kernel_size, gaussian_kernel_size),
sigmaX=sigma)

# Display the results


plt.figure(figsize=(12, 6))

plt.subplot(1, 2, 1)
plt.imshow(image, cmap='gray')
plt.title('Original Image')
plt.axis('off')

plt.subplot(1, 2, 2)
plt.imshow(filtered_image_cv2, cmap='gray')
plt.title('Filtered Image (OpenCV)')
plt.axis('off')

plt.show()
10. Solving a segmentation problem (point/line/edge
detection)

(1) Point Detection (Corner Detection)


(i) Manual Implementation (Using Harris Corner Detection)
import numpy as np
from skimage import io, img_as_float, color
import matplotlib.pyplot as plt
from scipy.ndimage import gaussian_filter
from scipy import signal

# Read and prepare the image


image = io.imread('input_image.jpg')
image = img_as_float(image)

# Convert image to grayscale if it is not already


if len(image.shape) == 3:
image = color.rgb2gray(image)

# Harris Corner Detection manually


def harris_corner_detection(image_array, sigma=1):
height, width = image_array.shape

# Compute gradients
dx = np.gradient(image_array, axis=1)
dy = np.gradient(image_array, axis=0)
# Compute products of gradients
Ix2 = gaussian_filter(dx[0]**2, sigma)
Iy2 = gaussian_filter(dy[0]**2, sigma)
Ixy = gaussian_filter(dx[0]*dy[0], sigma)

# Compute the Harris response


k = 0.04
R = (Ix2 * Iy2 - Ixy**2) - k * (Ix2 + Iy2)**2

return R

# Apply Harris corner detection


corners = harris_corner_detection(image)

# Display the results


plt.figure(figsize=(12, 6))
plt.subplot(1, 2, 1)
plt.imshow(image, cmap='gray')
plt.title('Original Image')
plt.axis('off')

plt.subplot(1, 2, 2)
plt.imshow(corners, cmap='hot')
plt.title('Harris Corners (Manual)')
plt.axis('off')
plt.show()
(ii) Using Built-in Function skimage
from skimage.feature import corner_harris, corner_peaks

# Apply Harris corner detection using skimage


corners = corner_harris(image)
corner_peaks_result = corner_peaks(corners, min_distance=5)

# Display the results


plt.figure(figsize=(12, 6))
plt.subplot(1, 2, 1)
plt.imshow(image, cmap='gray')
plt.title('Original Image')
plt.axis('off')

plt.subplot(1, 2, 2)
plt.imshow(image, cmap='gray')
plt.scatter(corner_peaks_result[:, 1], corner_peaks_result[:, 0], c='red', s=1)
plt.title('Harris Corners (skimage)')
plt.axis('off')

plt.show()
(2) Line Detection
(i) Manual Implementation (Using Hough Line Transform)
from skimage import transform, feature
from skimage.color import rgb2gray
# Apply edge detection
edges = feature.canny(image)

# Hough Transform for line detection


def hough_line_transform(edge_image):
hspace, angles, distances = transform.hough_line(edge_image)
return hspace, angles, distances

# Apply Hough Line Transform


hspace, angles, distances = hough_line_transform(edges)

# Display results
plt.figure(figsize=(12, 6))
plt.subplot(1, 2, 1)
plt.imshow(edges, cmap='gray')
plt.title('Edges')
plt.axis('off')

plt.subplot(1, 2, 2)
plt.imshow(np.log(1 + hspace), cmap='hot')
plt.title('Hough Space')
plt.axis('off')
plt.show()
(ii) Using Built-in Function skimage
from skimage.transform import probabilistic_hough_line

# Apply probabilistic Hough line transform


lines = probabilistic_hough_line(edges, threshold=10, line_length=5, line_gap=3)

# Display results
plt.figure(figsize=(12, 6))
plt.imshow(image, cmap='gray')
for line in lines:
p0, p1 = line
plt.plot((p0[1], p1[1]), (p0[0], p1[0]), 'r-')
plt.title('Detected Lines (skimage)')
plt.axis('off')
plt.show()

(3) Edge Detection


(i) Manual Implementation (Using Sobel Operator)
from scipy.ndimage import convolve

# Sobel filters
sobel_x = np.array([[1, 0, -1],
[2, 0, -2],
[1, 0, -1]])
sobel_y = np.array([[1, 2, 1],
[0, 0, 0],
[-1, -2, -1]])

# Apply Sobel filters


edges_x = convolve(image, sobel_x)
edges_y = convolve(image, sobel_y)
edges = np.sqrt(edges_x**2 + edges_y**2)

# Display the results


plt.figure(figsize=(12, 6))
plt.subplot(1, 2, 1)
plt.imshow(image, cmap='gray')
plt.title('Original Image')
plt.axis('off')

plt.subplot(1, 2, 2)
plt.imshow(edges, cmap='gray')
plt.title('Edges (Manual)')
plt.axis('off')

plt.show()
(ii) Using Built-in Function skimage
from skimage import filters

# Apply edge detection using skimage's Sobel filter


edges_skimage = filters.sobel(image)

# Display results
plt.figure(figsize=(12, 6))
plt.subplot(1, 2, 1)
plt.imshow(image, cmap='gray')
plt.title('Original Image')
plt.axis('off')

plt.subplot(1, 2, 2)
plt.imshow(edges_skimage, cmap='gray')
plt.title('Edges (skimage)')
plt.axis('off')

plt.show()
11. Use of thresholding for a given problem

(1) Manual Thresholding Implementation


import numpy as np
from skimage import io, img_as_float
import matplotlib.pyplot as plt

# Read and prepare the image


image = io.imread('input_image.jpg')
image = img_as_float(image)

# Convert image to grayscale if it is not already


if len(image.shape) == 3:
image = np.mean(image, axis=2)

# Apply manual thresholding


def manual_thresholding(image_array, threshold):
binary_image = image_array > threshold
return binary_image

# Define a threshold value


threshold = 0.5

# Apply the threshold


binary_image = manual_thresholding(image, threshold)

# Display results
plt.figure(figsize=(12, 6))
plt.subplot(1, 2, 1)
plt.imshow(image, cmap='gray')
plt.title('Original Image')
plt.axis('off')

plt.subplot(1, 2, 2)
plt.imshow(binary_image, cmap='gray')
plt.title('Binary Image (Manual)')
plt.axis('off')

plt.show()
(2) Built-in Function Implementation Using skimage
from skimage import filters

# Apply Otsu's thresholding using skimage


threshold_otsu = filters.threshold_otsu(image)
binary_image_skimage = image > threshold_otsu

# Display results
plt.figure(figsize=(12, 6))
plt.subplot(1, 2, 1)
plt.imshow(image, cmap='gray')
plt.title('Original Image')
plt.axis('off')

plt.subplot(1, 2, 2)
plt.imshow(binary_image_skimage, cmap='gray')
plt.title('Binary Image (Otsu - skimage)')
plt.axis('off')

plt.show()
(3) Built-in Function Implementation Using openCV for Binary Thresholding
import cv2

# Convert image to grayscale if it is not already


if len(image.shape) == 3:
image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)

# Apply binary thresholding using OpenCV


_, binary_image_cv2 = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)

# Display results
plt.figure(figsize=(12, 6))
plt.subplot(1, 2, 1)
plt.imshow(image, cmap='gray')
plt.title('Original Image')
plt.axis('off')

plt.subplot(1, 2, 2)
plt.imshow(binary_image_cv2, cmap='gray')
plt.title('Binary Image (OpenCV)')
plt.axis('off')

plt.show()
(4) Built-in Function Implementation Using openCV for Adaptive Thresholding
# Apply adaptive thresholding using OpenCV
binary_image_adaptive_cv2 = cv2.adaptiveThreshold(image, 255,
cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)

# Display results
plt.figure(figsize=(12, 6))
plt.subplot(1, 2, 1)
plt.imshow(image, cmap='gray')
plt.title('Original Image')
plt.axis('off')

plt.subplot(1, 2, 2)
plt.imshow(binary_image_adaptive_cv2, cmap='gray')
plt.title('Binary Image (Adaptive - OpenCV)')
plt.axis('off')

plt.show()

You might also like