OpenCV—python簡単な画像品質検査
10215 ワード
これを書くのは主にOCRの認識率を高めるためで、画像の品質の極めて悪い2値図をスクリーニングして識別しないことを返して、品質の少し高い2値図だけを識別して、階調図とRGBのピクチャー.統計は以下の通りです:
' ', ' ', ' ',' / '
のこのいくつかの参考要素、もし
が1 /
より0.01より小さいならば、品質の極めて悪いピクチャーです.import cv2,os,csv
import numpy as np
def get_img(Img_path):
image_paths = []
for (dir, dirnames, filenames) in os.walk(Img_path):
for img_file in filenames:
ext = ['.jpg','.png','.jpeg','.tif']
if img_file.endswith(tuple(ext)):
image_paths.append(dir+'/'+img_file)
return image_paths
def Calculate_QD(img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
numdiff = np.diff(gray)
var_all = np.sum((numdiff) ** 2) / gray.size
baseline = np.bincount(gray.reshape(1,-1)[0])
sortNum = np.argsort(baseline)
var_255 = np.sum((gray - sortNum[-1]) ** 2) / gray.size
var_000 = np.sum((gray - sortNum[-2]) ** 2) / gray.size
return var_255,var_000,var_all
if __name__ == '__main__':
Img_path = './idcard'
csv_path = './idcard.csv'
Imgpaths = get_img(Img_path)
Result_CSV = []
for Img_path in Imgpaths:
filename = Img_path.split('/')[-1]
imgArray = cv2.imread(Img_path)
image_size = os.path.getsize(Img_path)
px_size = round((image_size/imgArray.size ),4)
var_255, var_000, var_all = Calculate_QD(imgArray)
Result_CSV.append((filename,var_255, var_000, var_all,px_size))
with open(csv_path, 'w', newline='') as csv_file:
csv_writer = csv.writer(csv_file)
csv_writer.writerow(('filename',' ', ' ', ' ',' / '))
csv_writer.writerows(Result_CSV)