gpt4 book ai didi

python - OpenCV2将图像转换为灰度并找到边缘但出现此错误

转载 作者:行者123 更新时间:2023-12-02 16:20:56 25 4
gpt4 key购买 nike

我遇到错误,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)

最佳答案

您必须将图像转换回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()

关于python - OpenCV2将图像转换为灰度并找到边缘但出现此错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55115466/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com