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

Assignment 1

Uploaded by

Haisam Abbas
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Assignment 1

Uploaded by

Haisam Abbas
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Shaheed Zulfikar Ali Bhutto Institute of Science & Technology University

ROBOTICS & ARTIFICIAL INTELLIGENCE DEPARTMENT

Total Marks: 04

Obtained Marks:

Computer Vision
Assignment # 01
Last Date of Submission: 12th October 2024

Submitted To: Dr. Muhammad Junaid Umer

Student Name: Haisam Abbas

Reg. Number: 23108406


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology University

Instructions: Copied or shown assignments will be marked zero. Late submissions are not
entertained in any case.

Note: Every coding block output must include your name as print function
must submit Jupyter and Pdf files

Take images of your choice and perform the following task


Q1: Take 2 images and perform addition subtraction multiplication and
division operations
on these also perform these operations by using scaler value of your choice on
each image
(Marks=1.5)
Q2: Take a single image of your choice and perform bit wise operations AND
OR and Not on It (Marks=1.5)
Q3: After applying the above operations describe the conclusion of each
operation what changes are observed after applying each operation in terms of
brightness. (Marks=1)

Answer:

Q1: Take 2 images and perform addition subtraction multiplication and


division operations

import cv2
import numpy as np
import matplotlib.pyplot as plt

# Haisam Abbas
# Reg: 23108406

def load_image(filename):
return cv2.imread(filename)

def save_image(filename, img):


cv2.imwrite(filename, img)

def resize_images(img1, img2):


h1, w1 = img1.shape[:2]
h2, w2 = img2.shape[:2]
h = min(h1, h2)
w = min(w1, w2)
return cv2.resize(img1, (w, h)), cv2.resize(img2, (w, h))

def add_images(img1, img2):


img1, img2 = resize_images(img1, img2)
return cv2.add(img1, img2)
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology University

def subtract_images(img1, img2):


img1, img2 = resize_images(img1, img2)
return cv2.subtract(img1, img2)

def multiply_images(img1, img2):


img1, img2 = resize_images(img1, img2)
return cv2.multiply(img1, img2)

def divide_images(img1, img2):


img1, img2 = resize_images(img1, img2)
return cv2.divide(img1, img2)

def scalar_operation(img, scalar, operation):


if operation == 'add':
return cv2.add(img, scalar)
elif operation == 'subtract':
return cv2.subtract(img, scalar)
elif operation == 'multiply':
return cv2.multiply(img, float(scalar))
elif operation == 'divide':
return cv2.divide(img, float(scalar))

# Load images
img1 = load_image('/content/Imran_Ahmed_Khan_Niazi.jpg')
img2 = load_image('/content/imi_1.webp')

# Perform operations
add_result = add_images(img1, img2)
subtract_result = subtract_images(img1, img2)
multiply_result = multiply_images(img1, img2)
divide_result = divide_images(img1, img2)

# Scalar operations (using 50 as an example scalar)


scalar = 50
scalar_add_result = scalar_operation(img1, scalar, 'add')
scalar_subtract_result = scalar_operation(img1, scalar, 'subtract')
scalar_multiply_result = scalar_operation(img1, scalar, 'multiply')
scalar_divide_result = scalar_operation(img1, scalar, 'divide')

