PreImageJudge.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. # encoding: utf-8
  2. import cv2 as cv
  3. import numpy as np
  4. import random
  5. from ssat_sdk.picture.RGB import RGBColor
  6. '''
  7. 用于图片初级检测,例如:颜色、噪声
  8. 此类已经废弃
  9. '''
  10. def getMaxRGB(imgFile,area):
  11. img = cv.imread(imgFile,1)
  12. if (area is None):
  13. area = (0,0,img.shape[1], img.shape[0])#mat shape:高、宽、颜色值个数
  14. img = cv.cvtColor(img,cv.COLOR_BGR2RGB)
  15. img = np.array(img)
  16. if len(area) == 4:
  17. rgbColor = RGBColor()
  18. x1 = area[0]
  19. y1 = area[1]
  20. x2 = area[2]
  21. y2 = area[3]
  22. img = img[y1:y2+1,x1:x2+1]
  23. return rgbColor.getMaxBGR(img)
  24. else:
  25. return None,None,None
  26. '''
  27. 截取图片区域RGB平均值。
  28. :param area:二维区域。坐标系:x轴:水平向右,y轴:纵向向下
  29. '''
  30. def getAverageRGB(img_addr, area = None):
  31. img = cv.imread(img_addr)
  32. return getImgAverageRGB(img, area)
  33. '''
  34. 截取图片区域RGB平均值。
  35. :param area:二维区域。坐标系:x轴:水平向右,y轴:纵向向下
  36. '''
  37. def getImgAverageRGB(img, area):
  38. if (area is None):
  39. area = (0,0,img.shape[1], img.shape[0])#mat shape:高、宽、颜色值个数
  40. print "getImgAverageRGB", img.shape, area
  41. # print img.shape
  42. img = cv.cvtColor(img,cv.COLOR_BGR2RGB)
  43. img = np.array(img)
  44. if len(area) == 4:
  45. rgbColor = RGBColor()
  46. x1 = area[0]
  47. y1 = area[1]
  48. x2 = area[2]
  49. y2 = area[3]
  50. img = img[y1:y2, x1:x2]
  51. return rgbColor.getAvgBGR(img)
  52. else:
  53. return None, None, None
  54. '''@废弃'''
  55. # 判断黑屏
  56. def isBlack(img_addr, black_th=15):
  57. gray = cv.imread(img_addr,0)
  58. width = gray.shape[1]
  59. hight = gray.shape[0]
  60. for i in range(0,50):
  61. random_y = random.randint(0,width-1)
  62. random_x = random.randint(0,hight-1)
  63. if gray[random_x][random_y] >= black_th:
  64. return False
  65. else:
  66. continue
  67. return True
  68. #判断噪点
  69. def hasNoise(img_addr):
  70. pass
  71. def getPoints(img_addr):
  72. kernel = np.ones((11, 11), dtype=np.uint8)
  73. img = cv.imread(img_addr, 1)
  74. gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
  75. gray = gray[gray.shape[0]/2:gray.shape[0],gray.shape[1]/2:gray.shape[1]]
  76. x = cv.Sobel(gray, cv.CV_16S,1, 0)
  77. y = cv.Sobel(gray, cv.CV_16S, 0, 1)
  78. absX = cv.convertScaleAbs(x) # 转回uint8
  79. absY = cv.convertScaleAbs(y)
  80. dst = cv.addWeighted(absX, 0.5, absY, 0.5,0)
  81. _, dst_2 = cv.threshold(dst, 100, 255, cv.THRESH_BINARY)
  82. img_close = cv.erode(cv.dilate(dst_2, kernel), kernel)
  83. image, contours, hierarchy = cv. findContours(img_close, 1, 2)
  84. true_rectang = []
  85. for i in range(0,len(contours)):
  86. rectang = []
  87. temp_arr = []
  88. for j in range(0,len(contours[i])):
  89. for points_index in range(0, len(contours[i][j])):
  90. temp_arr.append(contours[i][j][0])
  91. for temp_arr_index in range(0, len(temp_arr)):
  92. if temp_arr_index == 0:
  93. # 第一个点加入数组
  94. rectang.append(temp_arr[temp_arr_index])
  95. else:
  96. # 遍历所有点
  97. flag = 0
  98. for rectang_index in range(0, len(rectang)):
  99. if (abs(temp_arr[temp_arr_index][0]-rectang[rectang_index][0]) >= 15) or (abs(temp_arr[temp_arr_index][1]-rectang[rectang_index][1]) >= 15):
  100. flag = flag + 1
  101. if flag == len(rectang):
  102. rectang.append(temp_arr[temp_arr_index])
  103. if len(rectang) == 4:
  104. # 判断是否我们需要的矩形 ,如果是 则返回到true_rectang
  105. true_rectang.append(rectang)
  106. return true_rectang
  107. '''
  108. @废弃
  109. 判断马赛克
  110. '''
  111. def hasMosaic(std_pic,test_pic,num):
  112. std_pic = getPoints(std_pic)
  113. test_pic = getPoints(test_pic)
  114. print len(std_pic)
  115. print len(test_pic)
  116. if len(test_pic) != len(std_pic):
  117. return True
  118. else:
  119. return False
  120. class PreImageJudge():
  121. def __init__(self):
  122. self.BGR = RGBColor()
  123. '''
  124. 判断一张图片是否是纯黑图片。
  125. :param img:需要判断的图片
  126. :param grayTH : 黑色判断的灰阶阈值
  127. :param splitCount: 图片拆分的行列次数
  128. :param expTh:黑色计算的容错区域比例。
  129. '''
  130. def isBlack(self, img, grayTH = 70,splitCount=10, expTh = 0.01):
  131. return self.BGR.isBlack()
  132. if __name__ == '__main__':
  133. print hasMosaic('std.jpg','test.jpg',num=None)