The method can also feel their own angle rewritten



import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
import math

img = cv.imread(r'C:\Users\51102\Desktop\tradition\1.jpg',0)
img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
img = np.dot(img[..., :3], [0.299, 0.587, 0.114])
plt.subplot(331)
plt.imshow(img,cmap='gray')
plt.title('original_img')
print('original.shape=',img.shape)

# 构建梯度
gradient_x=np.array([[-1,0,1],[-2,0,2],[-1,0,1]])
gradient_y=np.array([[-1,-2,-1],[0,0,0],[1,2,1]])

plt.subplot(332)

dx=cv.filter2D(img,ddepth=-1,kernel=gradient_x)
print('dx.shape=',dx.shape)
plt.imshow(dx,cmap='gray')
plt.title('dx')
plt.subplot(333)
cv.filter2D = Dy (IMG, -1, gradient_x)
Print ( 'dy.shape =', dy.shape)
plt.imshow (Dy, CMap = 'Gray')
plt.title ( 'Dy')




# Directional


plt .subplot (334)
Magnitude = (np.multiply (Dy, Dy) + np.multiply (DX, DX))
plt.imshow (Magnitude, CMap = 'Gray')
plt.title ( 'Magnitude')



CITA = CV. phase (dy, dx, angleInDegrees = True) # find the angle


# generated Gaussian filter
"" "
Gaussian filter to generate a (2k + 1) x (2k + 1) , the individual elements of the filter is calculated as follows:
H [i, j] = (1 / (2 * pi * sigma ** 2)) * exp (-1 / 2 * sigma ** 2 ((ik-1) ** 2 + (jk-1) ** 2 ))
"" "
Sigma- 1 = Sigma2 = 0.6
gau_sum = 0
Gaussian np.zeros = ([. 5,. 5])
for I in Range (. 5):
for J in Range (. 5):
gaussian [i, j] = math.exp ((- 1 / (2 * sigma1 * sigma2)) * (np.square (i-2-1) + np.square (j-2-1))) / ( Math.PI Sigma- 1 * * * 2 Sigma2)
gau_sum = gau_sum Gaussian + [I, J]
Gaussian = Gaussian / gau_sum # normalized

Print ( '= Guss', gaussian.shape)

W is, H = original img.shape # grayscale image size
_, G = gaussian.shape
new_gray np.zeros = ([W is-. 5,. 5-H])
new_magnitude np.zeros = ([W is-. 5,. 5-H])

for I in Range (W is -5):
for J in Range (. 5-H):
'' '
filtering my own attempt a method, according to the angle to reconfigure the weights
I to reduce the amplitude of the larger gradient of the gradient direction can be suppressed, other strengthening direction
'' '
Guss = Gaussian
G_ = int ((. 1-G) / 2)

IF 0 <CITA [I, J] <90 or 180 [<CITA [I, J] <270 or -180 <CITA [I , j] <- 90 or -360 <cita [i,j]<-270:
for k in range(g_):
for kk in range(g_):
guss[g_+1+k,g_+1+kk]=gaussian[g_+1+k,g_+1+kk]/4
guss[g_-1 - k, g_ - 1 - kk] = gaussian[g_ - 1 - k, g_ - 1 - kk] / 4
if 90 < cita[i, j] < 180 or 270 < cita[i, j] < 360 or -90 < cita[i, j] < 0 or -180 < cita[i, j] < -270:
for k in range(g_):
for kk in range(g_):
guss[g_ + 1 + k, g_ - 1 - kk] = gaussian[g_ + 1 + k, g_ - 1 - kk] / 4
guss[g_ - 1 - k, g_ + 1 + kk] = gaussian[g_ - 1 - k, g_ + 1 + kk] / 4
new_gray[i, j] = np.sum(img[i:i+5, j:j+5] * guss)
new_magnitude[i, j] = np.sum(magnitude[i:i + 5, j:j + 5] * guss)








plt.subplot(335)
plt.imshow(new_gray,cmap='gray')
plt.title('new_gray')

plt.subplot(336)
plt.imshow(new_magnitude,cmap='gray')
plt.title('new_magnitude')




plt.show()




 

 








Guess you like

Origin www.cnblogs.com/tangjunjun/p/12514660.html