我一直在尝试使用imread
包中的cv2
来阅读jpeg图片。到目前为止,除了这种看似随机选择的图像之外,它还是很棒的。
问题在于,由于我将来自同一来源的所有图像都保存在同一个文件夹中,显然imread
处理了多个图像而不是其他图像,没有明显的原因。我仔细检查并确保所有图像都工作正常,类型相同等。但是,如下面附带的屏幕截图所示,只有选定的图像返回了合理的结果,而其他所有图像都返回了None
。我在这里使用的确切代码是:
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)