[go: up one dir, main page]

0% found this document useful (0 votes)
2 views30 pages

Cvrlabmanual

The document contains a series of Python programs that demonstrate various image processing techniques using OpenCV and other libraries. Techniques include loading images, image transformations (translation, flipping, rotation, scaling, shearing), filtering (Gaussian, median, mean), edge detection, motion detection, texture extraction, object detection, and face recognition. Additionally, it covers dimensionality reduction using PCA and model-based reconstruction using TensorFlow.

Uploaded by

bennyofficial801
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)
2 views30 pages

Cvrlabmanual

The document contains a series of Python programs that demonstrate various image processing techniques using OpenCV and other libraries. Techniques include loading images, image transformations (translation, flipping, rotation, scaling, shearing), filtering (Gaussian, median, mean), edge detection, motion detection, texture extraction, object detection, and face recognition. Additionally, it covers dimensionality reduction using PCA and model-based reconstruction using TensorFlow.

Uploaded by

bennyofficial801
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/ 30

1.

Write a program to load an image

import cv2

img = cv2.imread("image.jpg", cv2.IMREAD_COLOR)

cv2.imshow("image", img)

cv2.waitKey(0)

cv2.destroyAllWindows()

output:
2.Write a program for image transalation

import numpy as np

import cv2 as cv

img = cv.imread('a1.jpg', 0)

rows, cols = img.shape

M = np.float32([[1, 0, 100], [0, 1, 50]])

dst = cv.warpAffine(img, M, (cols, rows))

cv.imshow('img', dst)

cv.waitKey(0)

cv.destroyAllWindows()
3.write a program to flip an image

import numpy as np

import cv2 as cv

img = cv.imread('image.jpg', 0)

rows, cols = img.shape

M = np.float32([[1, 0, 0],

[0, -1, rows],

[0, 0, 1]])

reflected_img = cv.warpPerspective(img, M,

(int(cols),

int(rows)))

cv.imshow('img', reflected_img)

cv.imwrite('reflection_out.jpg', reflected_img)

cv.waitKey(0)

cv.destroyAllWindows()
4.write a program to rotate an image

import numpy as np

import cv2 as cv

img = cv.imread('image.jpg', 0)

rows, cols = img.shape

M = np.float32([[1, 0, 0], [0, -1, rows], [0, 0, 1]])

img_rotation = cv.warpAffine(img,

cv.getRotationMatrix2D((cols/2, rows/2),

30, 0.6),

(cols, rows))

cv.imshow('img', img_rotation)

cv.imwrite('rotation_out.jpg', img_rotation)

cv.waitKey(0)

cv.destroyAllWindows()
5.write a program to scale an image

import numpy as np

import cv2 as cv

img = cv.imread('image.jpg', 0)

rows, cols = img.shape

img_shrinked = cv.resize(img, (250, 200),

interpolation=cv.INTER_AREA)

cv.imshow('img', img_shrinked)

img_enlarged = cv.resize(img_shrinked, None,

fx=1.5, fy=1.5,

interpolation=cv.INTER_CUBIC)

cv.imshow('img', img_enlarged)

cv.waitKey(0)

cv.destroyAllWindows()
6.write program to Image Shearing in X-Axis

import numpy as np
import cv2 as cv
img = cv.imread('a1.jpg', 0)
rows, cols = img.shape
M = np.float32([[1, 0.5, 0], [0, 1, 0], [0, 0, 1]])
sheared_img = cv.warpPerspective(img, M, (int(cols*1.5), int(rows*1.5)))
cv.imshow('img', sheared_img)
cv.waitKey(0)
cv.destroyAllWindows()
7.write a program to extract images from video

# Importing all necessary libraries

import cv2

import os

# Read the video from specified path

cam = cv2.VideoCapture("D:\\video.mp4")

try:

# creating a folder named data

if not os.path.exists('data'):

os.makedirs('data')

# if not created then raise error

except OSError:

print ('Error: Creating directory of data')

# frame

currentframe = 0

while(True):

# reading from frame

ret,frame = cam.read()

if ret:

# if video is still left continue creating images


name = './data/frame' + str(currentframe) + '.jpg'

print ('Creating...' + name)

# writing the extracted images

cv2.imwrite(name, frame)

# increasing counter so that it will

# show how many frames are created

currentframe += 1

else:

break

# Release all space and windows once done

cam.release()

cv2.destroyAllWindows()
8.write a program in python to apply 2d convolution smoothy

import cv2

import numpy as np

# Reading the image

image = cv2.imread('a1.jpg')

Creating the kernel with numpy

kernel2 = np.ones((5, 5), np.float32)/25

# Applying the filter

img = cv2.filter2D(src=image, ddepth=-1, kernel=kernel2)

# showing the image

cv2.imshow('Original', image)

cv2.imshow('Kernel Blur', img)

v2.waitKey()

cv2.destroyAllWindows()
9.write a program to apply guassian filter

import cv2
import numpy as np

# Reading the image


image = cv2.imread('image.jpg')

# Applying the filter


gaussian = cv2.GaussianBlur(image, (3, 3), 0)

# Showing the image


cv2.imshow('Original', image)
cv2.imshow('Gaussian blur', gaussian)

cv2.waitKey()
cv2.destroyAllWindows()
10.write a program to apply median filter

# Importing Image and ImageFilter module from PIL package

from PIL import Image, ImageFilter

# creating a image object

im1 = Image.open("image.jpg")

# applying the median filter

im2 = im1.filter(ImageFilter.MedianFilter(size = 3))

im2.show()
11.write a program to apply mean filter

import numpy as np

import cv2

from matplotlib import pyplot as plt

from PIL import Image, ImageFilter

image = cv2.imread('AM04NES.JPG') # reads the image

image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) # convert to HSV

figure_size = 9 # the dimension of the x and y axis of the kernal.

new_image = cv2.blur(image,(figure_size, figure_size))

plt.figure(figsize=(11,6))

plt.subplot(121), plt.imshow(cv2.cvtColor(image, cv2.COLOR_HSV2RGB)),plt.title('Original')

plt.xticks([]), plt.yticks([])

plt.subplot(122), plt.imshow(cv2.cvtColor(new_image, cv2.COLOR_HSV2RGB)),plt.title('Mean filter')

plt.xticks([]), plt.yticks([])

plt.show()
12.write a program to apply fourier transform

import numpy as np

import cv2

from matplotlib import pyplot as plt

# read the input image

# you can specify the path to image

image_path = r"Dhoni-dive_165121_730x419-m.jpg"

image = cv2.imread(image_path, 0)

# calculating the discrete Fourier transform

DFT = cv2.dft(np.float32(image), flags=cv2.DFT_COMPLEX_OUTPUT)

# reposition the zero-frequency component to the spectrum's middle

shift = np.fft.fftshift(DFT)

row, col = image.shape

center_row, center_col = row // 2, col // 2

# create a mask with a centered square of 1s

mask = np.zeros((row, col, 2), np.uint8)

mask[center_row - 30:center_row + 30, center_col - 30:center_col + 30] = 1

# put the mask and inverse DFT in place.

fft_shift = shift * mask

fft_ifft_shift = np.fft.ifftshift(fft_shift)

imageThen = cv2.idft(fft_ifft_shift)

# calculate the magnitude of the inverse DFT

imageThen = cv2.magnitude(imageThen[:,:,0], imageThen[:,:,1])

# visualize the original image and the magnitude spectrum

plt.figure(figsize=(10,10))
plt.subplot(121), plt.imshow(image, cmap='gray')

plt.title('Input Image'), plt.xticks([]), plt.yticks([])

plt.subplot(122), plt.imshow(imageThen, cmap='gray')

plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])

plt.show()
13. write a program Histograms Equalization in OpenCV

# import Opencv

import cv2

# import Numpy

import numpy as np

# read a image using imread

img = cv2.imread('image.jpg', 0)

# creating a Histograms Equalization

# of a image using cv2.equalizeHist()

equ = cv2.equalizeHist(img)

# stacking images side-by-side

res = np.hstack((img, equ))

# show image input vs output

cv2.imshow('image', res)

cv2.waitKey(0)

cv2.destroyAllWindows()
14.write a program to implement morphological operations like dilation, erosion, opening and
closing given image

import cv2

import numpy as np

import matplotlib.pyplot as plt

import cv2

import numpy as np

import matplotlib.pyplot as plt

#Read the image for erosion

img1= cv2.imread("image.jpg",0)

#Acquire size of the image

m,n= img1.shape

#Show the image

plt.imshow(img1, cmap="gray")

# Define the structuring element

# k= 11,15,45 -Different sizes of the structuring element

k=15

SE= np.ones((k,k), dtype=np.uint8)

constant= (k-1)//2

#Define new image

imgErode= np.zeros((m,n), dtype=np.uint8)

#Erosion without using inbuilt cv2 function for morphology

for i in range(constant, m-constant):

for j in range(constant,n-constant):

temp= img1[i-constant:i+constant+1, j-constant:j+constant+1]

product= temp*SE
imgErode[i,j]= np.min(product)

plt.imshow(imgErode,cmap="gray")

cv2.imwrite("Eroded3.png", imgErode)
15.write a program to display edge detection of image

import cv2

# Read the original image

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

# Display original image

cv2.imshow('Original', img)

cv2.waitKey(0)

# Convert to graycsale

img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Blur the image for better edge detection

img_blur = cv2.GaussianBlur(img_gray, (3,3), 0)

# Sobel Edge Detection

sobelx = cv2.Sobel(src=img_blur, ddepth=cv2.CV_64F, dx=1, dy=0, ksize=5) # Sobel Edge Detection on


the X axis

sobely = cv2.Sobel(src=img_blur, ddepth=cv2.CV_64F, dx=0, dy=1, ksize=5) # Sobel Edge Detection on


the Y axis

sobelxy = cv2.Sobel(src=img_blur, ddepth=cv2.CV_64F, dx=1, dy=1, ksize=5) # Combined X and Y Sobel


Edge Detection

# Display Sobel Edge Detection Images

cv2.imshow('Sobel X', sobelx)

cv2.waitKey(0)

cv2.imshow('Sobel Y', sobely)

cv2.waitKey(0)

cv2.imshow('Sobel X Y using Sobel() function', sobelxy)

cv2.waitKey(0)

# Canny Edge Detection

edges = cv2.Canny(image=img_blur, threshold1=100, threshold2=200) # Canny Edge Detection


# Display Canny Edge Detection Image

cv2.imshow('Canny Edge Detection', edges)

cv2.waitKey(0)

cv2.destroyAllWindows()
16.write a program to detect motion

import cv2

cap = cv2.VideoCapture(0)

mog = cv2.createBackgroundSubtractorMOG2()

while True:

ret, frame = cap.read()

gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

fgmask = mog.apply(gray)

kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))

fgmask = cv2.erode(fgmask, kernel, iterations=1)

fgmask = cv2.dilate(fgmask, kernel, iterations=1)

contours, hierarchy = cv2.findContours(fgmask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

for contour in contours:

# Ignore small contours

if cv2.contourArea(contour) < 1000:

continue

# Draw bounding box around contour

x, y, w, h = cv2.boundingRect(contour)

cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)

cv2.imshow('Motion Detection', frame)

if cv2.waitKey(1) == ord('q'):

break

cap.release()

cv2.destroyAllWindows()
17. Implement texture extraction of a given image

import cv2

import numpy as np

# Load the image

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

# Convert to grayscale

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Detect edges using Canny method

edges = cv2.Canny(gray, 150, 300)

# Display the image with corners

img[edges == 255] = (255,0,0)

cv2.imshow('Canny Edges', img)

cv2.waitKey(0)

cv2.destroyAllWindows()
18. Implement object detection like recognizing pedestrians

import cv2

import imutils

# Initializing the HOG person

# detector

hog = cv2.HOGDescriptor()

hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())

# Reading the Image

image = cv2.imread('a2.jpg')

# Resizing the Image

image = imutils.resize(image,

width=min(400, image.shape[1]))

# Detecting all the regions in the

# Image that has a pedestrians inside it

(regions, _) = hog.detectMultiScale(image,

winStride=(4, 4),

padding=(4, 4),

scale=1.05)

# Drawing the regions in the Image

for (x, y, w, h) in regions:

cv2.rectangle(image, (x, y),

(x + w, y + h),

(0, 0, 255), 2)
# Showing the output Image

cv2.imshow("Image", image)

cv2.waitKey(0)

cv2.destroyAllWindows()
19. Implement face recognition of an image using K-Means clustering.

Kmean = KMeans(n_clusters=5)
Kmean.fit(data)

centers=np.array(Kmean.cluster_centers_)

labels=Kmean.labels_

labels = Kmean.predict(new_data)
20.Implement dimensionality reduction using PCA for the given images

# Import necessary libraries

from sklearn import datasets # to retrieve the iris Dataset

import pandas as pd # to load the dataframe

from sklearn.preprocessing import StandardScaler # to standardize the features

from sklearn.decomposition import PCA # to apply PCA

import seaborn as sns # to plot the heat maps

#Load the Dataset

iris = datasets.load_iris()

#convert the dataset into a pandas data frame

df = pd.DataFrame(iris['data'], columns = iris['feature_names'])

#display the head (first 5 rows) of the dataset

df.head()

#Standardize the features

#Create an object of StandardScaler which is present in sklearn.preprocessing

scalar = StandardScaler()

scaled_data = pd.DataFrame(scalar.fit_transform(df)) #scaling the data

scaled_data

#Check the Co-relation between features without PCA

sns.heatmap(scaled_data.corr())

#Applying PCA
#Taking no. of Principal Components as 3

pca = PCA(n_components = 3)

pca.fit(scaled_data)

data_pca = pca.transform(scaled_data)

data_pca = pd.DataFrame(data_pca,columns=['PC1','PC2','PC3'])

data_pca.head()

#Checking Co-relation between features after PCA

sns.heatmap(data_pca.corr())
21. Demonstrate model based reconstruction using tensor flow

import tensorflow as tf

from tensorflow.keras.datasets import imdb

from tensorflow.keras.layers import Embedding, Dense, LSTM

from tensorflow.keras.losses import BinaryCrossentropy

from tensorflow.keras.models import Sequential

from tensorflow.keras.optimizers import Adam

from tensorflow.keras.preprocessing.sequence import pad_sequences

# Model configuration

additional_metrics = ['accuracy']

batch_size = 128

embedding_output_dims = 15

loss_function = BinaryCrossentropy()

max_sequence_length = 300

num_distinct_words = 5000

number_of_epochs = 5

optimizer = Adam()

validation_split = 0.20

verbosity_mode = 1

# Disable eager execution

tf.compat.v1.disable_eager_execution()
# Load dataset

(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=num_distinct_words)

print(x_train.shape)

print(x_test.shape)

# Pad all sequences

padded_inputs = pad_sequences(x_train, maxlen=max_sequence_length, value = 0.0) # 0.0 because it


corresponds with <PAD>

padded_inputs_test = pad_sequences(x_test, maxlen=max_sequence_length, value = 0.0) # 0.0 because


it corresponds with <PAD>

# Define the Keras model

model = Sequential()

model.add(Embedding(num_distinct_words, embedding_output_dims,
input_length=max_sequence_length))

model.add(LSTM(10))

model.add(Dense(1, activation='sigmoid'))

# Compile the model

model.compile(optimizer=optimizer, loss=loss_function, metrics=additional_metrics)

# Give a summary

model.summary()

# Train the model

history = model.fit(padded_inputs, y_train, batch_size=batch_size, epochs=number_of_epochs,


verbose=verbosity_mode, validation_split=validation_split)
# Test the model after training

test_results = model.evaluate(padded_inputs_test, y_test, verbose=False)

print(f'Test results - Loss: {test_results[0]} - Accuracy: {100*test_results[1]}%')

You might also like