OpenCV2将图像转换为灰度并查找边缘,但出现此错误



我遇到了错误,openCv2将图像转换为灰度&查找边缘,但得到此错误

import cv2
import numpy as np
img = cv2.imread(r"F:Python_Folderlena.jpg")
# img = cv2.imread(r"F:Python_Folderlena.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()

错误:

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:projectsopencv-pythonopencvmodulesimgprocsrccolor.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)

您必须将图像转换回UINT8。检查以下编辑代码段:

import cv2
import numpy as np
img = cv2.imread("F:Python_Folderlena.jpg")
# img = cv2.imread(r"F:Python_Folderlena.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()

相关内容

最新更新