Write a Python program using OpenCV that performs the following tasks:
- Read and Display an Image.
- Adjust the brightness of an image.
- Modify the image contrast.
- Generate a third image using bitwise operations.
- Anaconda - Python 3.7
- Jupyter Notebook (for interactive development and execution)
Load an image from your local directory and display it.
Create a matrix of ones (with data type float64) to adjust brightness.
Create brighter and darker images by adding and subtracting the matrix from the original image.
Display the original, brighter, and darker images.
Modify the image contrast by creating two higher contrast images using scaling factors of 1.1 and 1.2 (without overflow fix).
Display the original, lower contrast, and higher contrast images.
Split the image (boy.jpg) into B, G, R components and display the channels
-
Name: Ananda Rakshan K V
-
Register Number: 212223230014
import cv2
import numpy as np
import matplotlib.pyplot as plt
img_bgr =cv2.imread('Eagle_in_Flight.jpg',0)
img_bgr.shape
plt.imshow(img_bgr,cmap='gray')
plt.title('BGR Image')
plt.axis('on')
plt.show()
img=cv2.imread('Eagle_in_Flight.jpg')
cv2.imwrite('Eagle.png',img)
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(img_rgb)
plt.title("RGB IMAGE")
plt.axis("on")
plt.show()
img_rgb.shape
cropped=img_rgb[20:420,200:550]
plt.imshow(cropped[:,:,::-1])
plt.title("CROPPED IMAGE")
plt.axis("off")
plt.show()
res= cv2.resize(cropped,(200*2, 200*2))
flipped_img = cv2.flip(cropped, 1)
plt.imshow(flipped_img[:,:,::-1])
plt.title('Flipped Horizontal')
plt.axis('off')
plt.show()
img=cv2.imread('Apollo-11-launch.jpg',cv2.IMREAD_COLOR)
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
text = cv2.putText(img_rgb, "Apollo 11 Saturn V Launch, July 16, 1969", (300, 700),cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
plt.imshow(text, cmap='gray')
plt.title("TEXT IMAGE")
plt.show()
rcol= (255, 0, 255)
cv2.rectangle(img_rgb, (400, 100), (800, 650), rcol, 3)
plt.title("Annotated image")
plt.imshow(img_rgb)
plt.show()
img =cv2.imread('boy.jpg',cv2.IMREAD_COLOR)
img_rgb= cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
m = np.ones(img_rgb.shape, dtype="uint8") * 50
img_brighter = cv2.add(img_rgb, m)
img_darker = cv2.subtract(img_rgb, m)
plt.figure(figsize=(10,5))
plt.subplot(1,3,1), plt.imshow(img_rgb), plt.title("Original Image"), plt.axis("off")
plt.subplot(1,3,2), plt.imshow(img_brighter), plt.title("Brighter Image"), plt.axis("off")
plt.subplot(1,3,3), plt.imshow(img_darker), plt.title("Darker Image"), plt.axis("off")
plt.show()
matrix1 = np.ones(img_rgb.shape) * 0.8
matrix2 = np.ones(img_rgb.shape,) * 1.2
img_lower = np.uint8(cv2.multiply(np.float64(img_rgb),matrix1))
img_higher = np.uint8(cv2.multiply(np.float64(img_rgb),matrix2))
plt.figure(figsize=[18,5])
plt.subplot(131), plt.imshow(img_rgb), plt.title("Original Image"), plt.axis("off")
plt.subplot(132), plt.imshow(img_lower), plt.title("Lower Contrast"), plt.axis("off")
plt.subplot(133), plt.imshow(img_higher), plt.title("Higher Contrast"), plt.axis("off")
plt.show()
b, g, r = cv2.split(img)
plt.figure(figsize=(10,5))
plt.subplot(1,3,1), plt.imshow(b), plt.title("Blue Channel"), plt.axis("off")
plt.subplot(1,3,2), plt.imshow(g), plt.title("Green Channel"), plt.axis("off")
plt.subplot(1,3,3), plt.imshow(r), plt.title("Red Channel"), plt.axis("off")
plt.show()
merged_rgb = cv2.merge([r, g, b])
plt.figure(figsize=(5,5))
plt.imshow(merged_rgb)
plt.title("Merged RGB Image")
plt.axis("off")
plt.show()
hsv_img = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
h, s, v = cv2.split(hsv_img)
plt.figure(figsize=(10,5))
plt.subplot(1,3,1), plt.imshow(h), plt.title("Hue Channel"), plt.axis("off")
plt.subplot(1,3,2), plt.imshow(s), plt.title("Saturation Channel"), plt.axis("off")
plt.subplot(1,3,3), plt.imshow(v), plt.title("Value Channel"), plt.axis("off")
plt.show()
merged_hsv = cv2.cvtColor(cv2.merge([h, s, v]), cv2.COLOR_HSV2RGB)
combined = np.concatenate((img_rgb, merged_hsv), axis=1)
plt.figure(figsize=(10, 5))
plt.imshow(combined)
plt.title("Original Image & Merged HSV Image")
plt.axis("off")
plt.show()
-
i) Read and Display an Image.
1.Read 'Eagle_in_Flight.jpg' as grayscale and display:
2.Save image as PNG and display:
3.Cropped image:
4.Resize and flip Horizontally:
5.Read 'Apollo-11-launch.jpg' and Display the final annotated image:
-
ii) Adjust Image Brightness.
1.Create brighter and darker images and display:
-
iii) Modify Image Contrast.

-
iv) Generate Third Image Using Bitwise Operations.
1.Split 'Boy.jpg' into B, G, R components and display:

2.Merge the R, G, B channels and display:
3.Split the image into H, S, V components and display:

4.Merge the H, S, V channels and display:
Thus, the images were read, displayed, brightness and contrast adjustments were made, and bitwise operations were performed successfully using the Python program.