Popular Posts

May 22, 2020

Contours


Contours is a python list of all the contours in the image. Each individual contour is a numpy array of (x,y) coordinates of boundary points of the object.






import numpy as np
import cv2

img = cv2.imread(
'opencv-logo.png')
imgray = cv2.cvtColor(img
, cv2.COLOR_BGR2GRAY)
ret
, thresh = cv2.threshold(imgray, 127, 255, 0)
contours
, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
print("Number of contours = "+str(len(contours)))
print(contours[0])


cv2.drawContours(img
, contours, 4, (0, 255, 0), 3)
#we can change the parameter -1(draw all contours) to 0to8 because the total contours is 9.

cv2.imshow('Image', img)
cv2.imshow(
'Image GRAY', imgray)
cv2.waitKey(
0)
cv2.destroyAllWindows()


Image Blending

Steps Image Blending:  
1.       Load the two images of apple and orange
2.       Find the Gaussian pyramids for apple and orange(in this particular example, number of lavels is 6).
3.       From Gaussian pyramids, find their laplacian pyramids.
4.       Now join the left half of apple and right half of orange in each levels of laplacian pyramids.
5.       Finally from this joint image pyramids, reconstruct the original image.

Note: When we apply the Gaussian and laplacian pyramid the line between apple and orange is not visible.




import cv2
import numpy as np
apple = cv2.imread(
'apple.jpg')
orange = cv2.imread(
'orange.jpg')
print(apple.shape)
print(orange.shape)
apple_orange = np.hstack((apple[:
, :256], orange[:, 256:]))
#generate Gaussian pyramid for apple
apple_copy = apple.copy()
gp_apple = [apple_copy]
for i in range(6):
    apple_copy = cv2.pyrDown(apple_copy)
    gp_apple.append(apple_copy)

#generate Gaussian pyramid for orange
orange_copy = orange.copy()
gp_orange = [orange_copy]
for i in range(6):
    orange_copy = cv2.pyrDown(orange_copy)
    gp_orange.append(orange_copy)

#generate Laplacian pyramid for apple
apple_copy = gp_apple[5]
lp_apple = [apple_copy]
for i in range(5, 0, -1):
    gaussian_expanded = cv2.pyrUp(gp_apple[i])
    laplacian = cv2.subtract(gp_apple[i-
1], gaussian_expanded)
    lp_apple.append(laplacian)

#generate Laplacian pyramid for orange
orange_copy = gp_orange[5]
lp_orange = [orange_copy]
for i in range(5, 0, -1):
    gaussian_expanded = cv2.pyrUp(gp_orange[i])
    laplacian = cv2.subtract(gp_orange[i-
1], gaussian_expanded)
    lp_orange.append(laplacian)

#now add left and right halves of images in each level
apple_orange_pyramid = []
n =
0
for apple_lap, orange_lap in zip(lp_apple, lp_orange):
    n +=
1
   
cols, rows, ch = apple_lap.shape
    laplacian = np.hstack((apple_lap[:
, 0:int(cols/2)], orange_lap[:, int(cols/2):]))
    apple_orange_pyramid.append(laplacian)

#now reconstruct
apple_orange_reconstruct = apple_orange_pyramid[0]
for i in range(1, 6):
    apple_orange_reconstruct = cv2.pyrUp(apple_orange_reconstruct)
    apple_orange_reconstruct = cv2.add(apple_orange_pyramid[i]
, apple_orange_reconstruct)

cv2.imshow(
"apple", apple)
cv2.imshow(
"orange", orange)
cv2.imshow(
"apple_orange", apple_orange)
cv2.imshow(
"apple_orange_reconstruct", apple_orange_reconstruct)
cv2.waitKey(
0)
cv2.destroyAllWindows()

Image pyramid

pyramid, or pyramid representation, is a type of multi-scale signal representation in which a signal or image is subject to repeated smoothing and subsampling.
Types of pyramid:
1.       Gaussian pyramid
2.       Laplacian pyramid
-A lavel in laplacian pyramid is formed by the difference between that level in  Gaussian pyramid and expanded version of its upper level in Gaussian pyramid.
Uses of laplacian/Gaussian pyramid: help us to bland the images and reconstruction of the images.



import cv2
import numpy as np

img = cv2.imread(
"lena.jpg")
#lower resolution
 #lr1 = cv2.pyrDown(img)
 #lr2 = cv2.pyrDown(lr1)
