sobel、prewitt、Isotropic算子处理加噪图片
程序员文章站
2022-07-14 11:02:05
...
#Isotropic算子
import cv2
import numpy as np
from PIL import Image
import math
import matplotlib.pyplot as plt
def Isotropic(str):
img = cv2.imread(str, 0).astype(np.float32)
matrix = np.asarray(img).astype(np.float32)
size = img.shape
mat=np.zeros(size)
for i in range(1,size[0]-1):
for j in range(1,size[1]-1):
dx=matrix[i-1][j-1]-matrix[i+1][j-1]+math.sqrt(2)*matrix[i-1][j]-math.sqrt(2)*matrix[i+1][j]+matrix[i-1][j+1]-matrix[i+1][j+1]
dy=matrix[i-1][j+1]+math.sqrt(2)*matrix[i][j+1]+matrix[i+1][j+1]-matrix[i-1][j-1]-math.sqrt(2)*matrix[i][j-1]-matrix[i+1][j-1]
S=math.sqrt(math.pow(dx,2)+math.pow(dy,2))
mat[i][j]=S
image = Image.fromarray(mat)
return image
img='002.jpg'
image=Isotropic(img)
plt.imshow(image)
plt.show()
#matlab上sobel和prewitt都是现成的,不得不说虽然
#现在深度学习很火,但是matlab还是要用起来
#传统方法很重要
image=imread('002.jpg');
image_noise=imnoise(image,'gaussian',0,0.02);
imshow(image_noise);
PRE=edge(image_noise,'prewitt',0.2);
figure;
imshow(PRE);
sobel=edge(image_noise,'sobel',0.2);
figure;
imshow(sobel);
效果: