[go: up one dir, main page]

0% found this document useful (0 votes)
11 views9 pages

Week 8

aitt

Uploaded by

22331a1237
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)
11 views9 pages

Week 8

aitt

Uploaded by

22331a1237
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
You are on page 1/ 9

22331A1237

WEEK-8
Objective: Implementation of basic image processing.

1. Aim: To read , Display and save the image.

1.1 Aim: To read and display the image.

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
color 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
img=cv2.imread("/content/download (1).jfif",1)

cv2_imshow(img)
cv2.imwrite("apple.png ",img)

Output:
1.2 Aim: To save the image.

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 utilized later. This process begins with
converting the image matrix, which consists of pixel values and associated metadata (such as
color 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
2. Aim: Accessing and modifying the pixels of an image.

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 color 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 color channels (e.g., RGB for color images or grayscale values). This access allows
for the examination or manipulation of individual pixel values, such as adjusting brightness,
contrast, or color balance. Modifying pixels involves altering these values based on desired
operations or algorithms, such as applying filters, adjusting colors, 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:
print("Images in pixels")
print(img)
print(img[0][0])
img[0][0]=135
print(img[0][0])
cv2_imshow(img)

Output:
3. Aim: To perform to display the image properties.

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), color
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:
dimension=img.shape
height=img.shape[0]
width=img.shape[1]
channel=img.shape[2]
size=img.size
b,r,g=cv2.split(img)
print("Dimension :",dimension)
print("height :",height)
print("width :",width)
print("number of channels :",channel)
print("image of size :",size)
print("\n")

Output:

Dimension : (148, 204, 3)

height : 148

width : 204

number of channels : 3
4. Aim: To perform splitting and merging of images.

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
localized modifications.
Program:
b,r,g=cv2.split(img)
print("After splitting :")
cv2_imshow(b)
cv2_imshow(r)
cv2_imshow(g)
img=cv2.merge((b,r,g))
cv2_imshow(img)

Output:
5. Aim: To change the color of the image.

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:

gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

print("GRAY SAVE")

cv2_imshow(gray)

Output:
6. Aim: To perform the resize operation on image.

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/2),int(h/2))

pyd3=cv2.resize(img,dem,interpolation=cv2.INTER_AREA)

print("After Resize")

cv2_imshow(pyd3)

Output:
7. Aim: To perform the image rotation operation on an image.

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(w/2),(h/2))

rotate_matrix=cv2.getRotationMatrix2D(center=center,angle=90,scale=1)

rotate_image=cv2.warpAffine(src=img,M=rotate_matrix,dsize=(w,h))

print("rotated_image")

cv2_imshow(rotate_image

Output:
8. Aim: To explore the erosion & dilation operations on an image.

Theory:
In image processing, erosion and dilation are fundamental morphological operations used to
manipulate the structure of binary or grayscale images. Erosion involves reducing the
boundaries of foreground objects in an image, effectively shrinking them by removing pixels
at the object’s edges. This operation is performed using a structuring element that moves over
the image, and a pixel is retained in the output image only if it fits entirely within the
structuring element. Conversely, dilation expands the boundaries of foreground objects by
adding pixels to the edges, effectively enlarging them.

Program:
kernel=np.ones((5,5),np.uint8)
img_erosin=cv2.erode(img,kernel,iterations=1)
img_dilation=cv2.dilate(img,kernel,iterations=1)
cv2_imshow(img)
cv2_imshow(img_erosin)
cv2_imshow(img_dilation)

Output:

You might also like