Aashutosh Exp

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Experiment 4

Name : Aashutosh Savita


Enrolment No. : 0901AM2110001
Branch : AIML VI
Batch : A

Write python code to perform Arithmetic operations


In [ ]: import cv2
import numpy as np
from PIL import Image

IMAGE_SHAPE = (500,450)

First Image

In [ ]: image1 = Image.open("image1.jpg").resize(IMAGE_SHAPE)
image1

Out[ ]:

Second Image

In [ ]: image2 = Image.open("image4.jpg").resize(IMAGE_SHAPE)
image2
Out[ ]:

In [ ]: image1_array, image2_array = np.array(image1), np.array(image2)

Addition: g(x,y) = f(x,y) + h(x,y)

Addition is used to combine the information in two images. Applications include development
of image restoration algorithm for molding additive noise, and special effects, such as image
morphing in motion pictures.

In [ ]: # Perform Weighted Sum


weightedSum = cv2.addWeighted(image1_array, 0.5, image2_array, 0.4, 0)

# Display the result using the cv2_imshow function


print("Weighted Sum:")
display(Image.fromarray(weightedSum))

Weighted Sum:
Subtraction: g(x,y) = f(x,y) - h(x,y)

Subtraction of two images is often used to detect motion.


consider the case where nothing has changed in a scene the image resulting from subtraction
of two Sequential image is filled with zero-a black image.
If something has moved in the scene, subtraction produces a nonzero result at the location of
movement.
Applications include Object tracking , Medical imaging, Law enforcement and Military
applications

In [ ]: # Subtract pixel values of image2 from image1


subtracted_image = cv2.subtract(image1_array, image2_array)

# Display the subtracted image using the cv2_imshow function


print("Subtracted Image:")
display(Image.fromarray(subtracted_image))

Subtracted Image:
Multiplication and Division are used to adjust the brightness of an image.

Multiplication : Multiplication of the pixel values by a number less than one will darken the
image (Brightness adjustment is often used as a processing step in image enhancement).

In [ ]: img = image1_array

# Check if the image is loaded successfully


if img is None:
print("Error: Could not read the image.")
else:
# Applying NumPy scalar multiplication on the image
fimg = (img.astype(np.float32) * 2.5).clip(0, 255).astype(np.uint8)

display(Image.fromarray(img))
display(Image.fromarray(fimg))
Division:

In [ ]: img = image1_array

# Check if the image is loaded successfully


if img is None:
print("Error: Could not read the image.")
else:
# Applying OpenCV scalar division on the image
fimg = cv2.divide(img, 2)

display(Image.fromarray(img))
display(Image.fromarray(fimg))

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy