# encoding: utf-8 import cv2 as cv import numpy as np import random from ssat_sdk.picture.RGB import RGBColor ''' 用于图片初级检测,例如:颜色、噪声 此类已经废弃 ''' def getMaxRGB(imgFile,area): img = cv.imread(imgFile,1) if (area is None): area = (0,0,img.shape[1], img.shape[0])#mat shape:高、宽、颜色值个数 img = cv.cvtColor(img,cv.COLOR_BGR2RGB) img = np.array(img) if len(area) == 4: rgbColor = RGBColor() x1 = area[0] y1 = area[1] x2 = area[2] y2 = area[3] img = img[y1:y2+1,x1:x2+1] return rgbColor.getMaxBGR(img) else: return None,None,None ''' 截取图片区域RGB平均值。 :param area:二维区域。坐标系:x轴:水平向右,y轴:纵向向下 ''' def getAverageRGB(img_addr, area = None): img = cv.imread(img_addr) return getImgAverageRGB(img, area) ''' 截取图片区域RGB平均值。 :param area:二维区域。坐标系:x轴:水平向右,y轴:纵向向下 ''' def getImgAverageRGB(img, area): if (area is None): area = (0,0,img.shape[1], img.shape[0])#mat shape:高、宽、颜色值个数 print "getImgAverageRGB", img.shape, area # print img.shape img = cv.cvtColor(img,cv.COLOR_BGR2RGB) img = np.array(img) if len(area) == 4: rgbColor = RGBColor() x1 = area[0] y1 = area[1] x2 = area[2] y2 = area[3] img = img[y1:y2, x1:x2] return rgbColor.getAvgBGR(img) else: return None, None, None '''@废弃''' # 判断黑屏 def isBlack(img_addr, black_th=15): gray = cv.imread(img_addr,0) width = gray.shape[1] hight = gray.shape[0] for i in range(0,50): random_y = random.randint(0,width-1) random_x = random.randint(0,hight-1) if gray[random_x][random_y] >= black_th: return False else: continue return True #判断噪点 def hasNoise(img_addr): pass def getPoints(img_addr): kernel = np.ones((11, 11), dtype=np.uint8) img = cv.imread(img_addr, 1) gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY) gray = gray[gray.shape[0]/2:gray.shape[0],gray.shape[1]/2:gray.shape[1]] x = cv.Sobel(gray, cv.CV_16S,1, 0) y = cv.Sobel(gray, cv.CV_16S, 0, 1) absX = cv.convertScaleAbs(x) # 转回uint8 absY = cv.convertScaleAbs(y) dst = cv.addWeighted(absX, 0.5, absY, 0.5,0) _, dst_2 = cv.threshold(dst, 100, 255, cv.THRESH_BINARY) img_close = cv.erode(cv.dilate(dst_2, kernel), kernel) image, contours, hierarchy = cv. findContours(img_close, 1, 2) true_rectang = [] for i in range(0,len(contours)): rectang = [] temp_arr = [] for j in range(0,len(contours[i])): for points_index in range(0, len(contours[i][j])): temp_arr.append(contours[i][j][0]) for temp_arr_index in range(0, len(temp_arr)): if temp_arr_index == 0: # 第一个点加入数组 rectang.append(temp_arr[temp_arr_index]) else: # 遍历所有点 flag = 0 for rectang_index in range(0, len(rectang)): 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): flag = flag + 1 if flag == len(rectang): rectang.append(temp_arr[temp_arr_index]) if len(rectang) == 4: # 判断是否我们需要的矩形 ,如果是 则返回到true_rectang true_rectang.append(rectang) return true_rectang ''' @废弃 判断马赛克 ''' def hasMosaic(std_pic,test_pic,num): std_pic = getPoints(std_pic) test_pic = getPoints(test_pic) print len(std_pic) print len(test_pic) if len(test_pic) != len(std_pic): return True else: return False class PreImageJudge(): def __init__(self): self.BGR = RGBColor() ''' 判断一张图片是否是纯黑图片。 :param img:需要判断的图片 :param grayTH : 黑色判断的灰阶阈值 :param splitCount: 图片拆分的行列次数 :param expTh:黑色计算的容错区域比例。 ''' def isBlack(self, img, grayTH = 70,splitCount=10, expTh = 0.01): return self.BGR.isBlack() if __name__ == '__main__': print hasMosaic('std.jpg','test.jpg',num=None)