使用 OpenCV Python 的 2D 图像中的深度错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19103933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 12:53:00  来源:igfitidea点击:

Depth error in 2D image with OpenCV Python

pythonopencv

提问by jmerkow

I am trying to compute the Canny Edges in an image (ndarray) using OpenCV with Python.

我正在尝试使用 OpenCV 和 Python 计算图像(ndarray)中的 Canny Edge。

slice1 = slices[15,:,:]
slice1 = slice1[40:80,60:100]
print slice1.shape
print slice1.dtype
slicecanny = cv2.Canny(slice1, 1, 100)

Output:

输出:

(40, 40)
float64
...
error: /Users/jmerkow/code/opencv-2.4.6.1/modules/imgproc/src/canny.cpp:49: 
error: (-215) src.depth() == CV_8U in function Canny

For some reason I get the above error. Any ideas why?

出于某种原因,我收到了上述错误。任何想法为什么?

回答by rifkinni

You can work around this error by saving slice1 to a file and then reading it

您可以通过将 slice1 保存到文件然后读取它来解决此错误

from scipy import ndimage, misc
misc.imsave('fileName.jpg', slice1)
image = ndimage.imread('fileName.jpg',0)
slicecanny = cv2.Canny(image,1,100)

This is not the most elegant solution, but it solved the problem for me

这不是最优雅的解决方案,但它为我解决了问题

回答by Ross

Slice1 will need to be casted or created as a uint8. CV_8U is just an alias for the datatype uint8.

Slice1 需要转换或创建为 uint8。CV_8U 只是数据类型 uint8 的别名。

import numpy as np
slice1Copy = np.uint8(slice1)
slicecanny = cv2.Canny(slice1Copy,1,100)

回答by Toda

In order to avoid losing precision while changing the data type to uint8, you can first adapt the scale to the 255 format just doing:

为了避免在将数据类型更改为 uint8 时丢失精度,您可以先将比例调整为 255 格式,只需执行以下操作:

(image*255).astype(np.uint8)

Here I'm considering that image is a numpy array and np stand for numpy. I hope it can help someone!

在这里,我认为该图像是一个 numpy 数组,而 np 代表 numpy。我希望它可以帮助某人!