하나의 하위 폴더 "input"(10 번째 줄)에서 이미지 파일을 추출하여 처리 한 다음 다른 하위 폴더에 저장하려고합니다. "output "(마지막 행의 10 번째 줄) 처리 된 파일은 저장되지 않습니다. 그러나 코드가 저장된 동일한 폴더에서 모든 이미지 파일을 가져 오면 (10 행의 추출 명령에 입력하지 않음) 하위 폴더 "출력"에 저장하면 성공합니다. 메모를 기억하십시오. 두 경우 모두 처리 된 파일이 표시되고 동일하며 한 경우에만 저장되지 않습니다.하나의 폴더에서 이미지를 추출하여 파이썬으로 다른 폴더에 저장합니다. opencv
import glob
import cv2
import numpy as np
import matplotlib.pyplot as plt
# initialize data and target as lists
data = []
target = []
# find all bmp files in the current folder
Files = glob.glob("input/*.bmp")
# go through all files and fit contour
for f in Files:
img = 255-cv2.imread(f,0) #0 for grayscale
white=cv2.imread("white.bmp")
# add image to the list
data.append(img)
# make lines thicker
kernel = np.ones((5,5),np.uint8)
img = cv2.dilate(img,kernel,iterations = 1)
# contour application
ret,thresh = cv2.threshold(img,127,255,0)
im2,contours,hierarchy = cv2.findContours(thresh, 1, 2)
# sort contours by area
areas = [cv2.contourArea(cnt) for cnt in contours]
idx = np.argsort(areas)[::-1]
contours = np.asarray(contours)[idx]
for cnt in contours:
(x,y),radius = cv2.minEnclosingCircle(cnt)
if radius < img.shape[0]-10 and radius > 20:
cv2.circle(white,(int(x),int(y)),int(radius),0,4)
break
plt.imshow(white)
#save the files to output folder with the name "Image_x"
filename = "output/Image_%s" %f
plt.colorbar()
# live image display
plt.draw()
# need to add pause command, otherwise it does not work
plt.pause(.01)
# clear the figure to avoid memory issues
plt.clf()
#save these contours (outputs) as bmps with same file names
cv2.imwrite(filename,white)
출력 파일 이름을 인쇄 해 보았습니까? 실제로 파일을 쓰려는 곳이 보이지 않으십니까? "output/Image_input/foo.bmp"와 같이 보이고, 존재하지 않는 디렉토리에 글쓰기를 시도하는 데 실패했다면 놀라지 않을 것입니다. (예를 들어,'imwrite '). –