python - 在opencv中应用sobel和laplacian滤波器后图像的边缘检测

标签 python opencv image-processing

我正在对图像进行图像分割,但是我想做的是在应用Laplacian和Sobel滤镜的并集之后,使用Canny边缘检测对图像进行图像分割。是的,我已经完成了值的归一化并将图像转换为灰度。我无法在最终图像或sob中进行边缘检测。
以下错误

error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\imgproc\src\canny.cpp:829: error: (-215:Assertion failed) _src.depth() == CV_8U in function 'cv::Canny'


import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt

path=r"C:\Users\MACHINE\Desktop\3.jpg"

img=cv.imread(path)
img=cv.cvtColor(img,cv.COLOR_RGB2GRAY)
laplacian=cv.Laplacian(img,cv.CV_64F)
laplacian=(laplacian-laplacian.min())/(laplacian.max()-laplacian.min())
sobelx = cv.Sobel(img,cv.CV_64F,1,0,ksize=5)
sobely = cv.Sobel(img,cv.CV_64F,0,1,ksize=5)
sob=(sobelx+sobely) 
sob=(sob-sob.min())/(sob.max()-sob.min()) # taking care of negative values and values out of range
final=sob+laplacian
final=(final-final.min())/(final.max()-final.min()) 
print(sob.shape)
#canny1=cv.Canny(sob,100,200) #thise code is showing error on sob .but works perfectly fine on orginal image



plt.subplot(2,2,1)    
plt.imshow(canny1,cmap='gray')
plt.subplot(2,2,2)    
plt.imshow(sob,cmap='gray')
plt.subplot(2,2,3)    
plt.imshow(final,cmap='gray')

enter image description here

最佳答案

传递给Canny的图像必须是uint8,但是您的soblaplacianfinalfloat64,范围为0-1。

您可以乘以255,然后转换为uint8:

canny1 = cv.Canny(np.uint8(sob * 255) ,100, 200)

要么:
canny1 = cv.Canny(cv.convertScaleAbs(sob * 255) ,100, 200)

关于python - 在opencv中应用sobel和laplacian滤波器后图像的边缘检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60648864/

相关文章:

Python 3.5 "ImportError: cannot import name ' 某个名字'

python - 通过请求模块调用 View 不会创建正确的 session 数据

python - 在opencv中将HSV转换为RGB

python - 图像识别——寻找相似图像

python - 从 numpy 数组到 QImage/QPixmap 的转换会产生彩色条纹

python - 在新型类中替换 __str__

python - 如何使用 opencv 从 2D 获取 3D 位置

python - 使用python计算灰度图像中掩码的面积(以像素为单位)

python - 图像处理方法

python - Jinja python for 循环语法