2

I am trying opencv with python now. What mean this error?

OpenCV Error: Assertion failed (m.dims >= 2) in Mat, file /build/opencv-ISmtkH/opencv-2.4.9.1+dfsg/modules/core/src/matrix.cpp, line 269 
Traceback (most recent call last): 
File "sabun5.py", line 16, in <module> 
img_m = cv2.threshold(img_df, 50, 255, cv2.THRESH_BINARY)[1] 
cv2.error: /build/opencv-ISmtkH/opencv-2.4.9.1+dfsg/modules/core/src/matrix.cpp:269: error: (-215) m.dims >= 2 in function Mat
2
  • it looks like it needs some 2-dimensional element but it gets 1-dimensional element. Commented Dec 26, 2016 at 8:22
  • I guess that your img_df is not loaded properly, maybe the path is not correct, so before calling cv2.threshold(), call print img_df.shape to check if the image is loaded properly. Commented Dec 26, 2016 at 9:28

2 Answers 2

2

You can see in the OpenCV documentation, that the threshold function just allow single-channel images.

If your image is a color one, it won't work. If it's grayscale but you are loading it with imread, it might be possible that OpenCV load it as a 3-channel one. You can add the flag to load it as a single-channel with CV_8UC1 (supposing it is an 8 bit unsigned one, which is the more common for a grayscale image). For example: img_df = cv2.imread("image/path", cv2.CV_8UC1)

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

Comments

0

you have to convert the image into grayscale before thresholding your image has more then two dimentions i.e (height,width,color-channel) gray scale image has only two dimention(height,width) it might help

    import cv2
    img = cv.imread('x.png',0)
    # where 0 converts the image in grayscale or gray = cv2.cvtColor(img,    cv2.COLOR_BGR2GRAY)

    img_m = cv2.threshold(img, 50, 255, cv2.THRESH_BINARY)[1] 

    cv2.waitKey(0)

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.