【问题标题】:edge detection on image after applying sobel and laplacian filter in opencv在opencv中应用索贝尔和拉普拉斯滤波器后的图像边缘检测
【发布时间】:2020-03-12 06:45:14
【问题描述】:

我正在对图像进行图像分割,这很好,但我正在尝试做的是在应用拉普拉斯和索贝尔滤波器的并集后使用精明边缘检测对图像进行图像分割。是的,我已经完成了值的归一化并将图像转换为灰度。我无法在最终图像或呜咽中进行边缘检测。 跟随错误

错误:OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\imgproc\src\canny.cpp:829: 错误:(-215:断言失败)_src.depth()==函数中的CV_8U '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')

【问题讨论】:

标签: python opencv image-processing


【解决方案1】:

传递给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)

【讨论】:

    【解决方案2】:

    错误代码表明您应该首先将图像转换为CV_8U 深度格式。而sobCV_64F 深度格式。所以这应该有效:

    sob = np.uint8(sob*255)
    canny1=cv.Canny(sob,100,200)    #after that you can call Canny
    

    【讨论】:

      猜你喜欢
      • 2019-02-02
      • 1970-01-01
      • 1970-01-01
      • 2011-04-28
      • 1970-01-01
      • 2023-03-24
      • 2021-02-26
      • 2012-04-25
      相关资源
      最近更新 更多