Thursday, July 3, 2025

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

A physicist confesses that the electron is round and that Jesus rose from the dead. (Bengali)

একজন পদার্থবিজ্ঞানী স্বীকার করেন যে ইলেকট্রন গোলাকার এবং যিশু মৃতদের মধ্য থেকে পুনরুত্থিত হয়েছিলেন। আমার নিজের বিশ্বাস প্রমাণ দ্বারা প্রভাব...