Exp4 2
Exp4 2
Exp4 2
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’
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)
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