user7341567
user7341567

Reputation: 49

Opencv Pythonprogram

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

Upvotes: 2

Views: 1105

Answers (2)

saugat timsina
saugat timsina

Reputation: 21

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)

Upvotes: 0

facug91
facug91

Reputation: 93

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)

Upvotes: 2

Related Questions