print("Haisam Abbas")
print("Reg: 23108406")
print("Image operations completed, results saved, and visualization
created.")

# Visualization
plt.figure(figsize=(20, 15))
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology University

plt.subplot(3, 3, 1), plt.imshow(cv2.cvtColor(img1, cv2.COLOR_BGR2RGB)),


plt.title('Original Image 1')
plt.subplot(3, 3, 2), plt.imshow(cv2.cvtColor(img2, cv2.COLOR_BGR2RGB)),
plt.title('Original Image 2')
plt.subplot(3, 3, 3), plt.imshow(cv2.cvtColor(add_result,
cv2.COLOR_BGR2RGB)), plt.title('Addition')
plt.subplot(3, 3, 4), plt.imshow(cv2.cvtColor(subtract_result,
cv2.COLOR_BGR2RGB)), plt.title('Subtraction')
plt.subplot(3, 3, 5), plt.imshow(cv2.cvtColor(multiply_result,
cv2.COLOR_BGR2RGB)), plt.title('Multiplication')
plt.subplot(3, 3, 6), plt.imshow(cv2.cvtColor(divide_result,
cv2.COLOR_BGR2RGB)), plt.title('Division')
plt.subplot(3, 3, 7), plt.imshow(cv2.cvtColor(scalar_add_result,
cv2.COLOR_BGR2RGB)), plt.title('Add Scalar')
plt.subplot(3, 3, 8), plt.imshow(cv2.cvtColor(scalar_subtract_result,
cv2.COLOR_BGR2RGB)), plt.title('Subtract Scalar')
plt.subplot(3, 3, 9), plt.imshow(cv2.cvtColor(scalar_multiply_result,
cv2.COLOR_BGR2RGB)), plt.title('Multiply Scalar')

plt.tight_layout()
plt.savefig('all_operations_result.png')
plt.show()

Output:
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology University
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology University

Q2: Take a single image of your choice and perform bit wise operations AND
OR and Not on It (Marks=1.5)

Answer:
import cv2
import numpy as np
import matplotlib.pyplot as plt

# Haisam Abbas
# Reg: 23108406

def load_image(filename):
return cv2.imread(filename)

def save_image(filename, img):


cv2.imwrite(filename, img)

def create_gradient(height, width):


gradient = np.linspace(0, 255, width, dtype=np.uint8)
gradient = np.tile(gradient, (height, 1))
return np.dstack((gradient, gradient, gradient))

# Load image
img = load_image('imi3.jpg')
height, width = img.shape[:2]

# Create gradient image


gradient_image = create_gradient(height, width)

# Perform bitwise operations


and_result = cv2.bitwise_and(img, gradient_image)
or_result = cv2.bitwise_or(img, gradient_image)
not_result = cv2.bitwise_not(img)

# Save results
save_image('and_result.jpg', and_result)
save_image('or_result.jpg', or_result)
save_image('not_result.jpg', not_result)

# Visualization
plt.figure(figsize=(20, 5))

plt.subplot(1, 5, 1), plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)),


plt.title('Original Image')
plt.subplot(1, 5, 2), plt.imshow(cv2.cvtColor(gradient_image,
cv2.COLOR_BGR2RGB)), plt.title('Gradient Image')
plt.subplot(1, 5, 3), plt.imshow(cv2.cvtColor(and_result,
cv2.COLOR_BGR2RGB)), plt.title('Bitwise AND')
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology University

plt.subplot(1, 5, 4), plt.imshow(cv2.cvtColor(or_result,


cv2.COLOR_BGR2RGB)), plt.title('Bitwise OR')
plt.subplot(1, 5, 5), plt.imshow(cv2.cvtColor(not_result,
cv2.COLOR_BGR2RGB)), plt.title('Bitwise NOT')

plt.tight_layout()
plt.savefig('bitwise_operations_result.png')
plt.show()

