Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Piyush Kushwah (0901AM211036)

Experiment - 4
AIM: Write a Program to Implement Point Operations
Description: The provided Python program enables the implementation of various
point operations on an input image. It utilizes the OpenCV library to read an image from
a specified file path and applies the selected point operation to each pixel in the image.
1. Digital Negative Transformation: This transformation involves computing the
negative of each pixel value, resulting in an inverted image where light pixels
become dark and vice versa.
2. Thresholding: Thresholding is a binary operation that converts grayscale
images into binary images by assigning a pixel value of either 0 or 255 based on a
specified threshold value. Pixels with intensity values below the threshold are
set to 0 (black), while pixels above the threshold are set to 255 (white).
3. Clipping: Clipping involves limiting pixel values to a specified range. This
operation ensures that pixel values fall within a defined intensity range,
preventing overexposure or underexposure in the image.
4. Logarithmic Transformation: This transformation enhances the contrast of
low-intensity pixels while compressing the higher intensity values. It is
particularly useful for images with low contrast or a wide dynamic range.
5. Power-law Transformation: Also known as gamma correction, this
transformation adjusts pixel values using a power-law function. It is used to
modify the brightness and contrast of images, allowing for non-linear
adjustments.
import cv2
import numpy as np
from google.colab.patches import cv2_imshow

!wget -O image.jpg
https://imgeng.jagran.com/images/2023/oct/Jarvo1696758979907.jpg

--2024-03-02 10:12:54--
https://imgeng.jagran.com/images/2023/oct/Jarvo1696758979907.jpg
Resolving imgeng.jagran.com (imgeng.jagran.com)... 23.64.98.217,
2600:1406:6c00:181::18c7, 2600:1406:6c00:180::18c7
Connecting to imgeng.jagran.com (imgeng.jagran.com)|
23.64.98.217|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 391831 (383K) [image/jpg]
Saving to: ‘image.jpg’

image.jpg 100%[===================>] 382.65K 1.90MB/s


in 0.2s

2024-03-02 10:12:55 (1.90 MB/s) - ‘image.jpg’ saved [391831/391831]


image_path = "image.jpg"

def digital_negative(image_path):
image = cv2.imread(image_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
L_minus_1 = np.max(image)
S = (L_minus_1 - image).astype(np.uint8)
negative_image = cv2.cvtColor(S, cv2.COLOR_RGB2BGR)
return negative_image
def image_thresholding(image_path, threshold_value):
image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
L_minus_one = np.max(image)
for i in range(len(image)):
for j in range(len(image[i])):
if image[i][j] >= threshold_value:
image[i][j] = L_minus_one
else:
image[i][j] = 0
return image
def image_clipping(image_path, min_value, max_value):
image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
L_minus_one = np.max(image)
for i in range(len(image)):
for j in range(len(image[i])):
if min_value <= image[i][j] <= max_value:
image[i][j] = L_minus_one
else:
image[i][j] = 0
return image
def logarithmic_transformation(image_path):
# Read the image
image = np.float32(cv2.imread(image_path))
L = np.max(image)

# Calculate the constant C


c = L/np.log( 1 + L )

# Apply the logarithmic transformations


log_transformed = c * np.log(1+image)

# Scale the pixel values to the range [0, 255]


transformed_image = np.uint8(log_transformed)

return transformed_image
def power_law(image_path, c, gamma):
image = cv2.imread(image_path)
image = c * (image**gamma)
return image

negative_image = digital_negative(image_path)
cv2_imshow(cv2.putText(cv2.imread(image_path), "Original Image",
(10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2))
cv2_imshow(cv2.putText(negative_image, "Digital Negative", (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2))
cv2.waitKey(0)
cv2.destroyAllWindows()

threshold_value = 140
thresholded_image = image_thresholding(image_path, threshold_value)
original_image = cv2.imread(image_path)
cv2_imshow(cv2.putText(original_image, "Original Image", (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2))
cv2_imshow(cv2.putText(thresholded_image, "Thresholded Image", (10,
30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2))
cv2.waitKey(0)
cv2.destroyAllWindows()
# Define the lower and upper bounds for clipping
mini,maxi = 130,200

# Apply image thresholding


clipped_image = image_clipping(image_path, mini,maxi)

# Display the original and thresholded images


original_image = cv2.imread(image_path)
cv2_imshow(cv2.putText(original_image, "Original Image", (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2))
cv2_imshow(cv2.putText(clipped_image, "Clipped Image", (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2))
cv2.waitKey(0)
cv2.destroyAllWindows()
# Apply image thresholding
logarithmic_image = logarithmic_transformation(image_path)

# Display the original and thresholded images


original_image = cv2.imread(image_path)
cv2_imshow(cv2.putText(original_image, "Original Image", (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2))
cv2_imshow(cv2.putText(logarithmic_image, "Logarithmic Image", (10,
30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2))
cv2.waitKey(0)
cv2.destroyAllWindows()
# Define the lower and upper bounds for clipping
c,gamma = 1,1.1

# Apply image thresholding


power_law_transformed_image = power_law(image_path, c,gamma)

# Display the original and thresholded images


original_image = cv2.imread(image_path)
cv2_imshow(cv2.putText(original_image, "Original Image", (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2))
cv2_imshow(cv2.putText(power_law_transformed_image, "Power-Law
Transformed Image", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,
255, 255), 2))
cv2.waitKey(0)
cv2.destroyAllWindows()
Conclusion: this experiment demonstrates the implementation of various point
operations for digital negative, thresholding, clipping, logarithmic, and power-law
transformations using Python and OpenCV. These operations play crucial roles in image
enhancement, contrast adjustment, and feature extraction in digital image processing.
By providing a flexible and customizable framework, the program enables users to
apply a range of transformations to images, enhancing their visual quality and
facilitating further analysis. This experiment lays the groundwork for exploring
advanced image processing techniques and their applications in diverse domains.

You might also like