0% found this document useful (0 votes)
23 views21 pages

Exp 3

The document outlines an experiment focused on implementing basic image processing functions using OpenCV, including grayscale conversion, intensity transformations, drawing shapes, and histogram operations. It provides code examples for various techniques such as image negative, logarithmic transformation, and thresholding. Additionally, it covers painting on images, live streaming with webcam, and cropping using masks.

Uploaded by

harinythangavelu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views21 pages

Exp 3

The document outlines an experiment focused on implementing basic image processing functions using OpenCV, including grayscale conversion, intensity transformations, drawing shapes, and histogram operations. It provides code examples for various techniques such as image negative, logarithmic transformation, and thresholding. Additionally, it covers painting on images, live streaming with webcam, and cropping using masks.

Uploaded by

harinythangavelu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

EXPERIMENT 3 22IR005

IMAGE PROCESSING BASICS IN OPENCV

Aim:

1) To implement the following basic functions on an image or a video in Open CV:


Convert to Gray Scale, implement image intensity transformations, Draw shapes and adding
Text, Mask or Crop, Histogram,Thresholding, Image Addition and Image Subtraction.

Software/ Packages Used:

1. Libraries used:
• NumPy
• Opencv-python
• Matplotlib
• Scipy

Programs:

1. To implement the basic functions of image processing in openCV

Image intensity transformations

• Read in an image
• Find the Width, Height and Color Channels of the image
• Converting to a shade
• Converting to grayscale

