ASIP Practical
ASIP Practical
# Down sampling
# Up sampling
# Replicating columns
for i in range(0, m-1):
for j in range(1, n-1, f):
img3[i, j:j+(f-1)] = img3[i, j-1]
Input :
Output:
Original Image:
Up Sampled Image:
Practical No- 2
Output :
2.For a given signal, plot the segment and compute the correlation between them.
importnumpy as np
importmatplotlib.pyplot as plt
# signal 1
time1=np.arange(0,100,0.1)
cossignal1=np.cos(time1)
plt.plot(cossignal1)
plt.title("Signal 1")
plt.show()
# signal 2
time2=np.arange(0,100,0.1)
cossignal2=np.cos(time2)
plt.plot(cossignal2)
plt.title("Signal 2")
plt.show()
Output:
Practical No 3
Aim: Write program to demonstrate the following aspects of signal on image data
1. Convolution operation
import cv2
import numpy as np
image = cv2.imread('Flower.jpg')
cv2.imshow('Orginal',image)
cv2.imshow('Sharpened',sharp_img)
cv2.imshow('Kernel blur',blur_img)
cv2.imwrite('sharp_image.jpg',sharp_img)
cv2.imwrite('blur_kernel.jpg',blur_img)
cv2.waitKey()
cv2.destroyAllWindows()
Input
Orginal
Output
Sharpened
Kernel blur
2. Template Matching
Input
Sprite.jpg
Template.jpg
Output
Detected Image
Practical No - 4
Aim :- Write program to implement point/pixel intensity transformations such as
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Read an image
image = cv2.imread('g4g.png')
# Apply log transformation method
c = 255 / np.log(1 + np.max(image))
log_image = c * (np.log(image + 1))
# Specify the data type so that
# float value will be converted to int
log_image = np.array(log_image, dtype = np.uint8)
# Display both images
plt.imshow(image)
plt.show()
plt.imshow(log_image)
plt.show()
Input Image:
# import Opencv
import cv2
# import Numpy
import numpy as np
import matplotlib.pyplot as plt #importing matplotlib
# read a image using imread
img = cv2.imread('download.jpg', 0)
histr = cv2.calcHist([img],[0],None,[256],[0,256])
# show the plotting graph of an image
plt.plot(histr)
plt.show()
# 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)
histr = cv2.calcHist([res],[0],None,[256],[0,256])
# show the plotting graph of an image
plt.plot(histr)
plt.show()
cv2.waitKey(0)
cv2.destroyAllWindows()
Input Image:
Output Image of 'Binary Threshold': Output Image of 'Set to 0', thresh4’:
Code :
import cv2
img = cv2.imread("E:\PYTHON\earth.jpg")
ddepth = cv2.CV_16S
kernel_size =3
red_Noise = cv2.GaussianBlur(img,(3,3),0)
gray=cv2.cvtColor(red_Noise,cv2.COLOR_BGR2GRAY)
dst=cv2.Laplacian(gray,ddepth,ksize=kernel_size)
abs_dst=cv2.convertScaleAbs(dst)
cv2.imshow('original image',img)
cv2.imshow('Laplacian image',abs_dst)
Code:
import numpy as np
import cv2
gauss = np.random.normal(0,1,image.size)
gauss = gauss.reshape(image.shape[0],image.shape[1],image.shape[2]).astype('uint8')
img_gauss = cv2.add(image,gauss)
#noiseImg = red_Noise=cv2.GaussianBlur(image,(3,3),0)
new_image=cv2.GaussianBlur(image,(figure_size,figure_size),0)
plt.figure(figsize=(11,6))
plt.subplot(121)
plt.imshow(cv2.cvtColor(img_gauss,cv2.COLOR_HSV2RGB))
plt.title('Gaussian Noise')
plt.xticks([]),plt.yticks([])
plt.subplot(122),plt.imshow(cv2.cvtColor(new_image,cv2.COLOR_HSV2RGB)),plt.title('Gaussian Filter')
plt.xticks([]),plt.yticks([])
plt.show()
Output:
Practical No: 7
Aim: Write a program to apply various image enhancement using image
derivatives by implementing smoothing filters for generating suitable images for
specific application requirements.
Code :
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('eye.jpg')
kernel = np.ones((5,5),np.float32)/25
dst=cv2.filter2D(img,-1,kernel)
plt.subplot(121),plt.imshow(img),plt.title('original')
plt.xticks([]),plt.yticks([])
plt.subplot(122),plt.imshow(dst),plt.title('Smoothened Image')
plt.xticks([]),plt.yticks([])
plt.show()
OUTPUT
Practical No: 8
Aim - Write a program to Apply edge detection techniques such as Sobel and
Canny to extract meaningful information from the given image samples
Code –
import cv2
img=cv2.imread('D:/flower_image.jpg')
cv2.imshow('Original',img)
cv2.waitKey(0)
img_gry=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
img_blur=cv2.GaussianBlur(img_gry,(3,3),0)
sobelx=cv2.Sobel(src=img_blur,ddepth=cv2.CV_64F,dx=1,dy=0,ksize=5)
sobely=cv2.Sobel(src=img_blur,ddepth=cv2.CV_64F,dx=0,dy=1,ksize=5)
sobelxy=cv2.Sobel(src=img_blur,ddepth=cv2.CV_64F,dx=1,dy=1,ksize=5)
cv2.imshow('SobelX',sobelx)
cv2.waitKey(0)
cv2.imshow('SobelY',sobely)
cv2.waitKey(0)
cv2.imshow('SobelXY',sobelxy)
cv2.waitKey(0)
edges=cv2.Canny(image=img_blur,threshold1=100,threshold2=200)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output : Original image
Sobely sobelx
Code -
import cv2
import numpy as np
image=cv2.imread('x.jpg')
kernel=np.ones((5,5),np.uint8)
erosion=cv2.erode(image,kernel,iterations=1)
dilation=cv2.dilate(image,kernel,iterations=1)
cv2.imshow('input',image)
cv2.imshow('erosion',erosion)
cv2.imshow('dilation',dilation)
output –
original image -
Erosion -
Dilation –
Opening and Closing
Code-
import cv2
import numpy as np
image=cv2.imread('opening.png')
kernel=np.ones((5,5),np.uint8)
opening=cv2.morphologyEx(image,cv2.MORPH_OPEN,kernel)
closing=cv2.morphologyEx(image,cv2.MORPH_CLOSE,kernel)
gradient=cv2.morphologyEx(image,cv2.MORPH_GRADIENT,kernel)
cv2.imshow('input',image)
cv2.imshow('opening',opening)
cv2.imshow('closeing',closing)
output -
original image –
Opening –
Closing –
Practical no: 10
1) Blob Detector
import cv2
import numpy as np
img = cv2.imread('C:/messi/ronaldo.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
gray = np.float32(gray)
dst = cv2.cornerHarris(gray,2,3,0.04)
dst = cv2.dilate(dst,None)
img[dst>0.01*dst.max()]=[0,0,255]
cv2.imshow('output',img)
cv2.imwrite('corner10.1_output.jpg',img)
Output:
2) Hog
import cv2
ori = cv2.imread('D:/signallingpractical/house-sparrow-1.webp')
img = cv2.imread('D:/signallingpractical/house-sparrow-1.webp')
_,hog_image =
hog(img,orientations=8,pixels_per_cell=(7,7),cells_per_block=(1,1),visualize=True,multichanne
l = True)
cv2.imshow('Original Image',ori)
cv2.imshow('Hog',hog_image)
cv2.imwrite('Original Image',ori)
cv2.imwrite('Hog',hog_image)
Output:
3) Harr
import cv2
import numpy as np
face_cascade=cv2.CascadeClassifier('C:/Python310/Lib/site-
packages/cv2/data/haarcascade_frontalface_default.xml')
eye_cascade=cv2.CascadeClassifier('C:/Python310/Lib/site-
packages/cv2/data/haarcascade_eye.xml')
img = cv2.imread('C:/messi/ronaldo.jpg')
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces=face_cascade.detectMultiScale(gray,1.3,5)
for(x,y,w,h)in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray=gray[y:y+h,x:x+w]
roi_color=img[y:y+h,x:x+w]
eyes=eye_cascade.detectMultiScale(roi_gray)
for(ex,ey,ew,eh) in eyes:
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
cv2.imshow('img',img)
cv2.imwrite('detected.jpg',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output:
Practical No: 11
Aim: Write the program to apply segmentation for detecting lines, circles, and
other shapes/objects. Also, implement edge-based and region-based
segmentation.
Code:
import numpy as np
import cv2 as cv
img = cv.imread('opencv-logo-white.png',0)
img = cv.medianBlur(img,5)
cimg = cv.cvtColor(img,cv.COLOR_GRAY2BGR)
circles = cv.HoughCircles(img,cv.HOUGH_GRADIENT,1,20,
param1=50,param2=30,minRadius=0,maxRadius=0)
circles = np.uint16(np.around(circles))
for i in circles[0,:]:
# draw the outer circle
cv.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
# draw the center of the circle
cv.circle(cimg,(i[0],i[1]),2,(0,0,255),3)
cv.imshow('detected circles',cimg)
cv.waitKey(0)
cv.destroyAllWindows()
Output: