6

I encountered a very strange issue when using OpenCV's cv2.Canny() function in the python shell in Emacs. When I run cv2.Canny(img, thresh1, thresh2), I got error message

OpenCV Error: Assertion failed (depth == CV_8U) in cv::Canny, file D:\Build\OpenCV\opencv-3.2.0\modules\imgproc\src\canny.cpp, line 845 Traceback (most recent call last): File "", line 1, in File "../myscript.py", line 34, in lines = cv2.HoughLines(edges,1,np.pi/180,200) cv2.error: D:\Build\OpenCV\opencv-3.2.0\modules\imgproc\src\canny.cpp:845: error: (-215) depth == CV_8U in function cv::Canny

I don't even have a D drive on my computer, and why the source code has issue with this? it should already been compiled..

I haven't observe any other functions in python-opencv cause this issue. I also tried to install different versions, and same error for this particular function call, but the line number varies because the canny.cpp file is of different versions.

However, everything is fine, when I run the same line in System Python Shell...

my system environment:

windows7 64bit
python 3.5
python-opencv: 3.1, or 3.2
emacs 25.1.1
2
  • Your img needs to be casted as an uint8. CV_8U. Import numpy and add the following line after you load your image img= np.uint8(img). Use cv2.Canny after. Commented Feb 22, 2017 at 22:35
  • but why no error when i am using system python shell? Commented Feb 23, 2017 at 1:41

1 Answer 1

9

The issue is that your image (or one of your images) is not in 8 bit format (8 bit format means each of three channels is in [0,255]).

You can change this by adding:

img = img.astype(np.uint8)

before you call cv2.Canny.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.