47

I have used openCV python and encountered an error.

img_blur = cv2.medianBlur(self.cropped_img,5)
img_thresh_Gaussian = cv2.adaptiveThreshold(img_blur, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)

plt.subplot(1,1,1),plt.imshow(img_thresh_Gaussian, cmap = 'gray')
plt.title("Image"), plt.xticks([]), plt.yticks([])
plt.show()

but I received:

cv2.error: /home/phuong/opencv_src/opencv/modules/imgproc/src/thresh.cpp:1280: error: (-215) src.type() == CV_8UC1 in function adaptiveThreshold

Do I have to install something else?

Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
PhuongHoang
  • 481
  • 1
  • 4
  • 5
  • 1
    the input must be 8bit, single channel. you probably forgot to convert to grayscale even before applying medianblur. – berak Nov 19 '14 at 10:18
  • CV_8UC1 is the source type whereas your `img_blur` type must be something different (i.e. need to be changed). The target needs to be either the same type i.e. CV8UC1 or bigger like CV16UC1 to cover for possible precision loss. I had similar issues in C++ API. – ha9u63a7 Nov 19 '14 at 10:18

7 Answers7

149

The problem is that you are trying to use adaptive thresholding to an image that is not in greyscale. And the function only works with a greyscale images.

So you have to convert your image to a greyscale format as it is described in documentation.

They read the image in a greyscale format with: img = cv2.imread('dave.jpg',0). You can also convert it to greyscale with: img_grey = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
  • 8
    Excellent. I would take a good explanation over code correct any time... – Selam Getachew Jul 21 '17 at 23:19
  • 7
    I'd suggest using the named constant `cv2.IMREAD_GRAYSCALE` instead of the literal `0` as the second argument to `imread`. They're equal, but using the constant is significantly more self-documenting. – Mark Amery May 10 '18 at 12:14
16

you should load your file like this

src.create(rows, cols, CV_8UC1);
src = imread(your-file, CV_8UC1);

and after that

adaptiveThreshold(src, dst, 255, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY, 75, 10);
David Schuler
  • 2,375
  • 1
  • 14
  • 6
sam
  • 1,363
  • 1
  • 20
  • 32
  • 9
    what is ``src`` ``type`` – muon Mar 11 '17 at 22:48
  • 14
    -1; this only works by coincidence. The [`imread` docs](https://docs.opencv.org/master/d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56) dictate that `flags` passed to `imread` should be [`ImreadModes`](https://docs.opencv.org/master/d4/da8/group__imgcodecs.html#ga61d9b0126a3e57d9277ac48327799c80) - the constants that start with `IMREAD_`. The flag you pass, `CV_8UC1`, is not such a constant, and it's not particularly meaningful to use it as an `imread()` flag - but it happens to work because `CV_8UC1` and `IMREAD_GRAYSCALE` are both defined as `0`. – Mark Amery May 10 '18 at 12:13
13

you can change the code to slightly like this :

img_blur = cv2.medianBlur(self.cropped_img,5).astype('uint8')
img_thresh_Gaussian = cv2.adaptiveThreshold(img_blur, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)

just by adding ('uint8') while blurring has resolved my issue.

Vaibhav K
  • 2,762
  • 3
  • 21
  • 22
6

you have to imread your image in grayscale, so use the code below:

image = imread('address', 0)

0 exchange the channel of image to grayscale

Adi Prasetyo
  • 1,006
  • 1
  • 15
  • 41
Aram.rt
  • 61
  • 1
  • 3
2

Try to add cv2.IMREAD_GRAYSCALE in the second parameter of imread

img = cv2.imread(File_path, cv2.IMREAD_GRAYSCALE)

Yazen WAKED
  • 133
  • 1
  • 5
2

Convert the image to grayscale before thresholding:

img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_blur = cv2.medianBlur(img_gray,5)
img_thresh_Gaussian = cv2.adaptiveThreshold(img_blur, 255,
        cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
Rostam
  • 359
  • 1
  • 5
1

To solve this issue, you need to load grayscale image using 0(zero) as another parameter than the image path.
img = cv2.imread(imagepath, 0)
OR
img = cv2.imread(imagepath, cv2.IMREAD_GRAYSCALE)

Vilas
  • 51
  • 4
  • I tried your solution and ones given by others on this thread. Nothing seems to be working. I am getting error on cv2.HoughLines(cdst, 1, np.pi / 180, 150, None, 0, 0) call with same error messages. – NewGuyInJava Jan 20 '22 at 16:23
  • Can you put your code and error here? @NewGuyInJava – Vilas Jan 29 '23 at 08:39