#!/usr/bin/python import os, sys, time, cv2 import numpy as np import matplotlib.pyplot as plt import config #查找并画出图片灰色直方图 def find_gray(img): #灰度化 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) gray = gray[2800:, :] #计算直方图 hist = cv2.calcHist([gray], [0], None, [256], [0, 256]) #画出直方图 plt.plot(hist) #查找颜色区域比例 def find_color_area(img, lower_color, upper_color): color = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) color = color[2800:, :] mask = cv2.inRange(color, lower_color, upper_color) area = cv2.countNonZero(mask) return round(area / (color.shape[0] * color.shape[1]), 4) if __name__ == '__main__': if len(sys.argv) == 3: path = sys.argv[1] psid = sys.argv[2] else: print('Usage: python findgray.py path psid') exit(1) path = os.path.abspath(path) ps_lights = config.ps_lights[psid] gcount, tcount = 0, 0 filelist = os.listdir(path) filelist.sort() for file in filelist: if file != '111125_8.jpg': img = cv2.imread(os.path.join(path, file)) gray_area = find_color_area(img.copy(), ps_lights['lower_gray'], ps_lights['upper_gray']) white_area = find_color_area(img.copy(), ps_lights['lower_white'], ps_lights['upper_white']) if white_area < gray_area or gray_area > ps_lights['gray_threshold']: print(' warning file:', file, 'gray_area:', gray_area, 'white_area:', white_area) else: print('file:', file, 'gray_area:', gray_area, 'white_area:', white_area) tcount += 1 find_gray(img) plt.title(path) plt.show() exit(0) print('start line--------------------------------------------------------------------------------') for file in filelist: if not file.endswith('.jpg'): continue try: img = cv2.imread(path + '/' + file) gray_area = find_color_area(img, config.lower_gray, config.upper_gray) white_area = find_color_area(img, config.lower_white, config.upper_white) if file.endswith('_1.jpg'): if gray_area > config.gray_threshold: print('file:', file, 'gray_area:', gray_area, 'white_area:', white_area) gcount += 1 else: print('warning file:', file, 'gray_area:', gray_area, 'white_area:', white_area) if file.endswith('_8.jpg'): if white_area > config.white_threshold: print('file:', file, 'gray_area:', gray_area, 'white_area:', white_area) tcount += 1 else: print('warning file:', file, 'gray_area:', gray_area, 'white_area:', white_area) except Exception as e: print(e) continue print('end line--------------------------------------------------------------------------------') print('优质建模组照片:', gcount, '/108 ', ' 可用贴图组照片:', tcount, '/108')