Docsity

Image Processing Techniques with Python and OpenCV, Cheat Sheet of Computer Vision

Toggle sidebar
bg1
EXPT 4 :
FOURIER TRANSFORM:-
PROGRAM :-
Import numpy as np
# now we will be loading the image and converting it to grayscale
Image = cv2.imread(“flower.jpg”)
Gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Compute the discrete Fourier Transform of the image
Fourier = cv2.dft(np.float32(gray), flags=cv2.DFT_COMPLEX_OUTPUT)
# Shift the zero-frequency component to the center of the spectrum
Fourier_shift = np.fft.fftshift(fourier)
# calculate the magnitude of the Fourier Transform
Magnitude = 20*np.log(cv2.magnitude(fourier_shift[:,:,0],fourier_shift[:,:,1]))
# Scale the magnitude for display
Magnitude = cv2.normalize(magnitude, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8UC1)
# Display the magnitude of the Fourier Transform
Cv2.imshow(‘Fourier Transform’, magnitude)
Cv2.waitKey(0)
Cv2.destroyAllWindows()
ORIGINAL IMAGE :-
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Image Processing Techniques with Python and OpenCV and more Cheat Sheet Computer Vision in PDF only on Docsity!

EXPT 4 :

FOURIER TRANSFORM:-

PROGRAM :-

Import numpy as np

now we will be loading the image and converting it to grayscale

Image = cv2.imread(“flower.jpg”) Gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

Compute the discrete Fourier Transform of the image

Fourier = cv2.dft(np.float32(gray), flags=cv2.DFT_COMPLEX_OUTPUT)

Shift the zero-frequency component to the center of the spectrum

Fourier_shift = np.fft.fftshift(fourier)

calculate the magnitude of the Fourier Transform

Magnitude = 20*np.log(cv2.magnitude(fourier_shift[:,:,0],fourier_shift[:,:,1]))

Scale the magnitude for display

Magnitude = cv2.normalize(magnitude, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8UC1)

Display the magnitude of the Fourier Transform

Cv2.imshow(‘Fourier Transform’, magnitude) Cv2.waitKey(0) Cv2.destroyAllWindows() ORIGINAL IMAGE :-

OUTPUT :-

HOUGH TRANSFORM :-

PROGRAM :-

Import cv Import numpy as np Img1=cv2.imread(“sudoku.jpg”, cv2.IMREAD_GRAYSCALE) Img2=cv2.imread(“exp4b.jpg”, cv2.IMREAD_GRAYSCALE) Orbb=cv2.ORB_create() Kp1, des1 = orbb.detectAndCompute(img1, None) Kp2, des2 = orbb.detectAndCompute(img2, None) Brute_force=cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True) Matches=brute_force.match(des1, des2) Matches=sorted(matches, key=lambda x:x.distance) Matching_results=cv2.drawMatches(img1, kp1, img2, kp2, matches, None) Cv2.imshow(“Feature_Matching”, matching_results) Cv2.waitKey(0) Cv2.destroyAllWindows()

Kp, des = orb.compute(img, kp)

draw only keypoints location,not size and orientation

Img2 = cv.drawKeypoints(img, kp, None, color=(0,255,0), flags=0) Plt.imshow(img2), plt.show() ORIGINAL IMAGE :- OUTPUT :-

FEATURE MATCHING :-

PROGRAM :-

Import cv Import numpy as np Img1=cv2.imread(“exp4.jpg”, cv2.IMREAD_GRAYSCALE) Img2=cv2.imread(“exp4b.jpg”, cv2.IMREAD_GRAYSCALE) Orbb=cv2.ORB_create()

Kp1, des1 = orbb.detectAndCompute(img1, None) Kp2, des2 = orbb.detectAndCompute(img2, None) Brute_force=cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True) Matches=brute_force.match(des1, des2) Matches=sorted(matches, key=lambda x:x.distance) Matching_results=cv2.drawMatches(img1, kp1, img2, kp2, matches, None) Cv2.imshow(“Feature_Matching”, matching_results) Cv2.waitKey(0) Cv2.destroyAllWindows() ORIGINAL IMAGE :- OUTPUT :-

FEATURE MATCHING BASED ALIGNMENT:-

PROGRAM :-

Import cv Import numpy as np

Load images

Img1 = cv2.imread(‘exp4.jpg’, cv2.IMREAD_GRAYSCALE) Img2 = cv2.imread(‘exp4b.jpg’, cv2.IMREAD_GRAYSCALE)

Initialize feature detector (ORB)

Orb = cv2.ORB_create()

Find keypoints and descriptors

Kp1, des1 = orb.detectAndCompute(img1, None) Kp2, des2 = orb.detectAndCompute(img2, None)

Initialize matcher (Brute-Force)

Bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)

Match descriptors

Matches = bf.match(des1, des2)

Sort matches by distance

Matches = sorted(matches, key=lambda x: x.distance)

Draw matches

Img_matches = cv2.drawMatches(img1, kp1, img2, kp2, matches[:10], None, Flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS)

Display result

Cv2.imshow(‘Matches’, img_matches) Cv2.waitKey(0) Cv2.destroyAllWindows()

Calculate homography

Src_pts = np.float32([kp1[m.queryIdx].pt for m in matches]).reshape(-1, 1, 2) Dst_pts = np.float32([kp2[m.trainIdx].pt for m in matches]).reshape(-1, 1, 2)

H, _ = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC)

Warp image

Aligned_img = cv2.warpPerspective(img1, H, (img2.shape[1], img2.shape[0]))

Display aligned image

Cv2.imshow(‘Aligned Image’, aligned_img) Cv2.waitKey(0) Cv2.destroyAllWindows() ORIGINAL IMAGE :- OUPUT :-

EXPT 5 :-

IMAGE SEGMENTATION USING GRABCUT :-

PROGRAM :-

Import numpy as np Import cv Def grabcut_segmentation(image):

ORIGINAL IMAGE :-

OUTPUT :-