1. IMAGE NEGATIVE
import cv2
import matplotlib.pyplot as plt
imgpath = 'test.tiff'
img = cv2.imread('bike.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
gray = cv2.imread('bike.jpg', 0)
colored_negative = abs(255-img)
gray_negative = abs(255-gray)
imgs = [img, gray, colored_negative, gray_negative]
title = ['colored', 'gray', 'colored-negative', 'colored-gray']

plt.subplot(2, 2, 1)
plt.title(title[0])
plt.imshow(imgs[0])
plt.xticks([])
plt.yticks([])

plt.subplot(2, 2, 2)
plt.title(title[1])
plt.imshow(imgs[1], cmap='gray')
plt.xticks([])
plt.yticks([])

plt.subplot(2, 2, 3)
plt.title(title[2])
plt.imshow(imgs[2])
plt.xticks([])
plt.yticks([])

plt.subplot(2, 2, 4)
plt.title(title[3])
plt.imshow(imgs[3], cmap='gray')
plt.xticks([])
plt.yticks([])

plt.show()

OUTPUT :
2. LOGARITHMIC TRANSFORMATION :
import cv2
import numpy as np

#open the image.


img = cv2.imread('bike.jpg')

#Apply log transforms


c = 255/(np.log(1 + np.max(img)))
log_transformed = c*np.log(1 + img)

#Specify the data type


log_transformed = np.array(log_transformed, dtype = np.uint8)
resized_image = cv2.resize(log_transformed,(250, 250))

#Save the output


cv2.imshow('resized_image', resized_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

OUTPUT :
3. POWER LAW (GAMMA) TRANSFORMATION :
import cv2
import numpy as np

#Open the image


img = cv2.imread('bike.jpg')

#Trying 4 gamma values


for gamma in [0.1, 0.5, 1.2, 2.2]:

#apply gamma correction


gamma_corrected = np.array(255*(img/255)**gamma,dtype='uint8')
#Save edited image
cv2.imshow('gamma_corrected'+str(gamma)+'.jpg', gamma_corrected)
cv2.waitKey(0)
cv2.destroyAllWindows()

OUTPUT :
4. CONTRAST STRETCHING :
import cv2
import numpy as np
# Function to map each intensity level to output intensity level.
def pixelVal(pix, r1, s1, r2, s2):
if (0 <= pix and pix <= r1):
return (s1 / r1)*pix
elif (r1 < pix and pix <= r2):
return ((s2 - s1)/(r2 - r1)) * (pix - r1) + s1
else:
return ((255 - s2)/(255 - r2)) * (pix - r2) + s2
# Open the image.
img = cv2.imread('dog.jpeg')
# Define parameters.
r1 = 70
s1 = 0
r2 = 140
s2 = 255
# Vectorize the function to apply it to each value in the Numpy array.
pixelVal_vec = np.vectorize(pixelVal)
# Apply contrast stretching.
contrast_stretched = pixelVal_vec(img, r1, s1, r2, s2)
contrast_stretched = cv2.resize(contrast_stretched, (500,500))
# Save edited image.
cv2.imshow('contrast_stretch.jpg', contrast_stretched)
cv2.waitKey(0)
cv2.destroyAllWindows()

OUTPUT :

INPUT IMAGE : OUTPUT IMAGE :


5. BIT PLANE SLICING :
import numpy as np
import cv2
img = cv2.imread('bike.jpg', 0)

lst = []
for i in range(img.shape[0]):
for j in range(img.shape[1]):
lst.append(np.binary_repr(img[i][j], width=8)) # width = no. of bits

eight_bit_img = (np.array([int(i[0]) for i in lst], dtype=np.uint8) * 128).reshape(img.shape[0],


img.shape[1])
seven_bit_img = (np.array([int(i[1]) for i in lst], dtype=np.uint8) * 64).reshape(img.shape[0],
img.shape[1])
six_bit_img = (np.array([int(i[2]) for i in lst], dtype=np.uint8) * 32).reshape(img.shape[0],
img.shape[1])
five_bit_img = (np.array([int(i[3]) for i in lst], dtype=np.uint8) * 16).reshape(img.shape[0],
img.shape[1])
four_bit_img = (np.array([int(i[4]) for i in lst], dtype=np.uint8) * 8).reshape(img.shape[0],
img.shape[1])
three_bit_img = (np.array([int(i[5]) for i in lst], dtype=np.uint8) * 4).reshape(img.shape[0],
img.shape[1])
two_bit_img = (np.array([int(i[6]) for i in lst], dtype=np.uint8) * 2).reshape(img.shape[0],
img.shape[1])
one_bit_img = (np.array([int(i[7]) for i in lst], dtype=np.uint8) * 1).reshape(img.shape[0],
img.shape[1])

finalr = cv2.hconcat([eight_bit_img, seven_bit_img, six_bit_img, five_bit_img])


finalv = cv2.hconcat([four_bit_img, three_bit_img, two_bit_img, one_bit_img])

final = cv2.vconcat([finalr, finalv])

cv2.imshow('a', final)
cv2.waitKey(0)
cv2.destroyAllWindows()

OUTPUT :
2. Paint and Draw on an image (Both in image and live stream)
1. Paint the image with certain color
2. Draw a Rectangle
3. Draw a circle
4. Draw a line
5. Write text

BLACK SCREEN AS BACKGROUND :


import numpy as np
import cv2
# initialize our canvas as a 300x300 pixel image with 3 channels
# (Red, Green, and Blue) with a black background
canvas = np.zeros((300, 300, 3), dtype="uint8")
# draw a green line from the top-left corner of our canvas to the
# bottom-right
green = (0, 255, 0)
cv2.line(canvas, (40, 250), (300, 300), green)
cv2.imshow("Canvas", canvas)
# draw a 2 pixel thick blue line from the top-right corner to the
# bottom-left
blue = (255, 0, 0)
cv2.line(canvas, (200, 50), (0, 300), blue, 2)
cv2.imshow("Canvas", canvas)
cv2.rectangle(canvas, (50, 50), (120, 120), green)
cv2.imshow("Canvas", canvas)
# draw another rectangle, this one blue with 4 pixel thickness
cv2.rectangle(canvas, (80, 150), (150, 250), blue, 4)
cv2.imshow("Canvas", canvas)
# draw a final rectangle (red and filled in )
red = (0, 0, 255)
cv2.rectangle(canvas, (200, 50), (225, 125), red, -1)
cv2.imshow("Canvas", canvas)
cv2.waitKey(0)
cv2.destroyAllWindows()
OUTPUT :
IMAGE AS BACKGROUND :
import numpy as np
import cv2
canvas = cv2.imread('bike.jpg')
# draw a green line from the top-left corner of our canvas to the
# bottom-right
green = (0, 255, 0)
cv2.line(canvas, (40, 250), (300, 300), green)
cv2.imshow("Canvas", canvas)
# draw a 2 pixel thick blue line from the top-right corner to the
# bottom-left
blue = (255, 0, 0)
cv2.line(canvas, (200, 50), (0, 300), blue, 2)
cv2.imshow("Canvas", canvas)
cv2.rectangle(canvas, (50, 50), (120, 120), green)
cv2.imshow("Canvas", canvas)
# draw another rectangle, this one blue with 4 pixel thickness
cv2.rectangle(canvas, (80, 150), (150, 250), blue, 4)
cv2.imshow("Canvas", canvas)
# draw a final rectangle (red and filled in )
red = (0, 0, 255)
cv2.rectangle(canvas, (200, 50), (225, 125), red, -1)
cv2.imshow("Canvas", canvas)
cv2.waitKey(0)
cv2.destroyAllWindows()
OUTPUT :
CONCENTIC CIRCLES :
import cv2
import numpy as np
img=np.ones((300,300,3),dtype="uint8")
img=255*img
cv2.circle(img, (150, 150), 10, (0, 0, 255), 2)
cv2.circle(img, (150, 150), 20, (255, 0, 0), 2)
cv2.circle(img, (150, 150), 30, (0, 255, 0), 2)
cv2.circle(img, (150, 150), 40, (0, 0, 255), 2)
cv2.circle(img, (150, 150), 50, (255, 0, 0), 2)
cv2.circle(img, (150, 150), 60, (0, 255, 0), 2)
cv2.circle(img, (150, 150), 70, (0, 0, 255), 2)
cv2.circle(img, (150, 150), 80, (255, 0, 0), 2)
cv2.circle(img, (150, 150), 90, (0, 255, 0), 2)
cv2.circle(img, (150, 150), 100, (0, 0, 255), 2)
image = cv2.putText(img,'Concentric', (100, 35), cv2.FONT_HERSHEY_PLAIN,
1, (255,0,255), 2)
# show the output image
cv2.imshow("Output", img)
cv2.waitKey(0)

OUTPUT :
LIVE STREAM (USING WEBCAM):

import cv2
cam = cv2.VideoCapture(0)
frame_width = int(cam.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cam.get(cv2.CAP_PROP_FRAME_HEIGHT))
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter('output.mp4', fourcc, 20.0, (frame_width, frame_height))
while True:
ret, frame = cam.read()
cv2.circle(frame, (150, 150), 100, (0, 0, 255), 1)
cv2.circle(frame, (115, 125), 10, (0, 0, 255), 1)
cv2.circle(frame, (190, 125), 10, (0, 0, 255), 1)
cv2.rectangle(frame, (25, 25), (275, 275), (0, 0, 255), 1)
img = cv2.putText(frame,"Manu",(150,300),cv2.FONT_HERSHEY_COMPLEX,
1,(0,255,0),1)
out.write(frame)
cv2.imshow('Camera', frame)
if cv2.waitKey(1) == ord('q'):
break
cam.release()
out.release()
cv2.destroyAllWindows()
OUTPUT :
3. MASK OR CROP :
import cv2
import numpy as np
low_green = np.array([25, 52, 72])
high_green = np.array([102, 255, 255])
cap=cv2.VideoCapture(0)
while True:
ret,frame=cap.read()
cv2.imshow('original Frame',frame)
hsv=cv2.cvtColor(frame,cv2.COLOR_HSV2BGR)
mask=cv2.inRange(hsv,low_green,high_green)
cv2.imshow('Masked Frame',mask)
if cv2.waitKey(1)==ord('q'):
break
cap.release()
cv2.destroyAllWindows()
OUTPUT (WITH WEBCAM):

(FROM ALBUMS):
4. HISTOGRAM OF AN IMAGE :
import cv2
from matplotlib import pyplot as plt
img = cv2.imread('bike.jpg',0)
histr = cv2.calcHist([img],[0],None,[256],[0,256])
plt.plot(histr)
plt.show()
OUTPUT :
Original image :

Original histogram of the image :


5. HISTOGRAM EQUALIZATION :
import cv2
# importing library for plotting
from matplotlib import pyplot as plt
# reads an input image
img = cv2.imread('image51.jpg',0)
resized_image = cv2.resize(img,(300,300))
cv2.imshow('resized_image', resized_image)
# find frequency of pixels in range 0-255
histr = cv2.calcHist([resized_image],[0],None,[256],[0,256])
cv2.imshow("hisimage",resized_image)
equalized = cv2.equalizeHist(resized_image)
cv2.imshow("equalized_image",equalized)
# show the plotting graph of an image
plt.subplot(1,2,1)
plt.plot(histr)
plt.subplot(1,2,2)
plt.plot(equalized)
plt.show()
HIS IMAGE: EQUALIZED IMAGE :
6. THRESHOLD :
1. SIMPLE THRESHOLD :
import cv2
import numpy as np
import matplotlib.pyplot as plt
image1 = cv2.imread('image51.jpg')
resized_image = cv2.resize(image1,(300,300))
cv2.imshow('resized_image', resized_image)
resized_image = cv2.cvtColor(resized_image, cv2.COLOR_BGR2GRAY)
ret, thresh1 = cv2.threshold(resized_image, 120, 255, cv2.THRESH_BINARY)
ret, thresh2 = cv2.threshold(resized_image, 120, 255, cv2.THRESH_BINARY_INV)
ret, thresh3 = cv2.threshold(resized_image, 120, 255, cv2.THRESH_TRUNC)
ret, thresh4 = cv2.threshold(resized_image, 120, 255, cv2.THRESH_TOZERO)
ret, thresh5 = cv2.threshold(resized_image, 120, 255, cv2.THRESH_TOZERO_INV)

imgs = [thresh1, thresh2, thresh3, thresh4, thresh5]


title = ['THRESH_BINARY', 'THRESH_BINARY_INV', 'THRESH_TRUNC', 'THRESH_TOZERO',
'THRESH_TOZERO_INV']
plt.subplot(2, 3, 1)
plt.title(title[0])
plt.imshow(imgs[0], cmap='gray')
plt.xticks([])
plt.yticks([])

plt.subplot(2, 3, 2)
plt.title(title[1])
plt.imshow(imgs[1], cmap='gray')
plt.xticks([])
plt.yticks([])

plt.subplot(2, 3, 3)
plt.title(title[2])
plt.imshow(imgs[2], cmap='gray')
plt.xticks([])
plt.yticks([])

plt.subplot(2, 3, 4)
plt.title(title[3])
plt.imshow(imgs[3], cmap='gray')
plt.xticks([])
plt.yticks([])

plt.subplot(2, 3, 5)
plt.title(title[4])
plt.imshow(imgs[4], cmap='gray')
plt.xticks([])
plt.yticks([])

if cv2.waitKey(0) & 0xff == 27:


cv2.destroyAllWindows()
plt.show()
OUTPUT :
INPUT RESIZED IMAGE :

OUTPUT :
2. ADAPTIVE THRESHOLD :
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
img = cv.imread('pexels-pixabay-66898.jpg', cv.IMREAD_GRAYSCALE)
#img = cv.medianBlur(img, 5)
ret, th1 = cv.threshold(img, 127, 255, cv.THRESH_BINARY)
th2 = cv.adaptiveThreshold(img, 255, cv.ADAPTIVE_THRESH_MEAN_C,cv.THRESH_BINARY,
11, 2)
th3 = cv.adaptiveThreshold(img, 255,
cv.ADAPTIVE_THRESH_GAUSSIAN_C,cv.THRESH_BINARY, 11, 2)
titles = ['Original Image', 'Global Thresholding (v = 127)',
'Adaptive Mean Thresholding', 'Adaptive Gaussian Thresholding']
images = [img, th1, th2, th3]
for i in range(4):
plt.subplot(2, 2, i + 1), plt.imshow(images[i], 'gray')
plt.title(titles[i])
plt.xticks([]), plt.yticks([])
plt.show()
OUTPUT :
INPUT IMAGE :
3. OTSU THRESHOLD :
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
img = cv.imread('pexels-futurekiiid-3551498.jpg', cv.IMREAD_GRAYSCALE)
# global thresholding
ret1, th1 = cv.threshold(img, 127, 255, cv.THRESH_BINARY)
# Otsu's thresholding
ret2, th2 = cv.threshold(img, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)
# Otsu's thresholding after Gaussian filtering
blur = cv.GaussianBlur(img, (5, 5), 0)
ret3, th3 = cv.threshold(blur, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)
# plot all the images and their histograms
images = [img, 0, th1,
img, 0, th2,
blur, 0, th3]
titles = ['Original Noisy Image', 'Histogram', 'Global Thresholding (v=127)',
'Original Noisy Image', 'Histogram', "Otsu's Thresholding",
'Gaussian filtered Image', 'Histogram', "Otsu's Thresholding"]
for i in range(3):
plt.subplot(3, 3, i * 3 + 1), plt.imshow(images[i * 3], 'gray')
plt.title(titles[i * 3]), plt.xticks([]), plt.yticks([])
plt.subplot(3, 3, i * 3 + 2), plt.hist(images[i * 3].ravel(), 256)
plt.title(titles[i * 3 + 1]), plt.xticks([]), plt.yticks([])
plt.subplot(3, 3, i * 3 + 3), plt.imshow(images[i * 3 + 2], 'gray')
plt.title(titles[i * 3 + 2]), plt.xticks([]), plt.yticks([])
plt.show()
OUTPUT :
Result:
Thus the basic image processing concepts were learnt using OpenCV in Visual studio code.
Post Lab Questions:
1. What are the applications of masking and cropping in image processing?
Masking and cropping are fundamental techniques in image processing, each serving
distinct but complementary purposes. Masking involves isolating a specific region of an
image by applying a binary mask, allowing for targeted operations like object detection,
image segmentation, background removal, and feature extraction. It is commonly used
in applications such as medical imaging (e.g., isolating tissues in CT scans), satellite
image analysis, and facial recognition, where only certain areas of the image need to be
processed. Cropping, on the other hand, involves trimming an image to a smaller region,
focusing on areas of interest while removing unnecessary parts. It is widely applied in
tasks like object localization for machine learning, data augmentation, and composition
adjustment in photography. Both techniques are often used together, where a mask first
highlights a region of interest and cropping refines the focus by cutting out irrelevant
areas, making them crucial in areas like remote sensing, document processing (e.g.,
OCR), and image registration for medical or scientific analysis.

2. Illustrate the Masking Concept ( in live stream—Masking your body)

Masking and cropping are essential techniques in image processing, each with specific
applications across various domains. Masking involves isolating a particular region of an
image using a binary mask, allowing for targeted processing without affecting the entire
image. This is especially useful in object detection, segmentation, and background
removal. For example, in medical imaging, a mask can help highlight and isolate a tumor
in an MRI scan, while in photography, masking allows the subject to be separated from
the background for further manipulation, such as background replacement. Additionally,
masking is used in satellite imagery, where only specific regions of interest are enhanced
or analyzed, and in feature extraction tasks like facial recognition. Cropping, on the other
hand, involves trimming an image to focus on a smaller region, which is useful for object
localization, data augmentation, and improving composition in photography. In deep
learning, cropping helps by providing different perspectives of the same image to train
models, while in OCR, it isolates text areas from scanned documents for efficient
recognition. Both techniques can be combined, as in the case of image registration, where
a mask isolates the region of interest, and cropping refines the image for comparison.
The concept of masking can be illustrated in a live stream scenario where someone uses
a virtual background effect. In this case, the mask can isolate the person’s body from the
background, allowing them to appear in front of a new, digitally inserted background.
This real-time masking isolates their body using a mask that dynamically adjusts to their
movements, ensuring the background remains unchanged while the subject stays
highlighted. This technique is common in video conferencing, gaming, or virtual live
streaming, where users mask out their surroundings and replace them with a virtual
backdrop.

3. Write a program to apply a circular mask on an image

import cv2
import numpy as np
image = cv2.imread("input_image.jpg")
height, width = image.shape[:2]
mask = np.zeros((height, width), dtype=np.uint8)
center = (width // 2, height // 2)
radius = min(height, width) // 4
cv2.circle(mask, center, radius, 255, -1)
masked_image = cv2.bitwise_and(image, image, mask=mask)
cv2.imshow("Original Image", image)
cv2.imshow("Circular Mask", masked_image)
cv2.imwrite("circular_masked_image.jpg", masked_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

4. What are the different functions available in OpenCV and Numpy packages to
calculate the histogram and histogram equalization of an image?
OpenCV Functions:
1. cv2.calcHist(): Computes image histograms.
2. cv2.equalizeHist(): Performs histogram equalization to enhance contrast.
NumPy Functions:
1. numpy.histogram(): Computes histograms from array data.
2. numpy.histogram2d(): Computes 2D histograms for multi-channel data.

5. Write a program to find the coordinates of an image


import cv2
def click_event(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
print(f"Coordinates: ({x}, {y})")
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img, f"({x},{y})", (x, y), font, 0.5, (255, 0, 0), 1)
cv2.imshow("Image", img)
img = cv2.imread("input_image.jpg")
cv2.imshow("Image", img)

cv2.setMouseCallback("Image", click_event)
cv2.waitKey(0)
cv2.destroyAllWindows()

6. What impact does masking a picture in a histogram have?


Masking in a histogram focuses analysis on specific regions, excluding irrelevant areas. It
enables selective contrast adjustment, reduces noise, improves analysis accuracy, and saves
computational effort.

7. Illustrate arithmetic operation


(i) Addition of two images
(ii) Subtraction of pixels of two images
(iii) Bitwise operators on images

1.
import cv2
img1 = cv2.imread('image1.jpg')
img2 = cv2.imread('image2.jpg')
added_image = cv2.add(img1, img2)
cv2.imshow("Added Image", added_image)
cv2.imwrite("added_image.jpg", added_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

2.
subtracted_image = cv2.subtract(img1, img2)
cv2.imshow("Subtracted Image", subtracted_image)
cv2.imwrite("subtracted_image.jpg", subtracted_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

3.
bitwise_and = cv2.bitwise_and(img1, img2)
bitwise_or = cv2.bitwise_or(img1, img2)
bitwise_xor = cv2.bitwise_xor(img1, img2)
bitwise_not = cv2.bitwise_not(img1)
cv2.imshow("Bitwise AND", bitwise_and)
cv2.imshow("Bitwise OR", bitwise_or)
cv2.imshow("Bitwise XOR", bitwise_xor)
cv2.imshow("Bitwise NOT", bitwise_not)
cv2.waitKey(0)
cv2.destroyAllWindows()

You might also like