#higher resolution
 #hr2 = cv2.pyrUp(lr2)

 #cv2.imshow("pyrDown 1 image", lr1)
 #cv2.imshow("pyrDown 2 image", lr2)
 #cv2.imshow("pyrUp 1 image", hr2)
 #in gaussian two methods available
  #-pyrDown and pyrUp
#but in laplacian pyramid no methods available
#A level in laplacian pyramid is formed by the difference between that lavel
#in gaussian pyramid and expanded version of its upper level in gaussian pyramid

layer = img.copy()
gp=[layer]

for i in range(6):
    layer = cv2.pyrDown(layer)
    gp.append(layer)
  
# cv2.imshow(str(i), layer)

layer = gp[5]
cv2.imshow(
'upper level gaussian pyramid', layer)
lp = [layer]

for i in range(5, 0, -1):
  
# print(i)
   
gaussian_extended = cv2.pyrUp(gp[i])
    laplacian = cv2.subtract(gp[i-
1], gaussian_extended)
    cv2.imshow(
str(i), laplacian)

cv2.imshow(
"Original image", img)
cv2.waitKey(
0)
cv2.destroyAllWindows()

image gradients and edge detection




import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread(
"messi5.jpg", cv2.IMREAD_GRAYSCALE)
#img = cv2.imread("sudoku.png", cv2.IMREAD_GRAYSCALE)
lap = cv2.Laplacian(img, cv2.CV_64F, ksize=3)
lap = np.uint8(np.absolute(lap))
sobelX = cv2.Sobel(img
, cv2.CV_64F, 1,0)
sobelY = cv2.Sobel(img
, cv2.CV_64F, 0, 1)
canny = cv2.Canny(img
, 100, 200)

sobelX = np.uint8(np.absolute(sobelX))
sobelY = np.uint8(np.absolute(sobelY))
sobelCombined = cv2.bitwise_or(sobelX
, sobelY)

titles = [
'image', 'Laplacian', 'sobelX', 'sobelY', 'sobelCombined', 'canny']
images = [img
, lap, sobelX, sobelY, sobelCombined, canny]
for i in range(6):
    plt.subplot(
2, 3, i+1), plt.imshow(images[i], 'gray')
    plt.title(titles[i])
    plt.xticks([])
, plt.yticks([])

plt.show()

Image gradients and edge detection



import cv2
import numpy as np
from matplotlib import pyplot as plt

#img = cv2.imread("messi5.jpg", cv2.IMREAD_GRAYSCALE)
img = cv2.imread("sudoku.png", cv2.IMREAD_GRAYSCALE)
lap = cv2.Laplacian(img
, cv2.CV_64F, ksize=3)
lap = np.uint8(np.absolute(lap))
sobelX = cv2.Sobel(img
, cv2.CV_64F, 1,0)
sobelY = cv2.Sobel(img
, cv2.CV_64F, 0, 1)

sobelX = np.uint8(np.absolute(sobelX))
sobelY = np.uint8(np.absolute(sobelY))
sobelCombined = cv2.bitwise_or(sobelX
, sobelY)

titles = [
'image', 'Laplacian', 'sobelX', 'sobelY', 'sobelCombined']
images = [img
, lap, sobelX, sobelY, sobelCombined]
for i in range(5):
    plt.subplot(
2, 3, i+1), plt.imshow(images[i], 'gray')
    plt.title(titles[i])
    plt.xticks([])
, plt.yticks([])

plt.show()

Image Blurring



import cv2
import numpy as np
from matplotlib import pyplot as plt

#test diff images
#img = cv2.imread('opencv-logo.png')
#img = cv2.imread('gauss_images.png')
img = cv2.imread('Noise_salt_and_pepper.png')
#img = cv2.imread('lena.jpg')


img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

kernel = np.ones((
5,5), np.float32)/25
dst = cv2.filter2D(img, -1, kernel)
blur = cv2.blur(img
, (5,5))
#Applying gaussian blur method
gblur = cv2.GaussianBlur(img, (5,5), 0)
median = cv2.medianBlur(img
, 5)
bilateralFilter = cv2.bilateralFilter(img
, 9, 75, 75)

titles = [
'image', '2D convolution', 'Blur', 'GaussianBlur', 'median', 'bilateralFilter']
images = [img
, dst, blur, gblur, median, bilateralFilter]

for i in range(6):
    plt.subplot(
2, 3, i+1), plt.imshow(images[i], 'gray')
    plt.title(titles[i])
    plt.xticks([])
, plt.yticks([])

plt.show()