我是Python的新手。通过对Google的一些最新研究,我发现了一个remove noise from an image程序以及如何write multiple images into a folder的程序。
对于单个图像,它正在工作,并且我得到正确的输出。但是,当我尝试对整个文件夹执行相同操作时,它显示一条错误消息
错误:(-215:声明失败)函数'cv :: connectedComponents_sub1'中的L.channels()== 1 && I.channels()== 1
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')