Uploaded by Abdul Rehman Abbasi

OpenCV Image Processing in Python

advertisement
keyboard_arrow_down Import necessary libraries
!pip install opencv-python
!pip install opencv-python-headless
Requirement already satisfied: opencv-python in /usr/local/lib/python3.11/dist-packages (4.10.0.84)
Requirement already satisfied: numpy>=1.21.2 in /usr/local/lib/python3.11/dist-packages (from opencv-python) (1.26.4)
Requirement already satisfied: opencv-python-headless in /usr/local/lib/python3.11/dist-packages (4.11.0.86)
Requirement already satisfied: numpy>=1.21.2 in /usr/local/lib/python3.11/dist-packages (from opencv-python-headless) (1.26.4)
from google.colab import files
uploaded = files.upload()
# After uploading, you can access the image by its filename
image_path = list(uploaded.keys())[0] # This will get the name of the uploaded file
Choose Files images.jfif
images.jfif(image/jpeg) - 4987 bytes, last modified: 1/31/2025 - 100% done
Saving images.jfif to images (1).jfif
from google.colab import files
uploaded = files.upload()
# After uploading, you can access the image by its filename
image_path = list(uploaded.keys())[0] # This will get the name of the uploaded file
Choose Files images.jfif
images.jfif(image/jpeg) - 4987 bytes, last modified: 1/31/2025 - 100% done
Saving images.jfif to images.jfif
import cv2
import numpy as np
import requests
import urllib
# URL of the image
url = 'https://example.com/your_image.jpg'
# Replace with the actual URL
# Read the image from the URL
image_response = requests.get(url)
image_array = np.asarray(bytearray(image_response.content), dtype=np.uint8)
image = cv2.imdecode(image_array, cv2.IMREAD_COLOR)
# Now you can use the 'image' variable in your operations
from google.colab import files
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Upload the image
uploaded = files.upload()
image_path = list(uploaded.keys())[0]
image = cv2.imread(image_path)
# Get the uploaded image filename
# The rest of your image processing code goes here...
Choose Files images.jfif
images.jfif(image/jpeg) - 4987 bytes, last modified: 1/31/2025 - 100% done
Saving images.jfif to images (2).jfif
# Import necessary libraries
import cv2
import numpy as np
import matplotlib.pyplot as plt
from google.colab import files
# Step 1: Upload the image
uploaded = files.upload()
image_path = list(uploaded.keys())[0]
image = cv2.imread(image_path)
# Get the uploaded image filename
# Function to display images
def display_images(images, titles):
plt.figure(figsize=(15, 10))
for i in range(len(images)):
plt.subplot(2, 3, i + 1)
plt.imshow(cv2.cvtColor(images[i], cv2.COLOR_BGR2RGB))
plt.title(titles[i])
plt.axis('off')
plt.show()
# 1. Sharpening of image
def sharpen_image(image):
kernel = np.array([[0, -1, 0],
[-1, 5, -1],
[0, -1, 0]])
sharpened = cv2.filter2D(image, -1, kernel)
return sharpened
sharpened_image = sharpen_image(image)
# 2. Rotation
def rotate_image(image, angle):
(h, w) = image.shape[:2]
center = (w // 2, h // 2)
M = cv2.getRotationMatrix2D(center, angle, 1.0)
rotated = cv2.warpAffine(image, M, (w, h))
return rotated
rotated_image = rotate_image(image, 45)
# Rotate by 45 degrees
# 3. Translation
def translate_image(image, tx, ty):
M = np.float32([[1, 0, tx], [0, 1, ty]])
translated = cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))
return translated
translated_image = translate_image(image, 50, 30)
# Translate by (50, 30)
# 4. Scaling to 2X
def scale_image(image, scale):
width = int(image.shape[1] * scale)
height = int(image.shape[0] * scale)
scaled = cv2.resize(image, (width, height), interpolation=cv2.INTER_LINEAR)
return scaled
scaled_image = scale_image(image, 2)
# 5. Mirroring
def mirror_image(image):
mirrored = cv2.flip(image, 1)
return mirrored
# Scale to 2X
# 1 for horizontal flip
mirrored_image = mirror_image(image)
# Display all images
images = [image, sharpened_image, rotated_image, translated_image, scaled_image, mirrored_image]
titles = ['Original Image', 'Sharpened Image', 'Rotated Image', 'Translated Image', 'Scaled Image', 'Mirrored Image']
display_images(images, titles)
Choose Files images.jfif
images.jfif(image/jpeg) - 4987 bytes, last modified: 1/31/2025 - 100% done
Saving images.jfif to images (3).jfif
# Save output images
cv2.imwrite('sharpened_image.jpg', sharpened_image)
cv2.imwrite('rotated_image.jpg', rotated_image)
cv2.imwrite('translated_image.jpg', translated_image)
cv2.imwrite('scaled_image.jpg', scaled_image)
cv2.imwrite('mirrored_image.jpg', mirrored_image)
print("Output images saved successfully.")
Output images saved successfully.
Download