GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — convolve and sobel (#971) · jusing-es/Python@e2d9953

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • digital_image_processing/filters
    • convolve.py
    • sobel_filter.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎digital_image_processing/filters/convolve.py‎

Copy file name to clipboard
+49Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# @Author : lightXu
2+
# @File : convolve.py
3+
# @Time : 2019/7/8 0008 下午 16:13
4+
from cv2 import imread, cvtColor, COLOR_BGR2GRAY, imshow, waitKey
5+
from numpy import array, zeros, ravel, pad, dot, uint8
6+
7+
8+
def im2col(image, block_size):
9+
rows, cols = image.shape
10+
dst_height = cols - block_size[1] + 1
11+
dst_width = rows - block_size[0] + 1
12+
image_array = zeros((dst_height * dst_width, block_size[1] * block_size[0]))
13+
row = 0
14+
for i in range(0, dst_height):
15+
for j in range(0, dst_width):
16+
window = ravel(image[i:i + block_size[0], j:j + block_size[1]])
17+
image_array[row, :] = window
18+
row += 1
19+
20+
return image_array
21+
22+
23+
def img_convolve(image, filter_kernel):
24+
height, width = image.shape[0], image.shape[1]
25+
k_size = filter_kernel.shape[0]
26+
pad_size = k_size//2
27+
# Pads image with the edge values of array.
28+
image_tmp = pad(image, pad_size, mode='edge')
29+
30+
# im2col, turn the k_size*k_size pixels into a row and np.vstack all rows
31+
image_array = im2col(image_tmp, (k_size, k_size))
32+
33+
# turn the kernel into shape(k*k, 1)
34+
kernel_array = ravel(filter_kernel)
35+
# reshape and get the dst image
36+
dst = dot(image_array, kernel_array).reshape(height, width)
37+
return dst
38+
39+
40+
if __name__ == '__main__':
41+
# read original image
42+
img = imread(r'../image_data/lena.jpg')
43+
# turn image in gray scale value
44+
gray = cvtColor(img, COLOR_BGR2GRAY)
45+
# Laplace operator
46+
Laplace_kernel = array([[0, 1, 0], [1, -4, 1], [0, 1, 0]])
47+
out = img_convolve(gray, Laplace_kernel).astype(uint8)
48+
imshow('Laplacian', out)
49+
waitKey(0)
Collapse file

‎digital_image_processing/filters/sobel_filter.py‎

Copy file name to clipboard
+31Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# @Author : lightXu
2+
# @File : sobel_filter.py
3+
# @Time : 2019/7/8 0008 下午 16:26
4+
import numpy as np
5+
from cv2 import imread, cvtColor, COLOR_BGR2GRAY, imshow, waitKey
6+
from digital_image_processing.filters.convolve import img_convolve
7+
8+
9+
def sobel_filter(image):
10+
kernel_x = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]])
11+
kernel_y = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]])
12+
13+
dst_x = img_convolve(image, kernel_x)
14+
dst_y = img_convolve(image, kernel_y)
15+
dst = np.sqrt((np.square(dst_x)) + (np.square(dst_y))).astype(np.uint8)
16+
degree = np.arctan2(dst_y, dst_x)
17+
return dst, degree
18+
19+
20+
if __name__ == '__main__':
21+
# read original image
22+
img = imread('../image_data/lena.jpg')
23+
# turn image in gray scale value
24+
gray = cvtColor(img, COLOR_BGR2GRAY)
25+
26+
sobel, d = sobel_filter(gray)
27+
28+
# show result images
29+
imshow('sobel filter', sobel)
30+
imshow('sobel degree', d)
31+
waitKey(0)

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.