0

I am new to Python. With some recent research on Google I have found a program to remove noise from an image and how to write multiple images into a folder.

For a single image it is working and I am getting the correct output. But when I tried to do the same with an entire folder, it is showing an error saying

error: (-215:Assertion failed) L.channels() == 1 && I.channels() == 1 in function 'cv::connectedComponents_sub1'

 import matplotlib.pyplot as plt
 import cv2 
 import numpy as np
 from PIL import Image
 import PIL.ImageOps 
 import glob
 path = "C:/Users/user/Desktop/enhance/images/*.*"
 for bb,file in enumerate (glob.glob(path)):

    img = cv2.imread(file)
    ret, bw = cv2.threshold(img, 128,255,cv2.THRESH_BINARY_INV)
    connectivity = 4
    nb_components, output, stats, centroids = cv2.connectedComponentsWithStats(bw, connectivity, cv2.CV_32S)
    sizes = stats[1:, -1]; nb_components = nb_components - 1
    min_size = 50 #threshhold value for small noisy components
    img2 = np.zeros((output.shape), np.uint8)

    for i in range(0, nb_components):
        if sizes[i] >= min_size:
            img2[output == i + 1] = 255

    res = cv2.bitwise_not(img2)
 cv2.imwrite('C:/Users/user/Desktop/enhance/output_images/messigray{}.png'.format(bb), img2)

 image = Image.open('C:/Users/user/Desktop/enhance/output_images/messigray{}.png')

 inverted_image = PIL.ImageOps.invert(image)

 inverted_image.save('C:/Users/user/Desktop/enhance/output_images/out{}.png')
nathancy
  • 42,661
  • 14
  • 115
  • 137
Ajzz
  • 340
  • 1
  • 7
  • 22
  • Most likely one of the images produces this error. – Alderven Mar 25 '19 at 10:43
  • But when I do each image separately, it is working fine – Ajzz Mar 25 '19 at 10:45
  • At what moment error occures? – Alderven Mar 25 '19 at 10:47
  • nb_components, output, stats, centroids = cv2.connectedComponentsWithStats(bw, connectivity, cv2.CV_32S) cv2.error: OpenCV(4.0.0) C:\projects\opencv-python\opencv\modules\imgproc\src\connectedcomponents.cpp:3927: error: (-215:Assertion failed) L.channels() == 1 && I.channels() == 1 in function 'cv::connectedComponents_sub1' – Ajzz Mar 25 '19 at 10:49
  • This is the complete error line – Ajzz Mar 25 '19 at 10:49
  • Can you add line `print('{} {}'.format(bb, file))` in your loop and show me the output? – Alderven Mar 25 '19 at 10:52
  • You read a color 3-channels image and connectedComponentsWithStats waits a gray 1-channel – Nuzhny Mar 25 '19 at 10:53
  • @Alderven same error is showing – Ajzz Mar 25 '19 at 11:07
  • Can you show the print output? – Alderven Mar 25 '19 at 11:08
  • C:\Users\user\Desktop\enhance>python file.py 0 C:/Users/user/Desktop/enhance/images\11.tiff 1 C:/Users/user/Desktop/enhance/images\171385_Page_001 - Copy.tiff 2 C:/Users/user/Desktop/enhance/images\2.tif – Ajzz Mar 25 '19 at 11:13
  • So the error occurs on image `2.tif`. Remove it from your folder and rerun the script. – Alderven Mar 25 '19 at 11:30
  • nop. tried that too. same error is showing – Ajzz Mar 25 '19 at 11:37
  • 1
    Try to force every candidate to be gray scale, in order to normalize your data input. Do `gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)` before the threshold operation. Similar problem [here](https://stackoverflow.com/questions/41257336/opencv-error-assertion-failed-l-channels-1-i-channels-1-in-conne) – Guilherme Vogt Mar 25 '19 at 13:24
  • @GuilhermeVogt there it goes. Thanks much budyy. – Ajzz Mar 26 '19 at 11:23

0 Answers0