Saturday, 20 March 2021

opencv 18 blending image with pyramids




stacked image

blended image


image pyramid (same image with different resolutions)

Laplacian pyramid (low resolution image + edge detection at different resolution) 
#main.py
import numpy as np
import cv2

mountain1 = cv2.imread('assets/mountain1.jpg')
mountain1 = cv2.resize(mountain1, (1200, 800))
#print(mountain1.shape)
mountain2 = cv2.imread('assets/mountain2.jpg')
mountain2 = cv2.resize(mountain2, (1200, 800))
#print(mountain2.shape)

mountain_stack = np.hstack((mountain1[:, :700], mountain2[:, 700:]))

#generate Gaussian pyramid for mountain1
mountain1_copy = mountain1.copy()
mountain1_pyramid = [mountain1_copy]
for i in range(6):
    #decrease image resolution
    mountain1_copy = cv2.pyrDown(mountain1_copy)
    mountain1_pyramid.append(mountain1_copy)

#generate Gaussian pyramid for mountain2
mountain2_copy = mountain2.copy()
mountain2_pyramid = [mountain2_copy]
for i in range(6):
    #decrease image resolution
    mountain2_copy = cv2.pyrDown(mountain2_copy)
    mountain2_pyramid.append(mountain2_copy)

#generate Laplacina Pyramid for mountain1
mountain1_copy = mountain1_pyramid[5]
lp_mountain1 = [mountain1_copy]
for i in range(5, 0, -1):
    resolution_up = cv2.pyrUp(mountain1_pyramid[i])
    shape = mountain1_pyramid[i-1].shape
    resolution_up = cv2.resize(resolution_up, (shape[1], shape[0]))
    laplacian = cv2.subtract(mountain1_pyramid[i-1], resolution_up)
    lp_mountain1.append(laplacian)

#generate Laplacina Pyramid for mountain2
mountain2_copy = mountain2_pyramid[5]
lp_mountain2 = [mountain2_copy]
for i in range(5, 0, -1):
    resolution_up = cv2.pyrUp(mountain2_pyramid[i])
    shape = mountain2_pyramid[i - 1].shape
    resolution_up = cv2.resize(resolution_up, (shape[1], shape[0]))
    laplacian = cv2.subtract(mountain2_pyramid[i-1], resolution_up)
    lp_mountain2.append(laplacian)

#stack laplacian images
lp_pyramid = []
for mount1_lap, mount2_lap in zip(lp_mountain1, lp_mountain2):
    row, col, ch = mount1_lap.shape
    #print(mount1_lap.shape)
    lp_stack = np.hstack((mount1_lap[:, :int(col*7/12)], mount2_lap[:, int(col*7/12):]))
    #print(lp_stack.shape)
    lp_pyramid.append(lp_stack)

#reconstruct stacked image
#reconstruct[0] has a low resolution stacked image
#reconstruct[1] is generated by adding scaled up reconstruct[0] and corresponding laplacian edges
#reconstruct[2] is generated by adding scaled up reconstruct[1] and corresponding laplacian edges
reconstruct = lp_pyramid[0]
for i in range(1, 6):
    reconstruct = cv2.pyrUp(reconstruct)
    #print(reconstruct.shape)
    #print(lp_pyramid[i].shape)
    shape = lp_pyramid[i].shape
    reconstruct = cv2.resize(reconstruct, (shape[1], shape[0]))
    reconstruct = cv2.add(lp_pyramid[i], reconstruct)

cv2.imshow('mountain1', mountain1)
cv2.imshow('mountain2', mountain2)
cv2.imshow('mountain stack', mountain_stack)

"""
for i in range(6):
    image_name = 'mountain2 pyramid' + str(i)
    cv2.imshow(image_name, mountain2_pyramid[i])

for i in range(5, -1, -1):
    image_name = 'mountain laplacian ' + str(i)
    cv2.imshow(image_name, lp_mountain1[i])
"""

cv2.imshow('blended image', reconstruct)

cv2.waitKey(0)
cv2.destroyAllWindows()

reference:

Thursday, 18 March 2021

运煤

opencv 17 canny edge detection



gray scale

canny edge detection

#main.py
import numpy as np
import cv2

cap = cv2.VideoCapture("assets/Santa Barbara.mp4")
high = 200
low = 100

trackbars_img = np.uint8(np.full((50, 500, 3), 255))
cv2.imshow('trackbars', trackbars_img)

def high_change(x):
    global high
    high = x

def low_change(x):
    global low
    low = x

cv2.createTrackbar('low', 'trackbars', 100, 150, low_change)
cv2.createTrackbar('high', 'trackbars', 200, 255, high_change)

while True:
    ret, frame = cap.read()
    width = int(cap.get(3))
    height = int(cap.get(4))

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

    canny = cv2.Canny(gray, low, high)

    cv2.imshow('frame', frame)
    cv2.imshow('gray', gray)
    cv2.imshow('canny', canny)

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

    if cv2.waitKey(1) == ord('p'):
        # wait until any key is pressed
        cv2.waitKey(-1)

cap.release()
cv2.destroyAllWindows()

reference:

Cost of living in Hawaii



Wednesday, 17 March 2021

Las Vegas

opencv 16 image gradients | edge detection

An image gradient is a directional change in the intensity or color in an image.


gray scale

Laplacian transform, edges are detected 

sobelX detects edge along Y axis

sobelY detects edge along X axis

sobelXY detects all edges
#main.py
import numpy as np
import cv2

cap = cv2.VideoCapture("assets/Santa Barbara.mp4")

while True:
    ret, frame = cap.read()
    width = int(cap.get(3))
    height = int(cap.get(4))

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

    lap = cv2.Laplacian(gray, cv2.CV_64F, ksize=3)
    lap = np.uint8(np.absolute(lap))

    sobelX = cv2.Sobel(gray, cv2.CV_64F, 1, 0)
    sobelY = cv2.Sobel(gray, cv2.CV_64F, 0, 1)

    sobelX = np.uint8(np.absolute(sobelX))
    sobelY = np.uint8(np.absolute(sobelY))

    sobelCombined = cv2.bitwise_or(sobelX, sobelY)

    cv2.imshow('frame', frame)
    cv2.imshow('gray', gray)
    cv2.imshow('laplacian', lap)
    cv2.imshow('sobelX', sobelX)
    cv2.imshow('sobelY', sobelY)
    cv2.imshow('sobelCombined', sobelCombined)

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

    if cv2.waitKey(1) == ord('p'):
        # wait until any key is pressed
        cv2.waitKey(-1)

cap.release()
cv2.destroyAllWindows()

reference:

Tuesday, 16 March 2021

opencv 15 smoothing | blurring



adaptive threshold

blur (2d convolution)

2d convolution kernel

gaussian blur removes high frequency noise

gaussian convolution kernel

median blur removes salt and pepper noise

bilateral blur preserves edges
#main.py
import numpy as np
import cv2

cap = cv2.VideoCapture("assets/Santa Barbara.mp4")

while True:
    ret, frame = cap.read()
    width = int(cap.get(3))
    height = int(cap.get(4))

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

    #adaptive threshold (frame, max pixel value, adaptive method, threshold type, neighbour block size, c constant)
    th1 = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2)
    #th2 = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)

    kernel = np.ones((5, 5), np.float32)/25
    #conv = cv2.filter2D(th1, -1, kernel)
    blur = cv2.blur(th1, (5,5))

    #gaussianBlur (frame, kernel, sigma)
    #designed to remove high frequency noise
    gblur = cv2.GaussianBlur(th1, (5, 5), 0)

    #median method designed to remove salt and pepper noise
    median = cv2.medianBlur(th1, 5)

    #bilateralFilter preserve edges
    bilateralFilter = cv2.bilateralFilter(th1, 9, 75, 75)

    cv2.imshow('frame', frame)
    cv2.imshow('threshold', th1)
    cv2.imshow('convolution', blur)
    cv2.imshow('gaussian', gblur)
    cv2.imshow('median', median)
    cv2.imshow('bilater', bilateralFilter)

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

    if cv2.waitKey(1) == ord('p'):
        # wait until any key is pressed
        cv2.waitKey(-1)

cap.release()
cv2.destroyAllWindows()

reference: