forked from xudiandian111/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
convolve and sobel (TheAlgorithms#971)
* add gaussian filter algorithm and lena.jpg * add img_convolve algorithm and sobel_filter
- Loading branch information
1 parent
32d5c1a
commit e2d9953
Showing
2 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# @Author : lightXu | ||
# @File : convolve.py | ||
# @Time : 2019/7/8 0008 下午 16:13 | ||
from cv2 import imread, cvtColor, COLOR_BGR2GRAY, imshow, waitKey | ||
from numpy import array, zeros, ravel, pad, dot, uint8 | ||
|
||
|
||
def im2col(image, block_size): | ||
rows, cols = image.shape | ||
dst_height = cols - block_size[1] + 1 | ||
dst_width = rows - block_size[0] + 1 | ||
image_array = zeros((dst_height * dst_width, block_size[1] * block_size[0])) | ||
row = 0 | ||
for i in range(0, dst_height): | ||
for j in range(0, dst_width): | ||
window = ravel(image[i:i + block_size[0], j:j + block_size[1]]) | ||
image_array[row, :] = window | ||
row += 1 | ||
|
||
return image_array | ||
|
||
|
||
def img_convolve(image, filter_kernel): | ||
height, width = image.shape[0], image.shape[1] | ||
k_size = filter_kernel.shape[0] | ||
pad_size = k_size//2 | ||
# Pads image with the edge values of array. | ||
image_tmp = pad(image, pad_size, mode='edge') | ||
|
||
# im2col, turn the k_size*k_size pixels into a row and np.vstack all rows | ||
image_array = im2col(image_tmp, (k_size, k_size)) | ||
|
||
# turn the kernel into shape(k*k, 1) | ||
kernel_array = ravel(filter_kernel) | ||
# reshape and get the dst image | ||
dst = dot(image_array, kernel_array).reshape(height, width) | ||
return dst | ||
|
||
|
||
if __name__ == '__main__': | ||
# read original image | ||
img = imread(r'../image_data/lena.jpg') | ||
# turn image in gray scale value | ||
gray = cvtColor(img, COLOR_BGR2GRAY) | ||
# Laplace operator | ||
Laplace_kernel = array([[0, 1, 0], [1, -4, 1], [0, 1, 0]]) | ||
out = img_convolve(gray, Laplace_kernel).astype(uint8) | ||
imshow('Laplacian', out) | ||
waitKey(0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# @Author : lightXu | ||
# @File : sobel_filter.py | ||
# @Time : 2019/7/8 0008 下午 16:26 | ||
import numpy as np | ||
from cv2 import imread, cvtColor, COLOR_BGR2GRAY, imshow, waitKey | ||
from digital_image_processing.filters.convolve import img_convolve | ||
|
||
|
||
def sobel_filter(image): | ||
kernel_x = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]) | ||
kernel_y = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]]) | ||
|
||
dst_x = img_convolve(image, kernel_x) | ||
dst_y = img_convolve(image, kernel_y) | ||
dst = np.sqrt((np.square(dst_x)) + (np.square(dst_y))).astype(np.uint8) | ||
degree = np.arctan2(dst_y, dst_x) | ||
return dst, degree | ||
|
||
|
||
if __name__ == '__main__': | ||
# read original image | ||
img = imread('../image_data/lena.jpg') | ||
# turn image in gray scale value | ||
gray = cvtColor(img, COLOR_BGR2GRAY) | ||
|
||
sobel, d = sobel_filter(gray) | ||
|
||
# show result images | ||
imshow('sobel filter', sobel) | ||
imshow('sobel degree', d) | ||
waitKey(0) |