Week 8
Week 8
WEEK-8
Objective:
Implementation of basic image processing.
Theory:
In image processing, the operation to read and display an image is fundamental and
involves two key steps. First, reading an image typically entails loading the image file
from a storage medium (such as a hard drive) into memory. This process is managed
by an image processing library or software, which decodes the image file format (such
as JPEG, PNG, or TIFF) and represents the image data as a matrix of pixel values.
Each pixel is associated with colour information (e.g., RGB values) and intensity. The
second step, displaying the image, involves rendering this matrix on a visual output
device like a computer screen or monitor.
Program:
import numpy as np
from google.colab.patches import cv2_imshow
import cv2
image=cv2.imread("/content/photo.jpg")
cv2_imshow(image)
Output:
22331A1241
Theory:
In image processing, the operation to save an image involves storing the processed or
original image data to a file format that can be retrieved and utilised later. This
process begins with converting the image matrix, which consists of pixel values and
associated metadata (such as colour profiles and resolution), into a specific file format
(e.g., JPEG, PNG, TIFF). The image processing software or library encodes the pixel
data and metadata according to the chosen file format's specifications. This encoding
includes compression, if applicable, to reduce file size while preserving image quality.
The final step is writing the encoded data to a storage medium, such as a hard drive or
cloud storage, resulting in a file that can be accessed for future use. Saving an image
ensures that modifications, annotations, or original captures are preserved and can be
easily retrieved for further processing, sharing, or archival.
Program:
cv2.imwrite("nature.png",image)
Output:
TRUE
22331A1241
Theory:
In image processing, accessing and modifying the pixels of an image involves directly
interacting with the fundamental elements of the image matrix, where each pixel
represents a specific colour or intensity value. To access pixels, image processing
software retrieves the pixel values from a digital image, typically represented as a
two-dimensional array of colour channels (e.g., RGB for colour images or grayscale
values). This access allows for the examination or manipulation of individual pixel
values, such as adjusting brightness, contrast, or colour balance. Modifying pixels
involves altering these values based on desired operations or algorithms, such as
applying filters, adjusting colours, or performing geometric transformations. By
manipulating pixel values, one can enhance image quality, correct distortions, or
apply artistic effects, making this operation crucial for tasks ranging from basic photo
editing to complex computer vision applications.
Program:
Image.shape
print(image[100][100])
print(image[0][0])
image[0][0]=255
print(image[0,0])
cv2_imshow(image)
Output:
22331A1241
Theory:
In image processing, displaying image properties involves extracting and presenting
key metadata and attributes of an image to provide insights into its characteristics.
This operation typically includes retrieving information such as image dimensions
(width and height), colour depth (bit depth), resolution (dpi), and file size.
Additionally, properties like colour space (RGB, CMYK, etc.), image format (JPEG,
PNG, TIFF), and metadata (EXIF data including camera settings, date of capture, and
location) may also be displayed. By presenting these properties, users gain a
comprehensive understanding of the image's technical specifications and quality,
which is essential for tasks such as quality assessment, format conversion, and
optimization.
Program:
Dimensions=image.shape
height=image.shape[0]
width=image.shape[1]
channel=image.shape[2]
size=image.size
b,r,g=cv2.split(image)
print("dimensions are:",Dimensions)
print("height is:",height)
print("width is:",width)
print("number of channels:",channel)
print("image size:",size)
print("\n")
Output:
22331A1241
Theory:
In image processing, splitting and merging of images are essential operations for
handling and manipulating complex visual data. Splitting an image involves dividing
it into smaller segments or regions, which can be useful for detailed analysis,
processing, or applying localised modifications.
Program:
B,R,G=cv2.split(image)
print("after the splitting")
cv2_imshow(b)
cv2_imshow(r)
cv2_imshow(g)
img=cv2.merge((B,R,G))
Output:
22331A1241
Theory:
In image processing, changing the colour of an image involves altering its colour
attributes to achieve desired effects or correct colour-related issues. This operation
typically entails modifying the colour channels of the image, which represent the
intensity of primary colours (e.g., red, green, blue for RGB images) or other colour
spaces (e.g., hue, saturation, and brightness for HSB/HSV images). Techniques such
as colour balancing, adjustment of hue, saturation, and brightness, or application of
colour filters are employed to achieve these changes. Additionally, colour
transformations can include converting between colour spaces (e.g., from RGB to
grayscale or sepia) or applying colour mappings to enhance certain features or artistic
effects. By manipulating these colour attributes, one can improve image aesthetics,
correct colour distortions, or highlight specific elements, thereby enhancing the
overall visual impact and functionality of the image.
Program:
import cv2
from google.colab.patches import cv2_imshow
from google.colab import files
image_path = '/content/photo.jpeg'
img = cv2.imread(image_path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray_image_path = '/content/photo.jpeg'
cv2.imwrite(gray_image_path, gray)
cv2_imshow(gray)
Output:
22331A1241
Theory:
Image resizing is a fundamental operation in image processing that involves
altering the dimensions of an image while maintaining its visual integrity. The core
theory behind image resizing revolves around interpolation techniques, which
estimate pixel values in the resized image based on the original image data. Common
methods include nearest-neighbour interpolation, which assigns the value of the
nearest pixel to the new pixel; bilinear interpolation, which calculates the average of
four surrounding pixels; and bicubic interpolation, which uses sixteen surrounding
pixels for a smoother result. Each method balances between computational efficiency
and image quality, with the choice depending on the specific application
requirements. Effective resizing maintains the image's detail and clarity, crucial for
tasks ranging from digital media editing to machine learning.
Program:
h,w =img.shape[:2]
dem=(int(w/4),int(h/4))
pyd3=cv2.resize(img,dem,interpolation=cv2.INTER_AREA)
print("äfter resize")
cv2_imshow(pyd3)
Output:
22331A1241
Theory:
In image processing, the image rotation operation involves rotating an image around a
specified centre point, usually the image's geometric centre, by a given angle. This
operation is achieved by transforming the image coordinates according to
trigonometric functions that calculate the new positions of pixels after rotation. The
process includes interpolating pixel values to handle the areas where original pixel
data does not directly map onto the new grid, using methods such as
nearest-neighbour, bilinear, or bicubic interpolation to preserve image quality and
minimise artefacts. Rotation is commonly used to correct the orientation of images,
align content for better presentation, or create dynamic effects. By adjusting the angle
of rotation, one can reposition elements within the image, enhancing its visual
composition and functionality.
Program:
center=(int(width/2),(height/2))
rotate_matrix=cv2.getRotationMatrix2D(center=center,angle=70,scale=1)
rotate_image=cv2.warpAffine(src=img,M=rotate_matrix,dsize=(width,height)
cv2_imshow(rotate_image)
Output:
22331A1241
kernel =np.ones((5,5),np.uint8)
img_erosion=cv2.erode(img,kernel, iterations=1)
img_dilation=cv2.dilate(img,kernel,iterations=1)
cv2_imshow(image)
cv2_imshow(img_erosion)
cv2_imshow(img_dilation)
Output: