Thursday, July 3, 2025

To write and execute programs for image arithmetic operations

 a. Addition of two images  

Program: 

import cv2 

img1 = cv2.imread("C:/Users/welcome/Desktop/sem6/DIP/Lenna.png") 

img2=cv2.imread("C:/Users/welcome/Desktop/sem6/DIP/Nature.png") 

img2 = cv2.resize(img2, (img1.shape[1], img1.shape[0])) 

added_image = cv2.add(img1, img2)   

cv2.imshow('Added Image', added_image) 

cv2.waitKey(0) 

cv2.destroyAllWindows()


b. Subtract one image from other image 

Program: 

import cv2 

img1 = cv2.imread("C:/Users/welcome/Desktop/sem6/DIP/Lenna.png") 

img2=cv2.imread("C:/Users/welcome/Desktop/sem6/DIP/Nature.png") 

img2 = cv2.resize(img2, (img1.shape[1], img1.shape[0])) 

subtracted_image = cv2.subtract(img1, img2)   

cv2.imshow('Subtracted Image', subtracted_image)   

cv2.waitKey(0) 

cv2.destroyAllWindows()


c. Calculate mean value of image  

Program: 

import cv2 

import numpy as np 

img1=cv2.imread("C:/Users/welcome/Desktop/sem6/DIP/Nature.png") 

mean_val = np.mean(img1) 

print(f"Mean pixel value of img1: {mean_val:.2f}") 

cv2.waitKey(0) 

cv2.destroyAllWindows() 



d. Different Brightness by changing mean value  

Program: 

import cv2 

import numpy as np 

img = cv2.imread("C:/Users/welcome/Desktop/sem6/DIP/Light.png") 

target_mean = 150 

current_mean = np.mean(img) 

difference = target_mean - current_mean 

adjusted_img = cv2.add(img, np.full(img.shape, difference, 

dtype=np.uint8)) 

cv2.imshow("Original_image", img) 

cv2.imshow('Adjusted Brightness to Target Mean', adjusted_img) 

cv2.waitKey(0) 

cv2.destroyAllWindows()

No comments:

Post a Comment

Complete Works of Swami Vivekananda [Volume 8,Page - 2069]

  Complete Works of Swami Vivekananda [ Volume 8, Page - 2069] Jesus Christ was God — the Personal God become man. He has manifested Himsel...