OpenCV2将图像转换为灰度并找到边缘但得到此错误

问题描述 投票:0回答:1

我收到错误,OpenCV2将图像转换为灰度并找到边缘但得到此错误

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()

错误:

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)
python image opencv
1个回答
1
投票

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

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()
© www.soinside.com 2019 - 2024. All rights reserved.