close
FFT spectrum amplitude using median filters with different kernel sizes
ex13_socre_median_filters.py
FFT spectrum amplitude using different filters, including median filter, moving average filter, and Laplacian filter.
ex14_socre_high_freq.py
ex17_simple_blockreduce.py
from utils import resizewithpool, rgb2gray
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
from skimage.measure import block_reduce
x = np.arange(1,82).reshape(9, 9)
x2 = block_reduce(x, (2, 2), np.max)
x3 = block_reduce(x, (3, 3), np.max)
print(f'x:{x}, \nx2:{x2}, \nx3:{x3}')
we did a block processing, and get a max value in each subblock, like this
ex15_blockreduce.py
kernel = np.ones((3,3),np.float)
n gets smaller when p_size gets higher.
block_reduce(grad, (n, n), np.max) lead grad to remain more details when n is getting smaller and smaller.
For example, when p_size is 30, n is about 11 (342/30)
and n is becoming to be about 3 (342/120) when p_size is 120.
img = cv.imread('E:\OpenCV\images\messi5.jpg')
# img = cv.imread('E:\OpenCV\images\lena.jpg')
# min(高度, 寬度)
image_dim = int(min(img.shape[0:2]))
gray = rgb2gray(img)
grad = np.abs(cv.Sobel(gray, cv.CV_64F, 0, 1, ksize=3)) + np.abs(cv.Sobel(gray, cv.CV_64F, 1, 0, ksize=3))
# 計算縮放至image_dim大小
grad = cv.resize(grad, (image_dim, image_dim), cv.INTER_AREA)
# thresholding the gradient map to generate the edge-map as a proxy of the contextual cues
# m:最小強度 M:最大強度
m = grad.min()
M = grad.max()
middle = m + (0.4 * (M - m))
# grad低於中間值為0,否則為1
grad[grad < middle] = 0
grad[grad >= middle] = 1
kernel = np.ones((3,3),np.float)
# hei = img.shape[0]
# scale = 8
# p_size = int( hei/scale ), p_size means pace size
# p_size: 12, 18, 24, 30, 36, 42
# mean_value: 0, 0.031, 0.1296, 0.2197, 0.28, 0.3396
# case #1
p_size = 30
i_size = img.shape[0]
n = int(np.floor(i_size/p_size))
grad_resized1 = skimage.measure.block_reduce(grad, (n, n), np.max)
cv.resize(grad_resized1, (p_size, p_size), cv.INTER_NEAREST)
# case #2
p_size = 60
n = int(np.floor(i_size/p_size))
grad_resized2 = skimage.measure.block_reduce(grad, (n, n), np.max)
cv.resize(grad_resized2, (p_size, p_size), cv.INTER_NEAREST)
# case #3
p_size = 120
n = int(np.floor(i_size/p_size))
grad_resized3 = skimage.measure.block_reduce(grad, (n, n), np.max)
cv.resize(grad_resized3, (p_size, p_size), cv.INTER_NEAREST)
# 將grad_resized邊緣進行形態學 變粗
dilated1 = cv.dilate(grad_resized1, kernel, iterations=1)
meanvalue1 = (1-dilated1).mean()
dilated2 = cv.dilate(grad_resized2, kernel, iterations=1)
meanvalue2 = (1-dilated2).mean()
dilated3 = cv.dilate(grad_resized3, kernel, iterations=1)
meanvalue3 = (1-dilated3).mean()
print(f'meanvalues: ({meanvalue1},{meanvalue2}{meanvalue3})')
plt.subplot(221).set_title('original image: '+ f'{img.shape[0]}*{img.shape[1]}')
plt.imshow(cv.cvtColor(img,cv.COLOR_BGR2RGB))
plt.subplot(222).set_title('p_size = 20, 1-dialate ='+f'{meanvalue1:.3f}')
plt.imshow(1-dilated1)
plt.subplot(223).set_title('p_size = 40, 1-dialate ='+f'{meanvalue2:.3f}')
plt.imshow(1-dilated2)
plt.subplot(224).set_title('p_size = 60, 1-dialate ='+f'{meanvalue3:.3f}')
plt.imshow(1-dilated3)
plt.show()
全站熱搜
留言列表