Thursday, July 3, 2025

To write and execute programs for image logical operations

a. AND operation between 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])) 

result = cv2.bitwise_and(img1, img2) 

cv2.imshow('AND Operation', result) 

cv2.waitKey(0) 

cv2.destroyAllWindows()



b. OR operation between 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])) 

result = cv2.bitwise_or(img1, img2) 

cv2.imshow('OR Operation', result) 

cv2.waitKey(0) 

cv2.destroyAllWindows()


c. Calculate intersection 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])) 

_, bin1 = cv2.threshold(img1, 127, 255, cv2.THRESH_BINARY) 

_, bin2 = cv2.threshold(img2, 127, 255, cv2.THRESH_BINARY) 

intersection = cv2.bitwise_and(bin1, bin2) 

cv2.imshow('Intersection', intersection) 

cv2.waitKey(0) 

cv2.destroyAllWindows()



d. Water Marking using EX-OR operation  

Program: 

import cv2 

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

watermark = 

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

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

watermarked_img = cv2.bitwise_xor(img1, watermark) 

cv2.imshow('Watermarked Image (XOR)', watermarked_img) 

cv2.waitKey(0) 

cv2.destroyAllWindows()


e. NOT operation (Negative image)  

Program: 

import cv2 

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

Negative_image = cv2.bitwise_not(img) 

cv2.imshow('original_image', img) 

cv2.imshow('Negative image', Negative_image) 

cv2.waitKey(0) 

cv2.destroyAllWindows() 



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()

To write and execute image processing programs using point processing method

 a. Obtain Negative image Program: 

 import cv2 

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

 cv2.imshow("Original_image", img) 

 img1=255-img 

 cv2.imshow("Negative_image", img1) 

 cv2.waitKey(0) 

 cv2.destroyAllWindows()


b.Obtain Flip image  

Program: 

import cv2 

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

horizontal_flip = cv2.flip(img, 1)  

vertical_flip = cv2.flip(img, 0)  

both_flip = cv2.flip(img, -1)  

cv2.imshow("Original_image", img) 

cv2.imshow("horizontal_flip_image", horizontal_flip) 

cv2.imshow("vertical_flip_image", vertical_flip) 

cv2.imshow("both_flip_image", both_flip) 

cv2.waitKey(0)  

cv2.destroyAllWindows()


c. Thresholding  

Program: 

import cv2 

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

cv2.IMREAD_GRAYSCALE) 

adaptive_thresh = cv2.adaptiveThreshold( 

    img, 255,                       

    cv2.ADAPTIVE_THRESH_GAUSSIAN_C,   

    cv2.THRESH_BINARY,        

    11, 2                          

cv2.imshow("Original_image", img) 

cv2.imshow("Adaptive Thresholding", adaptive_thresh) 

cv2.waitKey(0) 

cv2.destroyAllWindows()


d.Contrast stretching  

Program: 

import numpy as np 

import cv2 

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

min_val = np.min(img) 

max_val = np.max(img) 

stretched = ((img - min_val) * (255.0 / (max_val - 

min_val))).astype(np.uint8) 

 

cv2.imshow("Original_image", img) 

cv2.imshow("contrast_stretched", stretched) 

cv2.waitKey(0) 

cv2.destroyAllWindows()



Write given 2-D data in image file

 import numpy as np 

 import cv2 

 data=np.array([[255,0,255,0],[0,255,0,255],[255,0,255,0],[0,255,0,255]], dtype=np.uint8)

 cv2.imwrite('output_image.png',data) 

 cv2.imshow('image',data) 

 cv2.waitKey(0) 

 cv2.destroyAllWindows()

Create color image using R, G and B three separate planes. Program:

 import cv2 img = cv2.imread("C:/Users/welcome/Desktop/sem6/DIP/Lenna.png") cv2.imshow("Original_image", img) b,g,r=cv2.split(img) merge_bgr=cv2.merge((b,g,r)) cv2.imshow("Merge_image", merge_bgr) cv2.waitKey(0) cv2.destroyAllWindows()

Write given 2D image file program

 






Create color image using R, G and B three separate planes

 import cv2

import numpy as np

height, width = 300, 300

R = np.full((height, width), 255, dtype=np.uint8) # Red plane (fully red)

G = np.zeros((height, width), dtype=np.uint8) # Green plane (zero)

B = np.full((height, width), 100, dtype=np.uint8) # Blue plane (some blue)

color_image = cv2.merge([B, G, R])

cv2.imshow("Color Image from R, G, B Planes", color_image)

cv2.waitKey(0)

cv2.destroyAllWindows()

Draw image profile

 import cv2

import numpy as np

import matplotlib.pyplot as plt

image = cv2.imread('your_image.jpg', cv2.IMREAD_GRAYSCALE)

row = image.shape[0] // 2 # Middle row

profile = image[row, :] # Intensity values along that row

image_with_line = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)

cv2.line(image_with_line, (0, row), (image.shape[1], row), (0, 0, 255), 1)

cv2.imshow("Image with Profile Line", image_with_line)

plt.figure(figsize=(10, 4))

plt.title(f'Intensity Profile of Row {row}')

plt.plot(profile, color='black')

plt.xlabel('Column Index')

plt.ylabel('Intensity')

plt.grid(True)

plt.tight_layout()

plt.show()

cv2.waitKey(0)

cv2.destroyAllWindows()

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...