Skip to content

Commit e9f2b28

Browse files
author
Spencer.Luo
committed
Add DifferentialDerivative class
1 parent 5f300ce commit e9f2b28

File tree

2 files changed

+100
-3
lines changed

2 files changed

+100
-3
lines changed

Test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@
3030
# import advanced_pattern.ObjectPool
3131
# import advanced_pattern.Callback
3232

33-
# import application.ImageProcessing
34-
import application.Principle
33+
import application.ImageProcessing
34+
# import application.Principle

application/ImageProcessing.py

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,56 @@ def preProcessing(self, img):
6666
return cv2.addWeighted(absX, 0.5, absY, 0.5, 0)
6767

6868

69+
70+
# 一阶微分算子
71+
#=======================================================================================================================
72+
from abc import ABCMeta, abstractmethod
73+
# 引入ABCMeta和abstractmethod来定义抽象类和抽象方法
74+
import numpy as np
75+
# 引入numpy模块
76+
77+
class DifferentialDerivative(metaclass=ABCMeta):
78+
"""微分求导算法"""
79+
80+
def imgProcessing(self, img, width, height):
81+
"""模板方法,进行图像处理"""
82+
# 这里特别需要注意:OpenCv for Python中,(x, y)坐标点的像素用img[y, x]表示
83+
newImg = np.zeros([height, width], dtype=np.uint8)
84+
for y in range(0, height):
85+
for x in range(0, width):
86+
# 因为是采用(3*3)的核进行处理,所以最边上一圈的像素无法处理,需保留原值
87+
if (y != 0 and y != height-1 and x != 0 and x != width-1):
88+
value = self.derivation(img, x, y)
89+
# 小于0的值置为0,大于255的值置为255
90+
value = 0 if value < 0 else (255 if value > 255 else value)
91+
newImg[y, x] = value
92+
else:
93+
newImg[y, x] = img[y, x]
94+
return newImg
95+
96+
@abstractmethod
97+
def derivation(self, img, x, y):
98+
"""具体的步骤由子类实现"""
99+
pass
100+
101+
class DifferentialDerivativeX(DifferentialDerivative):
102+
"""水平微分求导算法"""
103+
104+
def derivation(self, img, x, y):
105+
"""Gx=f(x-1,y-1) + 2f(x-1,y) + f(x-1,y+1) - f(x+1,y-1) - 2f(x+1,y) - f(x+1, y+1)"""
106+
pix = img[y-1, x-1] + 2 * img[y, x-1] + img[y+1, x-1] - img[y-1, x+1] - 2 *img[y, x+1] - img[y+1, x+1]
107+
return pix
108+
109+
110+
class DifferentialDerivativeY(DifferentialDerivative):
111+
"""垂直微分求导算法"""
112+
113+
def derivation(self, img, x, y):
114+
"""Gy=f(x-1,y-1) + 2f(x,y-1) + f(x+1,y-1) - f(x-1,y+1) - 2f(x,y+1) - f(x+1,y+1)"""
115+
pix = img[y-1, x-1] + 2*img[y-1, x] + img[y-1, x+1] - img[y+1, x-1] - 2*img[y+1, x] - img[y+1, x+1]
116+
return pix
117+
118+
69119
def testImageProcessing():
70120
img = cv2.imread("E:\\TestImages\\bird.jpg")
71121
print("灰度化 --> 梯度化 --> 核心算法:边缘提取算法:")
@@ -81,4 +131,51 @@ def testImageProcessing():
81131
cv2.waitKey(0)
82132
cv2.destroyAllWindows()
83133

84-
testImageProcessing()
134+
def differentialDerivativeOpenCv():
135+
img = cv2.imread("E:\\TestImages\\person.jpg")
136+
137+
# 转换成单通道灰度图
138+
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
139+
140+
x = cv2.Sobel(img, cv2.CV_16S, 1, 0)
141+
y = cv2.Sobel(img, cv2.CV_16S, 0, 1)
142+
# 进行微分计算后,可能会出现负值,将每个像素加上最小负数的绝对值
143+
absX = cv2.convertScaleAbs(x) # 转回uint8
144+
absY = cv2.convertScaleAbs(y)
145+
# img = cv2.addWeighted(absX, 0.5, absY, 0.5, 0)
146+
147+
cv2.imshow("First order differential X", absX)
148+
cv2.imshow("First order differential Y", absY)
149+
cv2.waitKey(0)
150+
cv2.destroyAllWindows()
151+
152+
153+
def differentialDerivative():
154+
img = cv2.imread("E:\\TestImages\\person.jpg")
155+
156+
# 转换成单通道的灰度图
157+
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
158+
# 均值滤波
159+
# img = cv2.blur(img, (3, 3))
160+
161+
# 获取图片的宽和高
162+
width = img.shape[1]
163+
height = img.shape[0]
164+
# 进行微分求导
165+
derivativeX = DifferentialDerivativeX()
166+
imgX = derivativeX.imgProcessing(img, width, height)
167+
derivativeY = DifferentialDerivativeY()
168+
imgY = derivativeY.imgProcessing(img, width, height)
169+
imgScobel = cv2.addWeighted(imgX, 0.5, imgY, 0.5, 0)
170+
171+
cv2.imshow("First order differential X", imgX)
172+
cv2.imshow("First order differential Y", imgY)
173+
cv2.imshow("First order differential Scobel", imgScobel)
174+
cv2.waitKey(0)
175+
cv2.destroyAllWindows()
176+
177+
178+
# testImageProcessing()
179+
# differentialDerivativeOpenCv()
180+
differentialDerivative()
181+

0 commit comments

Comments
 (0)