cie_luv.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. # -*- coding:utf-8 -*-
  2. '''
  3. 颜色空间CIE LUV
  4. '''
  5. import os, sys, time
  6. import cv2 as cv
  7. import numpy as np
  8. import colorsys
  9. import math
  10. class LUV():
  11. def __init__(self, stdimg = None, testimg=None):
  12. self.stdimg = stdimg
  13. self.testimg = testimg
  14. def setSTDImage(self, img):
  15. self.stdimg = img
  16. def setTestImage(self ,img):
  17. self.testimg = img
  18. '''
  19. 计算单个指定区域的LUV平均值
  20. '''
  21. def getAverageLUV(self, img, area=None):
  22. if (area is None):
  23. area = (0, 0, img.shape[1], img.shape[0]) # mat shape:高、宽、颜色值个数
  24. # print "getAverageLUV", img.shape, area
  25. # print img.shape
  26. img = np.array(img)
  27. if len(area) == 4:
  28. x1 = area[0]
  29. y1 = area[1]
  30. x2 = area[2]
  31. y2 = area[3]
  32. img = img[y1:y2+1, x1:x2+1]
  33. # img = img[0:80, 0:100]
  34. # print img.shape
  35. total_L = 0
  36. total_U = 0
  37. total_V = 0
  38. pixel_total = img.shape[0] * img.shape[1]
  39. # print "pixel_total:",pixel_total
  40. for x in range(0, img.shape[0]):
  41. for y in range(0, img.shape[1]):
  42. total_L = total_L + img[x][y][0]
  43. total_U = total_U + img[x][y][1]
  44. total_V = total_V + img[x][y][2]
  45. return total_L / pixel_total, total_U / pixel_total, total_V / pixel_total
  46. else:
  47. return None, None, None
  48. '''
  49. 计算所有区域的LUV平均值
  50. '''
  51. def getMAverageLUV(self, img, areaList):
  52. total_L ,total_U, total_V, pixel_total = 0 ,0 ,0 ,0
  53. for area in areaList:
  54. L ,U ,V ,P = self.countTotalPLuv(img, area)
  55. total_L += L
  56. total_U += U
  57. total_V += V
  58. pixel_total += P
  59. # print "getMAverageLUV:",area,L,U,V,P
  60. if (pixel_total == 0):
  61. return None, None, None
  62. return total_L /pixel_total ,total_U /pixel_total ,total_V /pixel_total
  63. '''
  64. 计算整张图片指定区域Luv颜色值总和。
  65. '''
  66. def countTotalPLuv(self, img, area=None):
  67. if (area is None):
  68. area = (0, 0, img.shape[1], img.shape[0]) # mat shape:高、宽、颜色值个数
  69. # print "getAverageLUV", img.shape, area
  70. # print img.shape
  71. img = np.array(img)
  72. if len(area) == 4:
  73. x1 = area[0]
  74. y1 = area[1]
  75. x2 = area[2]
  76. y2 = area[3]
  77. img = img[y1:y2 +1, x1:x2 +1]
  78. # img = img[0:80, 0:100]
  79. # print img.shape
  80. total_L = 0
  81. total_U = 0
  82. total_V = 0
  83. pixel_total = img.shape[0] * img.shape[1]
  84. # print "pixel_total:",pixel_total
  85. for x in range(0, img.shape[0]):
  86. for y in range(0, img.shape[1]):
  87. total_L = total_L + img[x][y][0]
  88. total_U = total_U + img[x][y][1]
  89. total_V = total_V + img[x][y][2]
  90. return total_L ,total_U , total_V , pixel_total
  91. else:
  92. return None, None, None, None
  93. def getDiffLevel(self, stdLuv, testLuv):
  94. return self.getDiffUV(stdLuv, testLuv)
  95. def getDiffuvLevel(self, stdLuv, testLuv):
  96. diff = self.getDiffuv(stdLuv, testLuv)
  97. return diff[2]
  98. def getDiffluvLevel(self ,stdLuv, testLuv):
  99. return self.getDiffLUV(stdLuv, testLuv)
  100. def getDiffUV(self, stdLuv, testLuv):
  101. tmp = np.int32(testLuv[0]) - np.int32(stdLuv[0])
  102. lDiff = abs(tmp)
  103. tmp = np.int32(testLuv[1]) - np.int32(stdLuv[1])
  104. uDiff = abs(tmp)
  105. tmp = np.int32(testLuv[2]) - np.int32(stdLuv[2])
  106. vDiff = abs(tmp)
  107. lineDiff = np.sqrt(np.square(uDiff )+ np.square(vDiff))
  108. return uDiff ,vDiff ,lineDiff
  109. def getDiffuv(self, stdLuv, testLuv):
  110. return self.getDiffUV(stdLuv, testLuv)
  111. def getDiffLUV(self, stdLuv, testLuv):
  112. tmp = np.int32(testLuv[0]) - np.int32(stdLuv[0])
  113. lDiff = abs(tmp)
  114. tmp = np.int32(testLuv[1]) - np.int32(stdLuv[1])
  115. uDiff = abs(tmp)
  116. tmp = np.int32(testLuv[2]) - np.int32(stdLuv[2])
  117. vDiff = abs(tmp)
  118. lineDiff = np.sqrt(np.square(lDiff) + np.square(uDiff )+ np.square(vDiff))
  119. return lDiff, uDiff ,vDiff ,lineDiff
  120. '''
  121. # 描述:获取HSV平均值
  122. # 参数:
  123. # img_path:原图路径
  124. # area:要截图的区域
  125. # '''
  126. def getAverageHSV(self, img_path, area = None):
  127. img = cv.imread(img_path)
  128. if img is None:
  129. print u'Img is None, please check the img path!'
  130. return None, None, None
  131. # 求RGB;
  132. if (area is None):
  133. area = (0, 0, img.shape[1], img.shape[0]) # mat shape:高、宽、颜色值个数
  134. if len(area) == 4:
  135. x1 = area[0]
  136. y1 = area[1]
  137. x2 = area[2]
  138. y2 = area[3]
  139. img = img[y1:y2 +1, x1:x2 +1]
  140. # 宽,高;
  141. width, height = abs(x2 - x1), abs(y2 - y1)
  142. # print 'area=',area
  143. total_B = 0
  144. total_G = 0
  145. total_R = 0
  146. # pixel_total = hsv.shape[0] * hsv.shape[1]
  147. pixel_total = width * height
  148. if width == 0 or height == 0:
  149. print u'宽或高为0'
  150. return None, None, None
  151. # print 'width,hieght=',width,height,pixel_total
  152. for y in range(0, height):
  153. for x in range(0, width):
  154. total_B += img[y][x][0]
  155. total_G += img[y][x][1]
  156. total_R += img[y][x][2]
  157. # print u't->b,g,r=',total_B,total_G,total_R
  158. b ,g ,r = total_B / pixel_total, total_G / pixel_total, total_R / pixel_total
  159. # print 'bgr=',b,g,r
  160. # 转成hsv;
  161. hsv = self.rgb2hsv(r ,g ,b)
  162. # print 'hsv-1',hsv
  163. # hsv = colorsys.rgb_to_hsv(r,g,b)
  164. # print 'hsv-2',hsv
  165. return [round(hsv[0 ] /2), round(hsv[1]), round(hsv[2])]
  166. else:
  167. return [None, None, None]
  168. # def rgb2hsv(self, r, g, b):
  169. # r, g, b = r/255.0, g/255.0, b/255.0
  170. # mx = max(r, g, b)
  171. # mn = min(r, g, b)
  172. # df = mx-mn
  173. # if mx == mn:
  174. # h = 0
  175. # elif mx == r:
  176. # h = (60 * ((g-b)/df) + 360) % 360
  177. # elif mx == g:
  178. # h = (60 * ((b-r)/df) + 120) % 360
  179. # elif mx == b:
  180. # h = (60 * ((r-g)/df) + 240) % 360
  181. # if mx == 0:
  182. # s = 0
  183. # else:
  184. # s = df/mx
  185. # v = mx
  186. # return h, s, v
  187. def rgb2hsv(self, r, g, b):
  188. r, g, b = r/ 255.0, g / 255.0, b / 255.0
  189. mx = max(r, g, b)
  190. mn = min(r, g, b)
  191. df = mx - mn
  192. # h值
  193. if mx == mn:
  194. h = 0
  195. elif mx == r and g >= b:
  196. h = (60 * (g - b) / df) % 360
  197. elif mx == r and g < b:
  198. h = (60 * ((g - b) / df) + 360) % 360
  199. elif mx == g:
  200. h = (60 * ((b - r) / df) + 120) % 360
  201. elif mx == b:
  202. h = (60 * ((r - g) / df) + 240) % 360
  203. # s值
  204. if mx == 0:
  205. s = 0
  206. else:
  207. s = df / mx * 255
  208. # v值
  209. v = mx * 255
  210. # 返回;
  211. return h, s, v
  212. def isUVSimilar(self, stdLuv, testLuv, diffLevel=20):
  213. udiff, vdiff, lineDiff = self.getDiffUV(stdLuv, testLuv)
  214. if lineDiff <= diffLevel:
  215. return True
  216. else:
  217. return False