prewitt算子python代码
时间: 2023-07-09 18:36:47 浏览: 101
下面是一个使用Prewitt算子的Python代码示例:
```python
import cv2
import numpy as np
# 读取图像
img = cv2.imread('image.jpg', 0)
# 应用Prewitt算子
prewittx = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=3)
prewitty = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=3)
# 合并x和y方向的结果
prewitt = cv2.addWeighted(prewittx, 0.5, prewitty, 0.5, 0)
# 显示结果
cv2.imshow('Prewitt Edge Detection', prewitt)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
在这个例子中,我们使用OpenCV库中的Sobel函数来应用Prewitt算子。Sobel函数需要指定x和y方向的导数,以及内核大小。在这里,我们使用了3x3的内核大小。最后,我们将x和y方向的结果合并起来,得到最终结果。
相关问题
Python prewitt算子代码
以下是Python实现的Prewitt算子代码:
```python
import cv2
import numpy as np
# 读取图像
img = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE)
# 定义Prewitt算子
kernelx = np.array([[1, 0, -1], [1, 0, -1], [1, 0, -1]])
kernely = np.array([[1, 1, 1], [0, 0, 0], [-1, -1, -1]])
# 使用Prewitt算子进行边缘检测
img_prewittx = cv2.filter2D(img, -1, kernelx)
img_prewitty = cv2.filter2D(img, -1, kernely)
img_prewitt = img_prewittx + img_prewitty
# 显示结果
cv2.imshow('Original Image', img)
cv2.imshow('Prewitt Edge Detection', img_prewitt)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
希望这个代码能够帮到你!
Roberts算子、Prewitt算子、Sobel算子、Laplacian算子、Canny算子 代码python
以下是对应算子的简单示例代码:
Roberts算子:
```python
from scipy import ndimage
import numpy as np
def robert_cross(image):
roberts_cross_v = np.array( [[-1,0],[0,1]], dtype=np.int32 )
roberts_cross_h = np.array( [[0,-1],[1,0]], dtype=np.int32 )
vertical = ndimage.convolve( image, roberts_cross_v )
horizontal = ndimage.convolve( image, roberts_cross_h )
return np.sqrt( np.square(horizontal) + np.square(vertical) )
```
Prewitt算子:
```python
from scipy import ndimage
import numpy as np
def prewitt(image):
vert_prewitt = np.array([[-1,-1,-1],[0,0,0],[1,1,1]])
hori_prewitt = np.array([[-1,0,1],[-1,0,1],[-1,0,1]])
prewitt_vert = ndimage.convolve(image, vert_prewitt)
prewitt_hori = ndimage.convolve(image, hori_prewitt)
return np.sqrt( np.square(prewitt_vert) + np.square(prewitt_hori) )
```
Sobel算子:
```python
from scipy import ndimage
import numpy as np
def sobel(image):
vert_sobel = np.array([[-1,-2,-1],[0,0,0],[1,2,1]])
hori_sobel = np.array([[-1,0,1],[-2,0,2],[-1,0,1]])
sobel_vert = ndimage.convolve(image, vert_sobel)
sobel_hori = ndimage.convolve(image, hori_sobel)
return np.sqrt( np.square(sobel_vert) + np.square(sobel_hori) )
```
Laplacian算子:
```python
from scipy import ndimage
import numpy as np
def laplacian(image):
laplacian_kernal = np.array([[0, 1, 0], [1, -4, 1], [0, 1, 0]])
filtered_img = ndimage.convolve(image, laplacian_kernal)
return filtered_img
```
Canny算子:
```python
from scipy import ndimage
import numpy as np
def canny(image, low_threshold=0, high_threshold=255):
img_smooth = ndimage.gaussian_filter(image, sigma=1.4)
img_x = ndimage.sobel(img_smooth, axis=0)
img_y = ndimage.sobel(img_smooth, axis=1)
gradient_magnitude = np.hypot(img_x, img_y)
gradient_direction = np.arctan2(img_y, img_x)
gradient_direction = np.rad2deg(gradient_direction)
gradient_direction[gradient_direction < 0] += 180
max_magnitude = np.max(gradient_magnitude)
if high_threshold > max_magnitude:
high_threshold = max_magnitude
if low_threshold > high_threshold:
low_threshold, high_threshold = high_threshold, low_threshold
row, col = image.shape
canny_img = np.zeros((row, col), dtype=np.uint8)
weak = np.int32(50)
strong = np.int32(255)
strong_i, strong_j = np.where(gradient_magnitude >= high_threshold)
zeros_i, zeros_j = np.where(gradient_magnitude < low_threshold)
weak_i, weak_j = np.where((gradient_magnitude <= high_threshold) & (gradient_magnitude >= low_threshold))
canny_img[strong_i, strong_j] = strong
canny_img[weak_i, weak_j] = weak
for i in range(1, row-1):
for j in range(1, col-1):
if canny_img[i,j] == weak:
if (canny_img[i+1, j-1] == strong) or (canny_img[i+1, j] == strong) or (canny_img[i+1, j+1] == strong) \
or (canny_img[i, j-1] == strong) or (canny_img[i, j+1] == strong) \
or (canny_img[i-1, j-1] == strong) or (canny_img[i-1, j] == strong) or (canny_img[i-1, j+1] == strong):
canny_img[i, j] = strong
else:
canny_img[i, j] = 0
return canny_img
```
阅读全文