0% found this document useful (0 votes)
2 views7 pages

Basic Image Processing

The document provides a comprehensive guide on basic image processing techniques using OpenCV, including reading, displaying, and converting images to grayscale. It covers advanced topics such as image filtering with Gaussian and Canny edge detection, image transformation through rotation and affine transformation, and enhancement techniques like brightness adjustment and histogram equalization. Additionally, it discusses noise removal methods including median and bilateral filtering, with code examples for each operation.

Uploaded by

Elakkiya Sri
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

Basic Image Processing

The document provides a comprehensive guide on basic image processing techniques using OpenCV, including reading, displaying, and converting images to grayscale. It covers advanced topics such as image filtering with Gaussian and Canny edge detection, image transformation through rotation and affine transformation, and enhancement techniques like brightness adjustment and histogram equalization. Additionally, it discusses noise removal methods including median and bilateral filtering, with code examples for each operation.

Uploaded by

Elakkiya Sri
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1) Basic Image Processing

a. Reading and displaying an image using OpenCV/PIL

CODE:
import cv2 from [Link] import cv2_imshow
img = [Link]('[Link]')
if img is None:
raise Exception("Image file not found! Please check the filename or
upload again.")
cv2_imshow(img)
[Link](0)
[Link]()

OUTPUT:

b. Convert color image to gray scale

CODE:

Import cv2
from [Link] import cv2_imshow
try:
img = [Link]('[Link]')
if img is None:
raise Exception("Image file not found! Check the filename or
upload again.")
print("Original Image:")
cv2_imshow(img)
gray = [Link](img, cv2.COLOR_BGR2GRAY)print("Grayscale Image:")
cv2_imshow(gray)
[Link]('sample [Link]', gray)
print("Grayscale image saved as ‘[Link]'")
except Exception as e:
print("Error:", e)
OUTPUT:

2) Image Filtering
c. Gaussian Filter using OpenCV

CODE:

import cv2
from [Link] import cv2_imshow
from [Link] import files
uploaded = [Link]()
img = [Link]('sample [Link]')
if img is None:
print("Error: Image not found or unable to load.")
else:
gaussian = [Link](img, (5, 5), 0)
print("Original Image:")
cv2_imshow(img)
print("\nGaussian Filtered Image:")
cv2_imshow(gaussian)
[Link]('gaussian_filtered.jpg', gaussian)
print("\n✅ Gaussian filtered image saved as 'gaussian_filtered.jpg'")

OUTPUT:
b. Edge detection or canny

CODE:
import cv2
from [Link] import cv2_imshow
from [Link] import files
uploaded = [Link]()
try:
img = [Link](‘[Link]', cv2.IMREAD_GRAYSCALE)
if img is None:
raise Exception("Error: Image not found or unable to load!")
print("Grayscale Image:")
cv2_imshow(img)
blurred = [Link](img, (5, 5), 1.4)
print("\nBlurred Image:")
cv2_imshow(blurred)
edges = [Link](blurred, threshold1=100, threshold2=200)
print("\nCanny Edge Detection Result:")
cv2_imshow(edges)
[Link]('canny_edges.jpg', edges)
print("\n✅ Edge-detected image saved as 'canny_edges.jpg'")
except Exception as e:
print("⚠️ Error:", e)

OUTPUT:

3) Image Transformation
a. Rotate or resize image using OpenCV

CODE:

img = [Link]('sample [Link]')


if img is None:
raise Exception("Error: Image not found or unable to load!")
print("Original Image:")
cv2_imshow(img)
resized = [Link](img, (400, 400))
print("\nResized Image:")
cv2_imshow(resized)
(h, w) = [Link][:2]
center = (w // 2, h // 2)
rotation_matrix = cv2.getRotationMatrix2D(center, 45, 1.0)
rotated = [Link](resized, rotation_matrix, (w, h))
print("\nRotated Image (45°):")
cv2_imshow(rotated)
[Link]('resized_rotated.jpg', rotated)
print("\n✅ Resized and rotated image saved as 'resized_rotated.jpg'")

OUTPUT:

b. Affine

Transformation

CODE:
img = [Link]('sample [Link]')
if img is None:
raise Exception("Error: Image not found or unable to load!")
print("Original Image:")
cv2_imshow(img)
pts1 = np.float32([[50, 50], [200, 50], [50, 200]])
pts2 = np.float32([[70, 100], [220, 80], [100, 250]])
affine_matrix = [Link](pts1, pts2)
(h, w) = [Link][:2]
affine_transformed = [Link](img, affine_matrix, (w, h))
print("\nAffine Transformed Image:")
cv2_imshow(affine_transformed)
[Link]('affine_transformed.jpg', affine_transformed)
print("\n✅ Affine transformed image saved as
'affine_transformed.jpg'")

OUTPUT:
4) Image Enhancement
a. Brightness and contrast adjustment
CODE:

import cv2
from [Link] import cv2_imshow
from [Link] import files
uploaded = [Link]()
img = [Link]('sample [Link]')
if img is None:
raise Exception("Error: Image not found or unable to load!")
print("Original Image:")
cv2_imshow(img)
alpha = 1.5
beta = 50adjusted = [Link](img, alpha=alpha, beta=beta)
print("\nBrightness & Contrast Adjusted Image:")
cv2_imshow(adjusted)
[Link]('adjusted_image.jpg', adjusted)
print("\n✅ Adjusted image saved as 'adjusted_image.jpg'")

OUTPUT:

b. Histogram Equalization
CODE:
import cv2
from [Link] import cv2_imshow
import numpy as np
from [Link] import files
uploaded = [Link]()
img = [Link]('sample [Link]', cv2.IMREAD_GRAYSCALE)
if img is None:
raise Exception("Error: Image not found or unable to load!")
print("Original Grayscale Image:")
cv2_imshow(img)
equalized = [Link](img)
print("\nHistogram Equalized Image:")
cv2_imshow(equalized)
combined = [Link]((img, equalized))
print("\nOriginal and Equalized Side by Side:")
cv2_imshow(combined)
[Link]('original_gray.jpg', img)
[Link]('[Link]', equalized)
print("\n✅ Images saved as 'original_gray.jpg' and '[Link]'")

OUTPUT:

5) Noise Removal
a. Median Filtering

CODE:
import cv2
from [Link] import cv2_imshow
from [Link] import file
uploaded = [Link]()
img = [Link]('sample [Link]')
if img is None:
raise Exception("Error: Image not found or unable to load!")
print("Original Image:")
cv2_imshow(img)
kernel_size = 5
median_filtered = [Link](img, kernel_size)
print(f"\nMedian Blurred Image (Kernel size = {kernel_size}):")
cv2_imshow(median_filtered)
[Link]('median_filtered.jpg', median_filtered)
print("\n✅ Median blurred image saved as 'median_filtered.jpg'")

OUTPUT:
b. Bilateral Filtering

CODE:

import cv2
from [Link] import cv2_imshow
from [Link] import files
uploaded = [Link]()
img = [Link]('sample [Link]')
if img is None:
raise Exception("Error: Image not found or unable to load!")
print("Original Image:")
cv2_imshow(img)
bilateral_filtered = [Link](img, d=9, sigmaColor=75,
sigmaSpace=75)
print("\nBilateral Filtered Image:")
cv2_imshow(bilateral_filtered)
[Link]('bilateral_filtered.jpg', bilateral_filtered)
print("\n✅ Bilateral filtered image saved as 'bilateral_filtered.jpg'")

OUTPUT:

You might also like