简体   繁体   中英

OpenCV2 converting image into gray scale & finding edges but getting this error

i am getting error, OpenCV2 converting image into gray scale & finding edges but getting this error

import cv2
import numpy as np

img = cv2.imread(r"F:\Python_Folder\lena.jpg")
# img = cv2.imread(r"F:\Python_Folder\lena.jpg",0) also tried this
grey_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

laplacian = cv2.Laplacian(grey_img, cv2.CV_64F)
cv2.imshow("Laplacian image",laplacian)
cv2.waitKey(0)

cv2.destroyAllWindows()

error:

Traceback (most recent call last):
  File "F:/Python_Folder/new.py", line 7, in <module>
    cv2.imshow("Laplacian image",laplacian)
cv2.error: OpenCV(4.0.0) c:\projects\opencv-python\opencv\modules\imgproc\src\color.hpp:261: error: (-2:Unspecified error) in function '__thiscall cv::CvtHelper<struct cv::Set<1,-1,-1>,struct cv::Set<3,4,-1>,struct cv::Set<0,2,5>,2>::CvtHelper(const class cv::_InputArray &,const class cv::_OutputArray &,int)'
> Unsupported depth of input image:
>     'VDepth::contains(depth)'
> where
>     'depth' is 6 (CV_64F)

You have to convert your image back to uint8. Check following edited code snippet:

import cv2
import numpy as np

img = cv2.imread("F:\Python_Folder\lena.jpg")
# img = cv2.imread(r"F:\Python_Folder\lena.jpg",0) also tried this
grey_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

laplacian = cv2.Laplacian(grey_img, cv2.CV_64F)

# convert back to uint8 
laplacian = cv2.convertScaleAbs(laplacian)

cv2.imshow("Laplacian image",laplacian)
cv2.waitKey(0)

cv2.destroyAllWindows()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM