Failure to use adaptiveThreshold: CV_8UC1 in function adaptiveThreshold

前端 未结 6 961
执笔经年
执笔经年 2020-12-15 03:29

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,          


        
相关标签:
6条回答
  • 2020-12-15 03:39

    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)
    
    0 讨论(0)
  • 2020-12-15 03:41

    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)

    0 讨论(0)
  • 2020-12-15 03:45

    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.

    0 讨论(0)
  • 2020-12-15 03:46

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

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

    0 讨论(0)
  • 2020-12-15 03:55

    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);
    
    0 讨论(0)
  • 2020-12-15 03:57

    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

    0 讨论(0)
提交回复
热议问题