Experiment 7 Aim: To Study and Implement Image Negative, Gray Level Slicing and Thresholding Code: 1. Negative of An Image

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

Mrunal Dilip Pilankar BE CMPN-A 50 Batch : 3

Experiment 7
Aim: To study and implement image negative , gray level slicing and thresholding

Code:

1. Negative of an image:
import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('flower.jpeg')
grey_level= range(0,256)
print("Original image")
cv2_imshow(img)

L=256
S=[(255-i) for i in img]
a=np.asarray(S)

print("Image negative : ")


cv2_imshow(a)

OUTPUT:
2. Gray level Slicing:

img = cv2.imread('wiki.jpg',0)
r1=int(input("Enter the starting range value: "))
r2=int(input("Enter the starting range value: "))

img = cv2.imread('flower.jpeg',0)
print("Original image : ")
cv2_imshow(img)

w= np.arange(r1,r2,1)

final=[]
for pix in img:
S=[]
for i in pix:
if i in w:
S.append(255)
else:
S.append(0)
final.append(S)

final=np.asarray(final)

print("After grey level slicing : ")


cv2_imshow(final)
OUTPUT:

3. Thresholding:

img = cv2.imread('wiki.jpg',0)
thresh=int(input("Enter threshold value : "))

print("Original image : ")


cv2_imshow(img)

final=[]
for pix in img:
S=[]
for i in pix:
if i > thresh:
S.append(255)
else:
S.append(0)
final.append(S)

final=np.asarray(final)

print("After thresholding : ")


cv2_imshow(final)

OUTPUT:

You might also like