Python imread在某些图像上返回None,但在同一文件夹中的其他图像上不返回None
我一直在尝试使用Python imread在某些图像上返回None,但在同一文件夹中的其他图像上不返回None,python,image,opencv,image-processing,imread,Python,Image,Opencv,Image Processing,Imread,我一直在尝试使用imread从cv2软件包读取jpeg图像。到目前为止,除了这种看似随机选择的图像阅读之外,它一直都很不错 问题是,由于我将来自同一来源的所有图像保存在同一文件夹中,显然,imread处理了多个图像,但没有明显的原因处理了其他图像。我已仔细检查并确保所有相同类型的图像工作正常等。但是,正如我在下面附上的屏幕截图中所示,只有选定的图像返回了合理的结果,而所有其他图像返回了无。我在这里使用的确切代码是: img = cv2.imread(image,1) #most None 谁能
imread
从cv2
软件包读取jpeg图像。到目前为止,除了这种看似随机选择的图像阅读之外,它一直都很不错
问题是,由于我将来自同一来源的所有图像保存在同一文件夹中,显然,imread
处理了多个图像,但没有明显的原因处理了其他图像。我已仔细检查并确保所有相同类型的图像工作正常等。但是,正如我在下面附上的屏幕截图中所示,只有选定的图像返回了合理的结果,而所有其他图像返回了无
。我在这里使用的确切代码是:
img = cv2.imread(image,1) #most None
谁能告诉我可能出了什么问题?非常感谢!
我的代码作为一个整体:
images = os.listdir(os.path.join(input_dir, folder))
os.chdir(os.path.join(input_dir, folder))
#index += 1
index_array = []
out = []
# a quick recursion of summing up nested lists
rec = lambda x: sum(map(rec, x)) if isinstance(x, list) else x
minLineLength = 20 ##? how to set these?
maxLineGap = 5 ##? how to set these?
for image in images:
print(image)
if image == ".DS_Store" or (not image.endswith(".jpeg")):
continue
else:
img = cv2.imread(image,1)
#cv2.cv.LoadImage(image,CV_LOAD_IMAGE_COLOR)## #Opening image
if img is None:
print("None")
continue
elif img is not None:
print("Pppppppppaaaaaaaaaaassssssssss!")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY);
edges = cv2.Canny(gray, 50, 120);
nedges = rec(edges.tolist())/255.0;
lines = cv2.HoughLinesP(edges, 1, np.pi/180, 100, minLineLength, maxLineGap)
if lines is None:
nlines = 0
elif lines is not None:
nlines = lines.shape[0]
img_blur = cv2.GaussianBlur(gray, (5, 5), 0)
cimg = cv2.cvtColor(img_blur, cv2.COLOR_GRAY2BGR)
circles = cv2.HoughCircles(img_blur, cv2.HOUGH_GRADIENT, 1, 120, param1=100, param2=30, minRadius=0, maxRadius=0)
if circles is None:
ncircles = 0
elif circles is not None:
circles = np.uint16(np.around(circles))
ncircles = circles.shape[1]
gray_blur = cv2.normalize(img_blur, img_blur, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8UC1)
ret, thresh = cv2.threshold(gray_blur, 230, 255, cv2.THRESH_BINARY_INV)
square_cnts = []
thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, np.ones((5, 5), np.uint8))
tmpimage, contours, h = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
#print("D")
cnt0 = contours[0]
tmp_h = h[::-1]
appr = []
npoly = []
for index, cnt in enumerate(contours[::-1]):
approx = cv2.approxPolyDP(cnt, 0.1*cv2.arcLength(cnt, True), True)
if approx is None:
continue
elif approx is not None:
appr.append(approx)
npoly.append(len(approx))
new = [nedges, nlines, ncircles, sum(npoly)*1.0/len(npoly)]
out.append(new) #Adding new image to array shape of (x, 3, 100, 100) where x is image number
index_array.append(image)
imread
如果image
是无效的文件名,则通常返回None
。请显示它是如何初始化的。@DyZ,谢谢!如何显示其初始化?我从你的程序中抓取了一些图像,我想你的程序中一定有一个循环。把它包括在你的代码中。也许你的抓取没有正确抓取图像。尝试使用一些图像查看器查看它们-GIMP,feh
,etc@MarkSetchell,谢谢,但我的照片用应用打开时看起来很好。。。