1

I am doing image segmentation on an image which is fine, but what I am trying to do is apply image segmentation using canny edge detection on an image after applying the union of Laplacian and Sobel filter. Yes, I have done the normalization of values and converted the image into grayscale. I am not able to do edge detection in the final image or sob. following error

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

Machine
  • 103
  • 7
  • See here: https://stackoverflow.com/questions/19103933/depth-error-in-2d-image-with-opencv-python – Alex Sveshnikov Mar 12 '20 at 06:49
  • [The Laplacian is not an edge detector.](https://stackoverflow.com/questions/51411156/is-laplacian-of-gaussian-for-blob-detection-or-for-edge-detection/51414532#51414532) – Cris Luengo Mar 12 '20 at 15:05

2 Answers2

2

The image passed to Canny must be uint8, but your sob, laplacian and final are float64, in the range 0-1.

You can multiply by 255 then convert to uint8:

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

or:

canny1 = cv.Canny(cv.convertScaleAbs(sob * 255) ,100, 200)
Gustavo Kaneto
  • 643
  • 7
  • 17
0

The error code says that you should first convert your image to CV_8U depth format. And sob is in CV_64F depth format. So this should work:

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