Assignment 1
Assignment 1
Total Marks: 04
Obtained Marks:
Computer Vision
Assignment # 01
Last Date of Submission: 12th October 2024
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
Answer:
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Haisam Abbas
# Reg: 23108406
def load_image(filename):
return cv2.imread(filename)
# 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)
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.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)
# Load image
img = load_image('imi3.jpg')
height, width = img.shape[:2]
# 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.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.
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
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
5. Subtract Scalar
6. Multiply Scalar
The Bitwise AND operation between the original image and the gradient image results in a
progressive darkening 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:
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.
The Bitwise NOT operation on the original image results in a complete inversion of brightness:
The End
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology University