chroma22293_pattern.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #-*- coding:utf-8 -*-
  2. import sys
  3. reload(sys)
  4. sys.setdefaultencoding('utf-8')
  5. import cv2
  6. import numpy as np
  7. class PatternChecker():
  8. def __init__(self):
  9. print "Init Pattern Checker"
  10. def getGraySepLine(self, img):
  11. gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  12. edges = cv2.Canny(gray, 200, 300, apertureSize=5)
  13. lines = cv2.HoughLinesP(edges,1,np.pi/180,100,minLineLength=500,maxLineGap=10)
  14. destLines = []
  15. for line in lines:
  16. #print line #LINE:[[startX,startY,endX,endY]]
  17. if (line[0][0] == line[0][2]):
  18. destLines.append(line)
  19. return destLines
  20. def calcuateSep(self, img):
  21. lines = self.getGraySepLine(img)
  22. sepXList = []
  23. for line in lines:
  24. flag = False
  25. X = line[0][0]
  26. for sepX in sepXList:
  27. if(sepX.__len__() > 0) and (abs(sepX[0] - X) < 20):
  28. sepX.append(X)
  29. flag = True
  30. if flag == False:
  31. newSepX = []
  32. newSepX.append(X)
  33. sepXList.append(newSepX)
  34. #print sepXList
  35. return sepXList
  36. """
  37. 检测16灰阶图片是否能够分清楚
  38. """
  39. def check16GrayLevel(self, picPath):
  40. img_src = cv2.imread(picPath)
  41. height, width, type = img_src.shape
  42. print img_src.shape
  43. half_height = height/2
  44. img_halfup = img_src[0:half_height, 0:width]
  45. XList = self.calcuateSep(img_halfup)
  46. if (XList.__len__() == 15):
  47. return "Pass"
  48. else:
  49. return "Fail"
  50. def getSingleSortList(self, xList):
  51. increList = []
  52. for XItemList in xList:
  53. x_total = 0
  54. for x in XItemList:
  55. x_total += x
  56. x_avg = x_total/XItemList.__len__()
  57. increList.append(x_avg)
  58. print "increList:",increList
  59. if increList.__len__() < 2:
  60. return increList
  61. for index in range(1, increList.__len__()):
  62. for sindex in range(index, increList.__len__()):
  63. if (increList[index - 1] > increList[sindex]):
  64. tmp = increList[index - 1]
  65. increList[index - 1] = increList[sindex]
  66. increList[sindex] = tmp
  67. print "sorted increList:", increList
  68. return increList
  69. def getColorSTDList(self, img, xList, y_base):
  70. increXList = self.getSingleSortList(xList)
  71. img_width = img.shape[1]
  72. totalXList = [0] + increXList + [img_width]
  73. colorList = []
  74. for index in range(1, totalXList.__len__()):
  75. x_avg = totalXList[index - 1] + (totalXList[index] - totalXList[index - 1])/2
  76. colorList.append(img[y_base, x_avg])
  77. return colorList, totalXList
  78. """
  79. 检测彩条图像是否有串色
  80. """
  81. def checkColorLevel(self, picPath, stdBide=50):
  82. img_src = cv2.imread(picPath)
  83. XList = self.calcuateSep(img_src)
  84. y_base = 500
  85. stdColorList,totalXList = self.getColorSTDList(img_src, XList, y_base)
  86. BGR_list = []
  87. diffBideList = []
  88. for index in range(stdColorList.__len__() - 1):
  89. startX = -1
  90. for x in range(totalXList[index] + (totalXList[index+1] - totalXList[index])/2, totalXList[index+1]):
  91. startX = x
  92. if (self.cmp2Point(img_src[y_base, x], stdColorList[index] )== False):
  93. break
  94. print "startX=", startX
  95. endX = -1
  96. for i in range((totalXList[index + 2] - totalXList[index + 1])/2):
  97. x = totalXList[index + 2] - (totalXList[index + 2] - totalXList[index + 1])/2 - i
  98. endX = x
  99. if (self.cmp2Point(img_src[y_base,x], stdColorList[index+1]) ==False):
  100. break
  101. print "endX=", endX
  102. diffBideList.append(endX - startX)
  103. print diffBideList
  104. for bide in diffBideList:
  105. if (bide > stdBide):
  106. return "Fail"
  107. return "Pass"
  108. def cmp2Point(self, point1BGR, point2BGR):
  109. if (abs(point1BGR[0]&0xFF - point2BGR[0]&0xFF) > 10) \
  110. or (abs(point1BGR[1]&0xFF - point2BGR[1]&0xFF) > 10) \
  111. or (abs(point1BGR[2]&0xFF - point2BGR[2]&0xFF) > 10):
  112. return False
  113. else:
  114. return True
  115. if __name__ == "__main__":
  116. DIR = r"D:/TST/22293-src/"
  117. #picPath = DIR + "hdmi_std_t66_p47.jpg"
  118. patChecker = PatternChecker()
  119. #patChecker.check16GrayLevel(picPath)
  120. color_path = DIR + "color.jpg"
  121. print patChecker.checkColorLevel(color_path)