RGB.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. # encoding: utf-8
  2. import cv2 as cv
  3. import numpy as np
  4. import math
  5. #裁剪图片
  6. def cutImage(img_addr):
  7. img = cv.imread(img_addr)
  8. img_arr = np.array(img)
  9. hight = img_arr.shape[0]
  10. wight = img_arr.shape[1]
  11. dest_img = img_arr[hight/2+20:hight,wight/2:wight,:]
  12. return dest_img
  13. def cmpPicWithRGB(picPath1,picPath2):
  14. std_img = cutImage(picPath1)
  15. test_img = cutImage(picPath2)
  16. width = std_img.shape[0]
  17. hight = test_img.shape[1]
  18. std_B, std_G, std_R = cv.split(std_img)
  19. test_B, test_G, test_R = cv.split(std_img)
  20. sum_std_B = 0
  21. sum_std_G = 0
  22. sum_std_R = 0
  23. for x in range(0,width,1):
  24. for y in range(0,hight,1):
  25. sum_std_B = sum_std_B + std_B[x][y]
  26. sum_std_G = sum_std_G + std_G[x][y]
  27. sum_std_R = sum_std_R + std_R[x][y]
  28. avg_std_B = sum_std_B / (width*hight)
  29. avg_std_G = sum_std_G / (width*hight)
  30. avg_std_R = sum_std_R / (width*hight)
  31. for x in range(0,width,1):
  32. for y in range(0,hight,1):
  33. if abs(test_B[x][y] - avg_std_B) >10 or abs(test_G[x][y] - avg_std_G) > 10 or abs(test_R[x][y] - avg_std_R[x][y] > 10):
  34. print test_B[x][y]
  35. print test_G[x][y]
  36. print test_R[x][y]
  37. print avg_std_B
  38. print avg_std_R
  39. print avg_std_G
  40. return False
  41. else:
  42. return True
  43. class RGBColor():
  44. def __init__(self):
  45. pass
  46. '''
  47. BGR颜色差值小于offset值时,定性为灰阶色调。
  48. '''
  49. def isGray(self, bgr, offset = 10):
  50. # print "RGB isGray:", bgr,abs(bgr[0] - bgr[1])
  51. if abs(bgr[0] - bgr[1]) < offset \
  52. and abs(bgr[1] - bgr[2]) < offset\
  53. and abs(bgr[2] - bgr[0]) < offset:
  54. return True
  55. else:
  56. return False
  57. '''
  58. 传入的图片对象,必须是BGR颜色空间。
  59. 根据BGR颜色值,判断是否为纯色图片。同时忽略极少的噪点干扰。
  60. :param img:传入的图片对象, BGR颜色空间
  61. :param expTh: 颜色平均值偏差范围,> expTh 则是False。
  62. :return: [判断结果True/False,颜色值]
  63. '''
  64. def isSingleColor(self, img, expTh=0.01, splitCount=10):
  65. height, width, color = img.shape
  66. heightStep = height/splitCount
  67. widthStep = width/splitCount
  68. totalAvg = self.getAvgBGR(img)
  69. expCount = 0.0
  70. for heightCount in range(0,splitCount):
  71. for widthCount in range(0, splitCount):
  72. height0 = heightStep*heightCount
  73. width0 = widthStep*widthCount
  74. height1 = height0+heightStep
  75. width1 = width0+widthStep
  76. scaleAvg = self.getAvgBGR(img[height0:height1, width0:width1])
  77. ret = self.isColorSimilar(totalAvg, scaleAvg, faultTolerance=40)
  78. if ret is False:
  79. expCount += 1
  80. expRate = expCount/(splitCount*splitCount)
  81. print "isSingleColor,expRate:",expRate,expCount
  82. if expRate > expTh:
  83. return False, totalAvg
  84. else:
  85. return True, totalAvg
  86. def getAvgBGR(self, img):
  87. imgB = img[:,:,0]
  88. imgG = img[:,:,1]
  89. imgR = img[:,:,2]
  90. avgB = np.average(imgB)
  91. avgG = np.average(imgG)
  92. avgR = np.average(imgR)
  93. return avgB,avgG,avgR
  94. def getMaxBGR(self, img):
  95. imgB = img[:, :, 0]
  96. imgG = img[:, :, 1]
  97. imgR = img[:, :, 2]
  98. maxB = np.max(imgB)
  99. maxG = np.max(imgG)
  100. maxR = np.max(imgR)
  101. return maxB, maxG, maxR
  102. '''
  103. 判断一张图片是否是纯黑图片。
  104. :param img:需要判断的图片
  105. :param grayTH : 黑色判断的灰阶阈值
  106. :param splitCount: 图片拆分的行列次数
  107. :param expTh:黑色计算的容错区域比例。> expTh 则是False。
  108. '''
  109. def isBlack(self, img, grayTH = 70,splitCount=10, expTh = 0.01):
  110. height, width, color = img.shape
  111. heightStep = height / splitCount
  112. widthStep = width / splitCount
  113. totalAvg = self.getAvgBGR(img)
  114. expCount = 0.0
  115. for heightCount in range(0, splitCount):
  116. for widthCount in range(0, splitCount):
  117. height0 = heightStep * heightCount
  118. width0 = widthStep * widthCount
  119. height1 = height0 + heightStep
  120. width1 = width0 + widthStep
  121. scaleAvg = self.getAvgBGR(img[height0:height1, width0:width1])
  122. ret = self.isGray(scaleAvg)
  123. #print "rgb isBlack,gray ret:", ret
  124. if ret is False or scaleAvg[1] > grayTH:
  125. expCount += 1
  126. expRate = expCount / (splitCount * splitCount)
  127. print "isBlack,expRate:", expRate, expCount
  128. if expRate > expTh:
  129. return False
  130. else:
  131. return True
  132. '''
  133. # 描述:bgr色差计算。ΔE=( ΔL^2 + ΔA^2 + ΔB^2 ) ^ (1/2) ΔE
  134. # 参数:color1、color2: bgr格式
  135. # 返回值:
  136. # 相差值,越小越相近。
  137. # 0~50(微小色差),感觉极微;
  138. # 50~150(小色差),感觉轻微;
  139. # 150~300(较小色差),感觉明显;
  140. # 300~600(较大色差),感觉很明显;
  141. # 600以上(大色差),感觉强烈。
  142. # '''
  143. def colorDistance(self, color1, color2):
  144. b1, g1, r1 = color1
  145. b2, g2, r2 = color2
  146. rmean = (r1 + r2) / 2
  147. R = int(r1) - int(r2)
  148. G = int(g1) - int(g2)
  149. B = int(b1) - int(b2)
  150. return math.sqrt((2 + rmean / 256) * (R ** 2) + 4 * (G ** 2) + (2 + (255 - rmean) / 256) * (B ** 2))
  151. '''
  152. # 描述:颜色是否相近。
  153. # 参数:
  154. # color1、color2: bgr格式
  155. # faultTolerance: 容错值大小,表示两颜色的色差值
  156. # 返回值:<= faultTolerance返回True,否则返回False.
  157. #
  158. # '''
  159. def isColorSimilar(self, color1, color2, faultTolerance=100):
  160. if 0:
  161. b1 = color1[0] in range(color2[0] - faultTolerance, color2[0] + faultTolerance)
  162. b2 = color1[1] in range(color2[1] - faultTolerance, color2[1] + faultTolerance)
  163. b3 = color1[2] in range(color2[2] - faultTolerance, color2[2] + faultTolerance)
  164. if b1 == True and b2 == True and b3 == True:
  165. return True
  166. else:
  167. return False
  168. if 1:
  169. val = self.colorDistance(color1, color2)
  170. return True if val <= faultTolerance else False