print("Haisam Abbas")
print("Reg: 23108406")
print("Bitwise operations completed, results saved, and visualization
created.")

Output:

Q3: After applying the above operations describe the conclusion of each
operation what changes are observed after applying each operation in terms of
brightness. (Marks=1)

Answer:
1. Addition:

This is the result of adding the pixel values of the first and second images. The operation sums
corresponding pixel values from both images, resulting in a combined image where details from
both inputs are visible. Notice how parts of the person from both images appear superimposed, and
areas where both images had high intensity (such as the face) appear even brighter due to the pixel
addition.

Key aspects of the addition operation:

 Brightening: In regions where both images have high pixel values (like the face), the result
becomes very bright or overexposed.
 Blending: Some features from both images are visible, especially if one of the images is
brighter than the other in certain regions.
 Clipping: If the pixel values exceed the maximum allowed intensity (for example, 255 for
8-bit images), the values are clipped, which can result in loss of detail.
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology University

2. Subtraction

 The pixel values of the second image are subtracted from the first image.
 Result: Dark areas appear because subtraction reduces pixel intensity where values are
similar, especially in overlapping areas of the images. For instance, the darker regions on
the face suggest that the corresponding areas in both images had similar pixel values, so
their difference is small.
 Interpretation: You can think of this operation as emphasizing the differences between the
two images. Where there is a large difference between corresponding pixels, you’ll see a
brighter region; where there’s little difference, it becomes darker.

3. Multiplication

 The pixel values of the two images are multiplied together.


 Result: This results in a very bright image, especially in areas where both images have high
pixel values. Multiplication amplifies these areas, making them appear overexposed.
 Interpretation: Multiplication tends to enhance brightness significantly, especially where
both images have high intensity values. However, it can lead to extreme brightness
(overexposure) in regions where pixel values are high in both images, as seen in the center
of this result.

3. Division

 The pixel values of the first image are divided by the corresponding pixel values of the
second image.
 Result: The division operation usually results in an image where darker areas dominate,
especially when dividing by values greater than 1. It can also create noise if one of the
images contains very small or zero pixel values.
 Interpretation: The division operation tends to reduce brightness and can lead to a very
dark output if there is a significant difference in the pixel intensities between the two
images. As seen in the result, most of the image is quite dark, with only faint details being
visible.

4. Add Scalar

 A constant value is added to each pixel in the image.


 Result: The entire image becomes brighter. Adding a scalar increases the pixel intensity,
making darker regions lighter and brighter regions potentially overexposed.
 Interpretation: The overall brightness of the image increases uniformly, and no specific
areas stand out more than others. This operation can be useful for brightening an image with
poor lighting conditions.

5. Subtract Scalar

 A constant value is subtracted from each pixel in the image.


 Result: The entire image becomes darker. Subtracting a scalar decreases pixel intensity,
making lighter regions darker, and potentially causing very dark regions to clip to pure
black.
 Interpretation: The image becomes uniformly dimmer. This operation can be helpful for
reducing overexposure or lowering brightness in an image where the lighting is too strong.
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology University

6. Multiply Scalar

 Each pixel in the image is multiplied by a constant scalar value.


 Result: Multiplying the pixel values amplifies both light and dark areas of the image. As
seen in the result, bright areas become extremely overexposed, while darker regions might
either become brighter or stay dark, depending on the scalar value.
 Interpretation: Multiplication increases the contrast significantly. In areas that were
already bright, the result can lead to clipping, making those areas appear completely white.
This operation is often used to adjust the contrast in an image, but when overdone, it can
lead to extreme results like the overexposure visible here.

7. Bitwise AND Operation

The Bitwise AND operation between the original image and the gradient image results in a
progressive darkening effect from left to right:

 Left side: The image appears close to its original brightness.


 Right side: The image becomes significantly darker.
 Overall effect: Creates a fading or dimming effect from left to right.

Explanation: The AND operation keeps a bit only if it's 1 in both images. As the gradient increases
in brightness from left to right, more bits are kept on the left side (where the gradient is darker) and
fewer on the right (where the gradient is brighter).

8. Bitwise OR Operation

The Bitwise OR operation between the original image and the gradient image results in a
progressive lightening effect from left to right:

 Left side: The image appears close to its original brightness.


 Right side: The image becomes significantly brighter, potentially reaching full white.
 Overall effect: Creates a brightening or "fading to white" effect from left to right.

Explanation: The OR operation keeps a bit if it's 1 in either image. As the gradient increases in
brightness from left to right, more bits are set to 1 on the right side, leading to increased brightness.

9. Bitwise NOT Operation

The Bitwise NOT operation on the original image results in a complete inversion of brightness:

 Dark areas become bright.


 Bright areas become dark.
 Colors are inverted to their complementary colors.
 Overall effect: Creates a "negative" of the original image

The End
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology University

You might also like