简体   繁体   English

使用OpenCV Python在2D图像中出现深度错误

[英]Depth error in 2D image with OpenCV Python

I am trying to compute the Canny Edges in an image (ndarray) using OpenCV with Python. 我正在尝试使用OpenCV和Python计算图像中的Canny Edges(ndarray)。

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? 有什么想法吗?

Slice1 will need to be casted or created as a uint8. Slice1需要被铸造或创建为uint8。 CV_8U is just an alias for the datatype uint8. CV_8U只是数据类型uint8的别名。

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

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 这不是最优雅的解决方案,但它解决了我的问题

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. 在这里,我正在考虑图像是一个numpy数组,np代表numpy。 I hope it can help someone! 我希望它可以帮助别人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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