#-*- coding:utf-8 -*- import sys reload(sys) sys.setdefaultencoding('utf-8') import cv2 import numpy as np class PatternChecker(): def __init__(self): print "Init Pattern Checker" def getGraySepLine(self, img): gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) edges = cv2.Canny(gray, 200, 300, apertureSize=5) lines = cv2.HoughLinesP(edges,1,np.pi/180,100,minLineLength=500,maxLineGap=10) destLines = [] for line in lines: #print line #LINE:[[startX,startY,endX,endY]] if (line[0][0] == line[0][2]): destLines.append(line) return destLines def calcuateSep(self, img): lines = self.getGraySepLine(img) sepXList = [] for line in lines: flag = False X = line[0][0] for sepX in sepXList: if(sepX.__len__() > 0) and (abs(sepX[0] - X) < 20): sepX.append(X) flag = True if flag == False: newSepX = [] newSepX.append(X) sepXList.append(newSepX) #print sepXList return sepXList """ 检测16灰阶图片是否能够分清楚 """ def check16GrayLevel(self, picPath): img_src = cv2.imread(picPath) height, width, type = img_src.shape print img_src.shape half_height = height/2 img_halfup = img_src[0:half_height, 0:width] XList = self.calcuateSep(img_halfup) if (XList.__len__() == 15): return "Pass" else: return "Fail" def getSingleSortList(self, xList): increList = [] for XItemList in xList: x_total = 0 for x in XItemList: x_total += x x_avg = x_total/XItemList.__len__() increList.append(x_avg) print "increList:",increList if increList.__len__() < 2: return increList for index in range(1, increList.__len__()): for sindex in range(index, increList.__len__()): if (increList[index - 1] > increList[sindex]): tmp = increList[index - 1] increList[index - 1] = increList[sindex] increList[sindex] = tmp print "sorted increList:", increList return increList def getColorSTDList(self, img, xList, y_base): increXList = self.getSingleSortList(xList) img_width = img.shape[1] totalXList = [0] + increXList + [img_width] colorList = [] for index in range(1, totalXList.__len__()): x_avg = totalXList[index - 1] + (totalXList[index] - totalXList[index - 1])/2 colorList.append(img[y_base, x_avg]) return colorList, totalXList """ 检测彩条图像是否有串色 """ def checkColorLevel(self, picPath, stdBide=50): img_src = cv2.imread(picPath) XList = self.calcuateSep(img_src) y_base = 500 stdColorList,totalXList = self.getColorSTDList(img_src, XList, y_base) BGR_list = [] diffBideList = [] for index in range(stdColorList.__len__() - 1): startX = -1 for x in range(totalXList[index] + (totalXList[index+1] - totalXList[index])/2, totalXList[index+1]): startX = x if (self.cmp2Point(img_src[y_base, x], stdColorList[index] )== False): break print "startX=", startX endX = -1 for i in range((totalXList[index + 2] - totalXList[index + 1])/2): x = totalXList[index + 2] - (totalXList[index + 2] - totalXList[index + 1])/2 - i endX = x if (self.cmp2Point(img_src[y_base,x], stdColorList[index+1]) ==False): break print "endX=", endX diffBideList.append(endX - startX) print diffBideList for bide in diffBideList: if (bide > stdBide): return "Fail" return "Pass" def cmp2Point(self, point1BGR, point2BGR): if (abs(point1BGR[0]&0xFF - point2BGR[0]&0xFF) > 10) \ or (abs(point1BGR[1]&0xFF - point2BGR[1]&0xFF) > 10) \ or (abs(point1BGR[2]&0xFF - point2BGR[2]&0xFF) > 10): return False else: return True if __name__ == "__main__": DIR = r"D:/TST/22293-src/" #picPath = DIR + "hdmi_std_t66_p47.jpg" patChecker = PatternChecker() #patChecker.check16GrayLevel(picPath) color_path = DIR + "color.jpg" print patChecker.checkColorLevel(color_path)