def retrieve_dipstick_image(image, contour): """ Calculate the minimum area bounding the dipstick image, then apply a rotation matrix to straighten the rectangle. If the dipstick has been rotated into a vertical position it is rotated anticlockwise. The horizontal dipstick image is then resized for extracting squares on the dipstick. """ try: rectangle = cv.minAreaRect(contour) except cv.error: return None center, size, theta = rectangle height, width = image.shape[:2] center, size = tuple(map(int, center)), tuple(map(int, size)) matrix = cv.getRotationMatrix2D(center, theta, 1) dst = cv.warpAffine(image, matrix, (width, height)) dipstick = cv.getRectSubPix(dst, size, center) if dipstick.shape[0] > dipstick.shape[1]: dipstick = cv.rotate(dipstick, cv.ROTATE_90_COUNTERCLOCKWISE) dipstick = imutils.resize(dipstick, width=800) return dipstick
def rotate(image, angle, center=None, scale=1.0): assert image.dtype == np.uint8 """Copy paste of imutils method but with INTER_NEAREST and BORDER_CONSTANT flags""" # grab the dimensions of the image (h, w) = image.shape[:2] # if the center is None, initialize it as the center of # the image if center is None: center = (w // 2, h // 2) # perform the rotation M = cv2.getRotationMatrix2D(center, angle, scale) rotated = cv2.warpAffine( image, M, (w, h), flags=cv2.INTER_NEAREST, borderMode=cv2.BORDER_CONSTANT, borderValue=0, ) # return the rotated image return rotated
def transform(self, image): rows, cols = image.shape[:2] scale1 = (random.randrange(30, 45) / 100) scale2 = (random.randrange(30, 45) / 100) scale3 = (random.randrange(1, 5) / 10) scale4 = (random.randrange(1, 5) / 10) point1 = numpy.float32([[50, 50], [rows - 50, 50], [50, cols - 50], [rows - 50, cols - 50]]) point2 = numpy.float32([[(100 * scale1), (100 * scale2)], [(rows * (1 - scale1)), (cols * scale3)], [(rows * scale4), (cols * (1 - scale2))], [(rows * (1 - scale3)), (cols * (1 - scale4))]]) point1_ = point1[0:3] point2_ = point2[0:3] Matirx_aff = cv2.getAffineTransform(point1_, point2_) img_aff = cv2.warpAffine(image, Matirx_aff, (cols, rows), borderValue=(255, 255, 255)) Matirx_per = cv2.getPerspectiveTransform(point1, point2) img_per = cv2.warpPerspective(image, Matirx_per, (cols, rows), borderValue=(255, 255, 255)) return img_aff, img_per
def mtcnn_filter_save_single( image_path, save_image_folder, confidence_filter=0.99, face_height_filter=MIN_FACE_SIZE, nose_shift_filter=20, eye_line_angle_filter=45, sharpness_filter=25, ): _, file_name = os.path.split(image_path) image_name, img_ext = os.path.splitext(file_name) print(image_name, img_ext) MTCNN_res = get_MTCNN_result(image_path) for image_idx, res in enumerate(MTCNN_res): confidence = res['confidence'] bounding_box = res['box'] keypoints = res['keypoints'] upper_left_x, upper_left_y, width, height = bounding_box # change box from rectangular to square side = max(height, width) upper_left_x = int(upper_left_x + width / 2 - side / 2) if upper_left_x < 0: upper_left_x = 0 if upper_left_y < 0: upper_left_y = 0 width = height = side if (confidence >= confidence_filter) and (height >= face_height_filter): # find an angle of line of eyes. dY = keypoints['right_eye'][1] - keypoints['left_eye'][1] dX = keypoints['right_eye'][0] - keypoints['left_eye'][0] angle = np.degrees(np.arctan2(dY, dX)) # calculate rotation matrix for this anlge around nose as a central point rot_mat = cv2.getRotationMatrix2D(keypoints['nose'], angle, 1.0) # calculate new coordinates of keypoints keypoints_rotated = get_rotated_keypoints(keypoints, rot_mat) # calculate nose shift nose_shift = 100 * abs( (keypoints_rotated['nose'][0] - keypoints_rotated['left_eye'][0] - dX / 2) / dX) if (nose_shift <= nose_shift_filter) and (abs(angle) <= eye_line_angle_filter): image = read_image(image_path) if is_not_grayscale(image): image_rotated = cv2.warpAffine(image, rot_mat, image.shape[1::-1], flags=cv2.INTER_LINEAR) image_cropped_central_part = image_rotated[ int(upper_left_y + height * 1 / 2):int(upper_left_y + height * 2 / 3), int(upper_left_x + width * 1 / 3):int(upper_left_x + width * 2 / 3)] sharpness = variance_of_laplacian( image_cropped_central_part) if sharpness >= sharpness_filter: image_cropped = image_rotated[ upper_left_y:upper_left_y + height, upper_left_x:upper_left_x + width] image_resized = resize_image(image_cropped, (160, 160)) imagefile_path = save_image_folder + '/' + image_name + '_' + str( image_idx) + img_ext cv2.imwrite( imagefile_path, cv2.cvtColor(image_resized, cv2.COLOR_RGB2BGR)) else: print('image sharpness < ', sharpness_filter) else: print('grayscale image') else: print('nose shift:', nose_shift, '>', 'filter:', nose_shift_filter, 'or eye_line_angle:', abs(angle), '>', 'filter', eye_line_angle_filter) else: print('low_confidence:', confidence, 'or height:', height, '<', 'height_filter:', face_height_filter)
filename = os.path.basename(captcha_image_file) captcha_correct_text = os.path.splitext(filename)[0] image = cv2.imread(captcha_image_file) image = cv2.bitwise_not(image) # counter = 0 save_path = os.path.join(IMG_DES, captcha_correct_text) if not os.path.exists(save_path): os.makedirs(save_path) for i in range(50): #i = j+1 M = cv2.getRotationMatrix2D((cols / 2, rows / 2), i, 1) dst = cv2.warpAffine(image, M, (cols, rows)) #dst.dtype = 'uint8' M1 = cv2.getRotationMatrix2D((cols / 2, rows / 2), i * (-1), 1) dst1 = cv2.warpAffine(image, M1, (cols, rows)) #dst.dtype = 'uint8' p = os.path.join(save_path, "{}.png".format(str(i))) p1 = os.path.join(save_path, "{}.png".format(str(50 + i))) cv2.imwrite(p, dst) cv2.imwrite(p1, dst1) p1 = os.path.join(save_path, "{}.png".format(str(110))) p2 = os.path.join(save_path, "{}.png".format(str(111))) p3 = os.path.join(save_path, "{}.png".format(str(112))) cv2.imwrite(p1, image) cv2.imwrite(p2, image)
def warp(src, mat, outsize): return cv.warpAffine(src, mat[:2, :].astype("float32"), outsize)
def videoCap(): # pytesseract.pytesseract.tesseract_cmd = "C:/Program Files/Tesseract-OCR/tesseract" cap = cv2.VideoCapture( 'C:/Users/jeawa/Desktop/project/LPR_Project/project/video/test_car.mp4' ) count = 0 before_chars = "" while True: el = cv2.getTickCount() fps = cap.get(5) ret, img_ori = cap.read() height, width, channel = img_ori.shape # 높이, 너비, 채널 확보 gray = cv2.cvtColor(img_ori, cv2.COLOR_BGR2GRAY) # img_ori = cv2.imread('LPR_Project/project/image/3.jpg') # 이미지 불러오기 # gray = cv2.cvtColor(img_ori, cv2.COLOR_BGR2GRAY) img_blurred = cv2.GaussianBlur(gray, ksize=(3, 3), sigmaX=0) # 노이즈 제거 img_thresh = cv2.adaptiveThreshold( img_blurred, maxValue=255.0, adaptiveMethod=cv2.ADAPTIVE_THRESH_GAUSSIAN_C, thresholdType=cv2.THRESH_BINARY_INV, blockSize=19, # 12 통과 15,20 C=9) contours, _ = cv2.findContours( # 윤곽선을 찾기 img_thresh, mode=cv2.RETR_LIST, # method=cv2.CHAIN_APPROX_TC89_KCOS # ) temp_result = np.zeros((height, width, channel), dtype=np.uint8) cv2.drawContours( temp_result, # 원본이미지 contours=contours, # contours 정보 contourIdx=-1, # -1 : 전체 color=(255, 255, 255), thickness=1) contours_dict = [] for contour in contours: x, y, w, h = cv2.boundingRect(contour) # 컴투어를 감싸는 사각형 # cv2.rectangle( # temp_result, # pt1=(x, y), # pt2=(x+w, y+h), # color=(255, 255, 255), # thickness=1 # ) contours_dict.append({ 'contour': contour, 'x': x, 'y': y, 'w': w, 'h': h, 'cx': x + (w / 2), # 중심 좌표 'cy': y + (h / 2) }) MIN_AREA, MAX_AREA = 100, 1000 # boundingRect(사각형)의 최소넓이 MIN_WIDTH, MIN_HEIGHT = 2, 8 # boundingRect의 최소 넓이, 높이 2, 8 MIN_RATIO, MAX_RATIO = 0.3, 0.9 # boundingRect의 가로 세로 비율 possible_contours = [] # 위의 조건의 만족하는 사각형 cnt = 0 for d in contours_dict: area = d['w'] * d['h'] # 가로 * 세로 = 면적 ratio = d['w'] / d['h'] # 가로 / 세로 = 비율 if MIN_AREA < area < MAX_AREA and d['w'] > MIN_WIDTH and d[ 'h'] > MIN_HEIGHT and MIN_RATIO < ratio < MAX_RATIO: d['idx'] = cnt # 조건의 맞는 값을 idx에 저장한다. cnt += 1 possible_contours.append(d) # possible_contour를 업데이트한다. # temp_result = np.zeros((height, width, channel), dtype=np.uint8) for d in possible_contours: # cv2.drawContours(temp_result, d['contour'], -1, (255, 255, 255)) cv2.rectangle(temp_result, pt1=(d['x'], d['y']), pt2=(d['x'] + d['w'], d['y'] + d['h']), color=(0, 0, 255), thickness=1) MAX_DIAG_MULTIPLYER = 4 # 대각선의 5배 안에 있어야함 MAX_ANGLE_DIFF = 12.0 # 12.0 #세타의 최대값 12, 0.7, 0.5, 0.6, 3 MAX_AREA_DIFF = 0.3 # 0.5 #면적의 차이 MAX_WIDTH_DIFF = 0.5 # 너비차이 MAX_HEIGHT_DIFF = 0.6 # 높이차이 MIN_N_MATCHED = 4 # 3 #위의 조건이 3개 미만이면 뺀다 def find_chars(contour_list): # matched_result_idx = [] # idx 값 저장 for d1 in contour_list: matched_contours_idx = [] for d2 in contour_list: if d1['idx'] == d2['idx']: # d1 과 d2가 같으면 컨티뉴 continue dx = abs(d1['cx'] - d2['cx']) dy = abs(d1['cy'] - d2['cy']) diagonal_length1 = np.sqrt(d1['w']**2 + d1['h']**2) distance = np.linalg.norm( np.array([d1['cx'], d1['cy']]) - np.array([d2['cx'], d2['cy']])) # 대각선 길이 if dx == 0: angle_diff = 90 # 0일 때 각도 90도 (예외처리) else: angle_diff = np.degrees(np.arctan(dy / dx)) # 세타 구하기 area_diff = abs(d1['w'] * d1['h'] - d2['w'] * d2['h']) / ( d1['w'] * d1['h']) # 면적 비율 width_diff = abs(d1['w'] - d2['w']) / d1['w'] # 너비비율 height_diff = abs(d1['h'] - d2['h']) / d1['h'] # 높이비율 # 조건들 if distance < diagonal_length1 * MAX_DIAG_MULTIPLYER \ and angle_diff < MAX_ANGLE_DIFF and area_diff < MAX_AREA_DIFF \ and width_diff < MAX_WIDTH_DIFF and height_diff < MAX_HEIGHT_DIFF: # d2만 넣었기 때문에 마지막으로 d1을 넣은다 matched_contours_idx.append(d2['idx']) # append this contour matched_contours_idx.append(d1['idx']) if len(matched_contours_idx) < MIN_N_MATCHED: # 3개 이하이면 번호판 x continue matched_result_idx.append(matched_contours_idx) # 최종 후보군 unmatched_contour_idx = [] # 최종후보군이 아닌 애들 for d4 in contour_list: if d4['idx'] not in matched_contours_idx: unmatched_contour_idx.append(d4['idx']) unmatched_contour = np.take(possible_contours, unmatched_contour_idx) # recursive recursive_contour_list = find_chars(unmatched_contour) for idx in recursive_contour_list: matched_result_idx.append(idx) # 번호판 이외의 값을 재정의 break return matched_result_idx result_idx = find_chars(possible_contours) matched_result = [] for idx_list in result_idx: matched_result.append(np.take(possible_contours, idx_list)) # visualize possible contours temp_result = np.zeros((height, width, channel), dtype=np.uint8) for r in matched_result: for d in r: cv2.rectangle(temp_result, pt1=(d['x'], d['y']), pt2=(d['x'] + d['w'], d['y'] + d['h']), color=(0, 0, 255), thickness=1) PLATE_WIDTH_PADDING = 1.2 # 1.3 PLATE_HEIGHT_PADDING = 1.5 # 1.5 MIN_PLATE_RATIO = 3 MAX_PLATE_RATIO = 7 #10 plate_imgs = [] plate_infos = [] for i, matched_chars in enumerate(matched_result): sorted_chars = sorted(matched_chars, key=lambda x: x['cx']) # x방향으로 순차적으로 정렬 plate_cx = (sorted_chars[0]['cx'] + sorted_chars[-1]['cx']) / 2 # 센터 좌표 구하기 plate_cy = (sorted_chars[0]['cy'] + sorted_chars[-1]['cy']) / 2 plate_width = (sorted_chars[-1]['x'] + sorted_chars[-1]['w'] - sorted_chars[0]['x']) * PLATE_WIDTH_PADDING # 너비 # sum_height = 0 # for d in sorted_chars: # sum_height += d['h'] # plate_height = int(sum_height / len(sorted_chars) # * PLATE_HEIGHT_PADDING) # 높이 plate_height = (sorted_chars[-1]['y'] - sorted_chars[0]['y'] + sorted_chars[-1]['h']) * PLATE_HEIGHT_PADDING # triangle_height = sorted_chars[-1]['cy'] - sorted_chars[0]['cy'] triangle_hypotenus = np.linalg.norm( np.array([sorted_chars[0]['cx'], sorted_chars[0]['cy']]) - np.array([sorted_chars[-1]['cx'], sorted_chars[-1]['cy']])) angle = np.degrees(np.arcsin(triangle_height / triangle_hypotenus)) rotation_matrix = cv2.getRotationMatrix2D(center=(plate_cx, plate_cy), angle=angle, scale=1.0) # 회전 img_rotated = cv2.warpAffine(img_thresh, M=rotation_matrix, dsize=(width, height)) img_cropped = cv2.getRectSubPix(img_rotated, patchSize=(int(plate_width), int(plate_height)), center=(int(plate_cx), int(plate_cy))) if img_cropped.shape[1] / img_cropped.shape[ 0] < MIN_PLATE_RATIO or img_cropped.shape[ 1] / img_cropped.shape[ 0] < MIN_PLATE_RATIO > MAX_PLATE_RATIO: continue plate_imgs.append(img_cropped) plate_infos.append({ 'x': int(plate_cx - plate_width / 2), 'y': int(plate_cy - plate_height / 2), 'w': int(plate_width), 'h': int(plate_height) }) for i, plate_img in enumerate(plate_imgs): plate_img = cv2.resize(plate_img, dsize=(0, 0), fx=1.6, fy=1.6) _, plate_img = cv2.threshold(plate_img, thresh=0.0, maxval=255.0, type=cv2.THRESH_BINARY | cv2.THRESH_OTSU) # plt.imshow(plate_img, cmap='gray') # plt.show() # find contours again (same as above) contours, _ = cv2.findContours(plate_img, mode=cv2.RETR_LIST, method=cv2.CHAIN_APPROX_SIMPLE) plate_min_x, plate_min_y = plate_img.shape[1], plate_img.shape[ 0] plate_max_x, plate_max_y = 0, 0 for contour in contours: x, y, w, h = cv2.boundingRect(contour) area = w * h ratio = w / h if area > MIN_AREA \ and w > MIN_WIDTH and h > MIN_HEIGHT \ and MIN_RATIO < ratio < MAX_RATIO: if x < plate_min_x: plate_min_x = x if y < plate_min_y: plate_min_y = y if x + w > plate_max_x: plate_max_x = x + w if y + h > plate_max_y: plate_max_y = y + h img_result = plate_img[plate_min_y:plate_max_y, plate_min_x:plate_max_x] img_result = cv2.GaussianBlur(img_result, ksize=(3, 3), sigmaX=0) _, img_result = cv2.threshold(img_result, thresh=0.0, maxval=255.0, type=cv2.THRESH_BINARY | cv2.THRESH_OTSU) img_result = cv2.copyMakeBorder(img_result, top=10, bottom=10, left=20, right=10, borderType=cv2.BORDER_CONSTANT, value=(0, 0, 0)) chars = pytesseract.image_to_string(img_result, lang='kor', config='--psm 7 --oem 0') result_chars = '' has_digit = False # <= ord('힣') for c in chars: if \ + ord('가') == ord(c) or \ + ord('나') == ord(c) or \ + ord('다') == ord(c) or \ + ord('라') == ord(c) or \ + ord('마') == ord(c) or \ + ord('거') == ord(c) or \ + ord('너') == ord(c) or \ + ord('더') == ord(c) or \ + ord('러') == ord(c) or \ + ord('머') == ord(c) or \ + ord('버') == ord(c) or \ + ord('서') == ord(c) or \ + ord('어') == ord(c) or \ + ord('저') == ord(c) or \ + ord('고') == ord(c) or \ + ord('노') == ord(c) or \ + ord('도') == ord(c) or \ + ord('로') == ord(c) or \ + ord('모') == ord(c) or \ + ord('보') == ord(c) or \ + ord('소') == ord(c) or \ + ord('오') == ord(c) or \ + ord('조') == ord(c) or \ + ord('구') == ord(c) or \ + ord('누') == ord(c) or \ + ord('두') == ord(c) or \ + ord('루') == ord(c) or \ + ord('무') == ord(c) or \ + ord('부') == ord(c) or \ + ord('수') == ord(c) or \ + ord('우') == ord(c) or \ + ord('주') == ord(c) or \ + ord('아') == ord(c) or \ + ord('바') == ord(c) or \ + ord('사') == ord(c) or \ + ord('자') == ord(c) or \ + ord('배') == ord(c) or \ + ord('하') == ord(c) or \ + ord('허') == ord(c) or \ + ord('호') == ord(c) or c.isdigit(): if c.isdigit(): has_digit = True result_chars += c # print(result_chars) # print(len(result_chars)) if result_chars == "": pass else: if before_chars == result_chars and 6 < len(result_chars) < 9: count += 1 else: before_chars = result_chars count = 0 if count == 1: print(result_chars) count = 0 before_chars = "" # print(fps + " : 프레임 영상입니다.") print(fps) a = np.hstack((img_ori, temp_result)) cv2.imshow("Go", a) if cv2.waitKey(42) == ord('q'): break e2 = cv2.getTickCount() time = (e2 - el) / cv2.getTickFrequency() print(time) # print("1프레임 : 1초당 처리속도는 " + time + "입니다.") cap.release() cv2.destroyAllWindows()
def shift(pic_path, save_path, min_shift_right, max_shift_right, min_shift_down, max_shift_down): file = os.listdir(pic_path) count = 0 # 判斷是否要寫標頭 for shift_right in range(min_shift_right, max_shift_right + 1, 1): for shift_down in range(min_shift_down, max_shift_down + 1, 1): outOfRangeCount = 0 with open(pic_path + "test_labels.csv", newline='' ) as csvfile: # newline=''參數,讓資料中包含的換行字元可以正確被解析以迴圈輸出每一列 rows = list(csv.reader(csvfile)) #tempCsvFilename = [] #tempCsvFilename = rows[1][0].split('-') csvFilename = save_path + "test_labels_shift.csv" for n in file: #平移圖片並儲存 extension = n.split('.') pic_name = extension[0] for i in range(1, len(extension) - 1): pic_name = pic_name + "." + extension[i] if (".jpg" in n) or (".png" in n) or (".JPG" in n) or (".jpeg" in n): image = cv2.imread(pic_path + n) image_height, image_width = image.shape[0:2] M = np.float32([[1, 0, shift_right], [0, 1, shift_down]]) dst = cv2.warpAffine(image, M, (image_width, image_height), borderValue=(255, 255, 255)) cv2.imwrite( save_path + pic_name + "_" + str(shift_right) + "_" + str(shift_down) + "_" + str('shift') + "." + str(extension[len(extension) - 1]), dst) #find data range end = 0 first = 0 duplicateFirst = 0 #make duplicateFirst exist for i in range(1, len(rows) - outOfRangeCount): # 尋找照片對應csv檔檔名 if n == str(rows[i][0]): #檢查檔名是否相同 first = i if (i + 1 != len(rows)) and ( (rows[i][0] == rows[i + 1][0]) or (rows[i][0] == rows[i - 1][0])): #檢查下一列 一樣為多物件 if (rows[i][0] != rows[i - 1][0]): # 一張照片第一個 end = i - 1 # y從i-1開始 duplicateFirst = i end = end + 1 else: end = first if (duplicateFirst > 0): first = duplicateFirst if (first == end == 0): continue outOfRange = False thisOutOfRangeCount = 0 for x in range(first, end + 1): xmin = int( rows[x - thisOutOfRangeCount][4]) + shift_right ymin = int( rows[x - thisOutOfRangeCount][5]) + shift_down xmax = int( rows[x - thisOutOfRangeCount][6]) + shift_right ymax = int( rows[x - thisOutOfRangeCount][7]) + shift_down rows[x - thisOutOfRangeCount][4] = xmin rows[x - thisOutOfRangeCount][5] = ymin rows[x - thisOutOfRangeCount][6] = xmax rows[x - thisOutOfRangeCount][7] = ymax outOfRange = False if ((xmin < 0) or (ymin < 0)): #檢查是否超出範圍 outOfRange = True elif ((xmax > image_width) or (ymax > image_height)): outOfRange = True if outOfRange == True: # 超出範圍 for i in range(x - thisOutOfRangeCount, len(rows) - 1): # 將outofrange row後面rows往前移 for index in range(len(rows[i])): rows[i][index] = rows[i + 1][index] outOfRangeCount = outOfRangeCount + 1 thisOutOfRangeCount = thisOutOfRangeCount + 1 continue # 0: filename 1:file_size 2: file_attributes 3: region_count 4: region_id 5: region_shape_attribute 6: region elif outOfRange == False: rows[x - thisOutOfRangeCount][0] = shiftFilename( rows[x - thisOutOfRangeCount][0], shift_right, shift_down) #cv2.rectangle(dst, (xmin,ymin), (xmax,ymax), (0,0,255), 2) # circle(img, center, radius, color, thickness=None, lineType=None, shift=None) cv2.imwrite( save_path + pic_name + "_after_" + str(shift_right) + "_" + str(shift_down) + "_shift." + str(extension[len(extension) - 1]), dst) addcount = 0 with open(csvFilename, "a", newline='') as wcsvfile: # csv標頭 writer = csv.writer(wcsvfile) for row in rows: if (count != 0) and (addcount == 0): # 不是第一次寫入 addcount = addcount + 1 continue if addcount != (len(rows) - outOfRangeCount): writer.writerow(row) addcount = addcount + 1 else: break count = count + 1
from cv2 import cv2 import numpy as np img = cv2.imread("./sources/helikopter.jpg",0) row,col = img.shape M = np.float32([[1,0,10],[0,1,70]]) dst = cv2.warpAffine(img,M,(row,col)) cv2.imshow("dst",dst) cv2.waitKey(0)
# cv.waitKey(0) (B, G, R) = img[400, 350] print('R={},G={},B={}'.format(R, G, B)) roi = img[290:380, 290:380] # cv.imshow('roi', roi) # cv.waitKey(0) resized = cv.resize(img, (300, 200))#后面的元组一二个元素分别代表w,h. # cv.imshow('fixed img', resized) # cv.waitKey(0) r = 300.0 / w dim = (300, int(h * r))#dimension resized = cv.resize(img, dim) # cv.imshow('resized', resized) # cv.waitKey(0) center = (w // 2, h // 2) M = cv.getRotationMatrix2D(center, -180, 1.0) rotated=cv.warpAffine(img,M,(w,h)) cv.imshow('Opencv Rotation', rotated) cv.waitKey(0) rotated = imutils.rotate_bound(img, -45) cv.imshow('Opencv Rotation', rotated) cv.waitKey(0) blurred = cv.GaussianBlur(img, (5, 5), 0) cv.imshow('blurred', blurred) cv.waitKey(0)
def rotateImage(image, angle): image_center = tuple(np.array(image.shape[1::-1]) / 2) rot_mat = cv2.getRotationMatrix2D(image_center, angle, 1.0) result = cv2.warpAffine(image, rot_mat, image.shape[1::-1], flags=cv2.INTER_LINEAR) return result
def shift(self, img, sx, sy): # preprocessing rows, cols = img.shape M = np.float32([[1, 0, sx], [0, 1, sy]]) shifted = cv2.warpAffine(img, M, (cols, rows)) return shifted
# to be 300px but compute the new height based on the aspect ratio r = 300.0 / w dim = (300, int(h * r)) resized = cv2.resize(image, dim) cv2.imshow("Aspect Ratio Resize", resized) cv2.waitKey(0) '''***************************************************************''' '''********************* Rotating an image ***********************''' '''***************************************************************''' # let's rotate an image 45 degrees clockwise using OpenCV by first # computing the image center, then constructing the rotation matrix, # and then finally applying the affine warp center = (w // 2, h // 2) M = cv2.getRotationMatrix2D(center, -45, 1.0) rotated = cv2.warpAffine(image, M, (w, h)) cv2.imshow("OpenCV Rotation", rotated) cv2.waitKey(0) '''***************************************************************''' '''********************* Smoothing an image **********************''' '''***************************************************************''' # in many image processing pipelines, we must blur an image to reduce high-frequency noise, # making it easier for our algorithms to detect and understand the actual contents of the image # rather than just noise that will “confuse” our algorithms. # Blurring an image is very easy in OpenCV and there are a number of ways to accomplish it. # apply a Gaussian blur with a 11x11 kernel to the image to smooth it, # useful when reducing high frequency noise blurred = cv2.GaussianBlur(image, (11, 11), 0) cv2.imshow("Blurred", blurred)
from cv2 import cv2 as cv import numpy as np img = cv.imread('datas/images/window_image.jpg') cv.imshow('Original Image',img) res = cv.resize(img,dsize=None,fx=2, fy=2, interpolation = cv.INTER_CUBIC) cv.imshow('Scaling Image',res) rows,cols,tmp = img.shape M = np.float32([[1,0,100],[0,1,50]]) dst = cv.warpAffine(img,M,(cols,rows)) cv.imshow('Shifting Image',dst) M2 = cv.getRotationMatrix2D((cols/2,rows/2),90,scale=1) dst2 = cv.warpAffine(img,M2,(cols,rows)) cv.imshow('Rotating Image',dst2) cv.waitKey(0) cv.destroyAllWindows()
def translate(image, x, y): M = np.float32([[1, 0, x], [0, 1, y]]) shifted = cv2.warpAffine(image, M, (image.shape[1], image.shape[0])) return shifted
k = cv.waitKey(0) if k == 27: cv.destroyAllWindows() img = cv.imread('image/zy.jpg') hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV) # 将bgr模式转换成hsv模式 # 获取红色阈值 lower_red = np.array([160, 100, 100]) upper_red = np.array([179, 255, 255]) # 获取掩模 mask = cv.inRange(hsv, lower_red, upper_red) # 与操作获得结果 res = cv.bitwise_and(img, img, mask=mask) # 转换成灰度图 gray_img = cv.cvtColor(res, cv.COLOR_BGR2GRAY) # 因res图像除了长方形外其余皆为黑色,所以阈值可以设成较低值 ret, thresh = cv.threshold(gray_img, 10, 255, cv.THRESH_BINARY) # 进行旋转操作 rows, cols = thresh.shape[:2] # 负号顺时针旋转,获得变换矩阵 M = cv.getRotationMatrix2D((cols / 2, rows / 2), -19.5, 1) dst = cv.warpAffine(thresh, M, (cols, rows)) cv_show('res', res) # 只留下长方形 cv_show('thresh', thresh) # 二值化后的图像 cv_show('dst', dst) # 旋转后图像
def DetectColorGrids(Color_checker_image): img_gray = cv.cvtColor(Color_checker_image,cv.COLOR_BGR2GRAY) img_denoise = cv.fastNlMeansDenoising(img_gray,10,7,21) img_threshold = cv.adaptiveThreshold(img_denoise,255,cv.ADAPTIVE_THRESH_MEAN_C,cv.THRESH_BINARY,21,3) kernel = np.ones((3,3), np.uint8) img_eroded = cv.erode(img_threshold,kernel) small_grid_size_range = GetSmallGridSizeRange(Color_checker_image) contours, _hierarchy = cv.findContours(img_eroded, cv.RETR_TREE, cv.CHAIN_APPROX_NONE) color_grid_contours = FilterColorGrid(contours, small_grid_size_range) centroid_positions = [] centroid_positions = FindContourCenter(color_grid_contours) centroids_no_overlap = [] centroids_no_overlap = FilterOutOverlapPoint(color_grid_contours,centroid_positions) #small to big, base on y axis centroids_no_overlap = sorted(centroids_no_overlap,key=lambda centroid: centroid[1]) rad_top = math.atan((centroids_no_overlap[1][1] - centroids_no_overlap[0][1]) / (centroids_no_overlap[1][0] - centroids_no_overlap[0][0])) angle_top = rad_top * 180 / PI centroids_no_overlap_size = len(centroids_no_overlap) rad_bottom = math.atan((centroids_no_overlap[centroids_no_overlap_size - 1][1] - centroids_no_overlap[centroids_no_overlap_size - 2][1]) / (centroids_no_overlap[centroids_no_overlap_size - 1][0] - centroids_no_overlap[centroids_no_overlap_size - 2][0])) angle_bottom = rad_bottom * 180 / PI angle_subtract = abs(angle_top - angle_bottom) if angle_subtract >= ANGLE_SUBTRACT_MAX_ENDURANCE: orientation_left_top = centroids_no_overlap[0] orientation_right_bottom=[0.0,0.0] for centroid in centroids_no_overlap: if centroid[0] < orientation_left_top[0]: orientation_left_top[0] = centroid[0] if centroid[1] < orientation_left_top[1]: orientation_left_top[1] = centroid[1] if centroid[0] > orientation_right_bottom[0]: orientation_right_bottom[0] = centroid[0] if centroid[1] > orientation_right_bottom[1]: orientation_right_bottom[1] = centroid[1] translation=[0.0,0.0] translation[0] = (orientation_right_bottom[0] - orientation_left_top[0]) / (TOTAL_COLUMNS - 1) translation[1] = (orientation_right_bottom[1] - orientation_left_top[1]) / (TOTAL_ROWS - 1) grids_position = np.zeros((TOTAL_ROWS, TOTAL_COLUMNS, 2), dtype=np.int) row_count = 0 col_count = 0 while(row_count < TOTAL_ROWS): col_count = 0 while(col_count < TOTAL_COLUMNS): grids_position[row_count][col_count][0] = orientation_left_top[0] + translation[0]*col_count grids_position[row_count][col_count][1] = orientation_left_top[1] + translation[1]*row_count col_count += 1 row_count += 1 return grids_position else: angle_avg = (angle_top + angle_bottom) / 2 img_rotation = np.zeros(img_gray.shape, dtype=np.uint8) rows, cols = img_gray.shape center = [cols / 2, rows / 2] rotation_mat = cv.getRotationMatrix2D((center[0],center[1]),angle_avg,1.0) img_rotation = cv.warpAffine(img_eroded,rotation_mat,(cols,rows)) contours, _hierarchy = cv.findContours(img_rotation, cv.RETR_TREE, cv.CHAIN_APPROX_NONE) color_grid_contours = FilterColorGrid(contours, small_grid_size_range) centroid_positions = [] centroid_positions = FindContourCenter(color_grid_contours) orientation_left_top = centroid_positions[0] orientation_right_bottom=[0.0,0.0] # contour_arr_all = np.zeros(img_gray.shape, dtype=np.uint8) i=0 for centroid in centroid_positions: if centroid[0] < orientation_left_top[0]: orientation_left_top[0] = centroid[0] if centroid[1] < orientation_left_top[1]: orientation_left_top[1] = centroid[1] if centroid[0] > orientation_right_bottom[0]: orientation_right_bottom[0] = centroid[0] if centroid[1] > orientation_right_bottom[1]: orientation_right_bottom[1] = centroid[1] i+=1 translation=[0.0,0.0] translation[0] = (orientation_right_bottom[0] - orientation_left_top[0]) / (TOTAL_COLUMNS - 1) translation[1] = (orientation_right_bottom[1] - orientation_left_top[1]) / (TOTAL_ROWS - 1) grid_coordinate = np.zeros((TOTAL_ROWS, TOTAL_COLUMNS, 2), dtype=np.int) row_count = 0 col_count = 0 while(row_count < TOTAL_ROWS): col_count = 0 while(col_count < TOTAL_COLUMNS): grid_coordinate[row_count][col_count][0] = orientation_left_top[0] + translation[0]*col_count grid_coordinate[row_count][col_count][1] = orientation_left_top[1] + translation[1]*row_count col_count += 1 row_count +=1 grids_position = np.zeros((TOTAL_ROWS, TOTAL_COLUMNS, 2), dtype=np.int) temp_coordinate = [0,0] row_count = 0 col_count = 0 while(row_count < TOTAL_ROWS): col_count = 0 while(col_count < TOTAL_COLUMNS): temp_coordinate = GetRotationPoint((grid_coordinate[row_count][col_count][0],grid_coordinate[row_count][col_count][1]), cols, rows, -1 * angle_avg) grids_position[row_count][col_count][0] = temp_coordinate[0] grids_position[row_count][col_count][1] = temp_coordinate[1] col_count += 1 row_count += 1 return grids_position
def shift(img, sx, sy): rows, cols = img.shape M = numpy.float32([[1,0,sx],[0,1,sy]]) # Translation (shift the object location) shifted = cv2.warpAffine(img, M, (cols,rows)) return shifted
cv2.circle(image_data_array_BGR, finger_gap3, 20, [0,0,255], -1) cv2.line(image_data_array_BGR,finger_gap0,finger_gap2,[0,255,0],2) cv2.imshow("finger detect", image_data_array_BGR) cv2.waitKey(100) # midpoint = (((point1[0]+point2[0])/2),((point1[1]+point2[1])/2)) slope = (point1[1]-point2[1])/(point1[0]-point2[0]) print(slope) angle = float(90) - (abs(numpy.arctan(slope))*180/numpy.pi) if (slope>0): angle = angle*-1 rot = cv2.getRotationMatrix2D(point1, angle, 1.0) image_data_array_rotated = cv2.warpAffine(image_data_array, rot, image_data_array.shape[1::-1], flags=cv2.INTER_LINEAR) erosion_rotated = cv2.warpAffine(erosion, rot, erosion.shape[1::-1], flags=cv2.INTER_LINEAR) cv2.imshow("rotated", image_data_array_rotated) cv2.waitKey(100) #---------------------------------------------------------------------------- # Median filtering and CLAHE #---------------------------------------------------------------------------- image_median = cv2.medianBlur(image_data_array_rotated, 5) clahe = cv2.createCLAHE(clipLimit=2,tileGridSize=(8,8)) image_clahe = clahe.apply(image_median) # cv2.imshow('image_clahe', image_clahe) # cv2.waitKey(100)
def rotate(image, x, y, angle): rot_matrix = cv2.getRotationMatrix2D((x, y), angle, 1.0) h, w = image.shape[:2] return cv2.warpAffine(image, rot_matrix, (w, h))
def Varredura(): #realiza a varredura e chama a função de analise de contornos img = Camera() #caso queira desabilitar a camera, é so comentar a linha de cima e usar as de baixo #img = cv2.imread("<endereço da imagem>") #img = cv2.resize(img, (639,479), interpolation = cv2.INTER_AREA) #chama a função de enquadramento img = Enquadramento(img) #a função da janela declarada é impedir q a imagem apareça gigantesca e ñ perca qualidade cv2.namedWindow('Deseja Recortar a imagem?', cv2.WINDOW_NORMAL) cv2.resizeWindow('Deseja Recortar a imagem?', (int(479 * proporcao), 479)) ##### rotacionar 180º para melhor compreenção altura, largura = img.shape[:2] ponto = (largura / 2, altura / 2 ) #ponto no centro da figura, ponto de giro rotacao = cv2.getRotationMatrix2D(ponto, 180, 1.0) img = cv2.warpAffine(img, rotacao, (largura, altura)) ###primeira janela pra corte print("\n\n\n") print("--Deseja recortar a imagem?--") print( "-Sim:\n Selecione clicando e arrastando.\n Confirma com a tecla Enter" ) print("-Não:\n Aperte a tecla Esc\n\n\n") xs, ys, w, h = cv2.selectROI( 'Deseja Recortar a imagem?', img) #permite o usuario cortar a imagem com o mouse cv2.destroyAllWindows() crop_img_true = crop_img_contour = img[ys:ys + h, xs:xs + w] #salvandoos dados do corte cortada = True if crop_img_true.shape[0] < 1: cortada = False crop_img_true = crop_img_contour = img print("\n\n\n") print( "--Utilize as barras para evidenciar, da melhor forma possível, os objetos desejados--" ) print( "--O filtro dinâmico possui limitações, logo, talvez não seja possível evidenciar todos os objetos desejados de um só vez--" ) #filtro dinamico desejo = "b" while 1: x, y, z, a, b, c = ( tr.tracker(crop_img_true) ) #biblioteca que gera filtro de cores de modo iterativo crop_img_true = cv2.cvtColor( crop_img_true, cv2.COLOR_BGR2HSV ) #é necessario converter para hsv para passar o filtro #criando imagem base pra varredura com o contorno do filtro de cores mask_inRange = cv2.inRange(crop_img_true, (x, y, z), (a, b, c)) #lógica para sobreposição de filtros, necessário caso o usuário queira evidencia cores distantes no espectro HSVe com intervalos if desejo == "a": mask_inRange = cv2.resize(mask_inRange, (int(479 * proporcao), 479), interpolation=cv2.INTER_AREA) mask_inRange_temp = cv2.resize(mask_inRange_temp, (int(479 * proporcao), 479), interpolation=cv2.INTER_AREA) for y in range(0, mask_inRange.shape[0]): #percorre linhas for x in range(0, mask_inRange.shape[1]): #percorre colunas #mask eh uma imagem composta apenas de 0 e 255 if (mask_inRange_temp[y][x] == (255)): mask_inRange[y][x] = (255) mask_inRange = cv2.resize(mask_inRange, (int(2500 * proporcao), 2500), interpolation=cv2.INTER_AREA) print("--Deseja avidenciar mais objetos?--") print(" a - Sim \n b - Não") desejo = input() if desejo == "a": mask_inRange_temp = mask_inRange crop_img_true = cv2.cvtColor(crop_img_true, cv2.COLOR_HSV2BGR) #caso o usuário envio qualquer coisa diferente de a, será considerado b else: break #caso o usuário opte por recoertar a imagem, é necessario corrigir as medidas if cortada == True: blank_space_black = np.zeros((img.shape[0], img.shape[1]), np.uint8) blank_space_black[:] = (0) blank_space_black[ys:ys + h, xs:xs + w] = mask_inRange mask_inRange = blank_space_black crop_img_true = crop_img_contour = img #etapa importante para que a função findContours funcione sem problemas _, threshold = cv2.threshold(mask_inRange, 250, 255, cv2.THRESH_BINARY) #varredura de contornos contours = [] contours, _ = cv2.findContours(threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) #listas de bordas contornos_reais = [] total_objetos = 0 # tirando objetos menores que o minimo... retira ruido for cnt in contours: if cv2.contourArea(cnt) >= maxi: contornos_reais.append(cnt) total_objetos += 1 print("\n\n") print("-- Total de objetos encontrados", total_objetos, " --") # todos os dados são obtidos atraves da mascara, uma imagem praticamente binária. #as edições (contornos, escrever o ID no centro do objeto) são feitas em cima da imagem crop_img_contour que é a mesma imagem justificada, enquadrada lá do inicio #desenhar contornos cv2.drawContours(crop_img_contour, contornos_reais, -1, (0, 255, 0), 5) crop_img_contour, tabela = Identificar_objeto( contornos_reais, crop_img_contour, img) #chamar a função Identificar_objetos img_final = cv2.resize(crop_img_contour, (850, 512), interpolation=cv2.INTER_AREA) cv2.imwrite("img_final.jpg", img_final) #salvando a imagem na pasta do programa ###tabela tabela_df = pd.DataFrame(data=tabela, columns=[ 'Id', 'Centro_X', 'Centro_Y', 'Ângulo', ]) tabela_df.to_csv("tabela.csv") return tabela_df, img_final
def ReadFolder(read_path, train_dir, test_dir, pos, vis=False, vis_dir=None): '''read position file. crop faces and save them.''' files = os.listdir(read_path) test_files = files[-len(files) // 8:] train_files = files[:-len(files) // 8] # build training set index = 0 faces = np.zeros((len(train_files) * 6, 224, 224, 3), dtype=int) for file in train_files: image = cv.imread(os.path.join(read_path, file)) # [h, w, c] # find faces try: locations = fr.api.face_locations(image, model='cnn') except Exception as e: print('\033[31m[FATAL] Locations in file {} cannot be extracted due to folloing reason:\n{}\033[37m'.format(os.path.join(read_path, file), e)) else: if not locations: print('\033[33m[WARN] Cannot find face in file {} \033[37m'.format(os.path.join(read_path, file))) continue flags = locations[max_area_indx(locations)] cropped = cv.resize(image[flags[0]:flags[2], flags[3]:flags[1], :], (224, 224)) h_flip = cv.flip(cropped, 1) # horizontal flip m = cv.getRotationMatrix2D((112, 112), 10, 1) rotate1 = cv.warpAffine(cropped, m, (224, 224)) m = cv.getRotationMatrix2D((112, 112), -10, 1) rotate2 = cv.warpAffine(cropped, m, (224, 224)) zoomin = cv.resize(cropped[25:-25, 25:-25], (224, 224)) noise = (np.random.randn(224, 224, 3) * 20 + cropped).clip(0, 255) if vis: cv.imwrite(os.path.join(vis_dir, 'train_' + str(index) + '.png'), cropped) faces[index] = cropped index += 1 faces[index] = h_flip index += 1 faces[index] = rotate1 index += 1 faces[index] = rotate2 index += 1 faces[index] = zoomin index += 1 faces[index] = noise index += 1 np.save(os.path.join(train_dir, pos), faces) print('[INFO] ' + os.path.join(train_dir, pos) + ' contains {} images'.format(faces.shape[0])) # build testing set index = 0 faces = np.zeros((len(test_files), 224, 224, 3), dtype=int) for file in test_files: image = cv.imread(os.path.join(read_path, file)) # [h, w, c] # find faces try: locations = fr.api.face_locations(image, model='cnn') except Exception as e: print('\033[31m[FATAL] Locations in file {} cannot be extracted due to folloing reason:\n{}\033[37m'.format(os.path.join(read_path, file), e)) else: if not locations: print('\033[33m[WARN] Cannot find face in file {} \033[37m'.format(os.path.join(read_path, file))) continue flags = locations[max_area_indx(locations)] cropped = cv.resize(image[flags[0]:flags[2], flags[3]:flags[1], :], (224, 224)) if vis: cv.imwrite(os.path.join(vis_dir, 'test_' + str(index) + '.png'), cropped) faces[index] = cropped index += 1 np.save(os.path.join(test_dir, pos), faces) print('[INFO] ' + os.path.join(test_dir, pos) + ' contains {} images'.format(faces.shape[0]))
def cloud_shadow_detection(self, solz, sola, vector=None, cloud_height=500.0, model_fn=None, pixel_size=10.0): if self.sensor == 'S2A': blue_fn = [item for item in self.rhot_fns if 'rhot_492' in item][0] green_fn = [item for item in self.rhot_fns if 'rhot_560' in item][0] red_fn = [item for item in self.rhot_fns if 'rhot_665' in item][0] nir_fn = [item for item in self.rhot_fns if 'rhot_833' in item][0] swir1_fn = [item for item in self.rhot_fns if 'rhot_1614' in item][0] swir2_fn = [item for item in self.rhot_fns if 'rhot_2202' in item][0] cirrus_fn = [ item for item in self.rhot_fns if 'rhot_1373' in item ][0] else: blue_fn = [item for item in self.rhot_fns if 'rhot_492' in item][0] green_fn = [item for item in self.rhot_fns if 'rhot_559' in item][0] red_fn = [item for item in self.rhot_fns if 'rhot_665' in item][0] nir_fn = [item for item in self.rhot_fns if 'rhot_833' in item][0] swir1_fn = [item for item in self.rhot_fns if 'rhot_1610' in item][0] swir2_fn = [item for item in self.rhot_fns if 'rhot_2186' in item][0] cirrus_fn = [ item for item in self.rhot_fns if 'rhot_1377' in item ][0] blue = utils.band_math([blue_fn], 'B1') green = utils.band_math([green_fn], 'B1') red = utils.band_math([red_fn], 'B1') ndsi = utils.band_math([green_fn, swir1_fn], '(B1-B2)/(B1+B2)') swir2 = utils.band_math([swir2_fn], 'B1') ndvi = utils.band_math([nir_fn, red_fn], '(B1-B2)/(B1+B2)') blue_swir = utils.band_math([blue_fn, swir1_fn], 'B1/B2') # step 1 cloud_prob1 = (swir2 > 0.03) * (ndsi < 0.8) * (ndvi < 0.5) * (red > 0.15) mean_vis = (blue + green + red) / 3 cloud_prob2 = (np.abs(blue - mean_vis) / mean_vis + np.abs(green - mean_vis) / mean_vis + np.abs(red - mean_vis) / mean_vis) < 0.7 cloud_prob3 = (blue - 0.5 * red) > 0.08 cloud_prob4 = utils.band_math([nir_fn, swir1_fn], 'B1/B2>0.75') cloud = cloud_prob1 * cloud_prob2 * cloud_prob3 * cloud_prob4 cloud = cloud.astype(np.uint8) cnt_cloud = len(cloud[cloud == 1]) cloud_level = cnt_cloud / np.shape(cloud)[0] / np.shape(cloud)[1] print('cloud level:%.3f' % cloud_level) cloud_large = np.copy(cloud) * 0 # -- only the cloud over water was saved -- # labels_struct[0]: count; # labels_struct[1]: label matrix; # labels_struct[2]: [minY,minX,block_width,block_height,cnt] labels_struct = cv2.connectedComponentsWithStats(cloud, connectivity=4) img_h, img_w = cloud.shape for i in range(1, labels_struct[0]): patch = labels_struct[2][i] if patch[4] > 2000: cloud_large[labels_struct[1] == i] = 1 # cloud shadow detection PI = 3.1415 shadow_dire = sola + 180.0 if shadow_dire > 360.0: shadow_dire -= 360.0 cloud_height = [100 + 100 * i for i in range(100)] shadow_mean = [] for item in cloud_height: shadow_dist = item * np.tan(solz / 180.0 * PI) / 10.0 w_offset = np.sin(shadow_dire / 180.0 * PI) * shadow_dist h_offset = np.cos(shadow_dire / 180.0 * PI) * shadow_dist * -1 affine_m = np.array([[1, 0, w_offset], [0, 1, h_offset]]) cloud_shadow = cv2.warpAffine(cloud_large, affine_m, (img_w, img_h)) cloud_shadow = (cloud_shadow == 1) * (cloud_large != 1) shadow_mean.append(np.mean(green[cloud_shadow])) cloud_hight_opt = cloud_height[shadow_mean.index(min(shadow_mean))] shadow_dist_metric = cloud_hight_opt * np.tan(solz / 180.0 * PI) if cloud_hight_opt > 200 and cloud_hight_opt < 10000 and shadow_dist_metric < 5000: print('cloud height: %dm' % cloud_hight_opt) shadow_dist = cloud_hight_opt * np.tan( solz / 180.0 * PI) / pixel_size w_offset = np.sin(shadow_dire / 180.0 * PI) * shadow_dist h_offset = np.cos(shadow_dire / 180.0 * PI) * shadow_dist * -1 affine_m = np.array([[1, 0, w_offset], [0, 1, h_offset]]) cloud_shadow = cv2.warpAffine(cloud_large, affine_m, (img_w, img_h)) cloud_shadow = (cloud_shadow == 1) * (cloud_large != 1) cloud1 = np.copy(cloud) cloud1[cloud_shadow] = 2 cloud_all = np.copy(cloud) cloud_all[cloud_shadow] = 1 self.cloud = (cloud_all == 1) dst_fn = os.path.join(self.res_dir, self.pre_name + 'cloud.tif') utils.raster2tif(cloud1, self.geo_trans, self.proj_ref, dst_fn, type='uint8') kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 2)) cloud_shadow = cv2.morphologyEx(cloud_shadow.astype(np.uint8), cv2.MORPH_OPEN, kernel) cloud_shadow_key = True else: cloud_shadow_key = False self.cloud = cloud == 1 cirrus = utils.band_math([cirrus_fn], 'B1') # step 1 cloud_prob1 = (red - 0.07) / (0.25 - 0.07) cloud_prob1[cloud_prob1 < 0] = 0 cloud_prob1[cloud_prob1 > 1] = 1 # step 2 cloud_prob2 = (ndsi + 0.1) / (0.2 + 0.1) cloud_prob2[cloud_prob2 < 0] = 0 cloud_prob2[cloud_prob2 > 1] = 1 cloud_prob = cloud_prob1 * cloud_prob2 # step 3: water cloud_prob[blue_swir > 2.5] = 0 cloud_prob = (cloud_prob * 100).astype(np.int) if cloud_shadow_key: self.water = (ndsi > -0.1) * (cloud_prob == 0) * ( cirrus < 0.012) * (cloud_shadow == 0) else: self.water = (ndsi > -0.1) * (cloud_prob == 0) * (cirrus < 0.012) # vector mask if vector is not None: self.vector_mask = utils.vector2mask(blue_fn, vector) self.water = self.water * (self.vector_mask == 0) else: self.vector_mask = self.water * 0
def rotate(image, degree): rows, cols = image.shape[:2] medium = cv2.getRotationMatrix2D((cols / 2, rows / 2), degree, 1) dst = cv2.warpAffine(image, medium, (cols, rows)) return dst
# coding: utf-8 import numpy as np from cv2 import cv2 import matplotlib.pyplot as plt img = cv2.imread(r'pictures\cat.jpg',0) rows,cols = img.shape M = cv2.getRotationMatrix2D((cols/2,rows/2),90,1) dst = cv2.warpAffine(img,M,(cols,rows)) # 脖子给你打歪来 cv2.imshow("Rotated",dst) cv2.waitKey(0) cv2.destroyAllWindows() # Third argument of the cv2.warpAffine() function is the size of the output image, which should be in the form of (width, height). Remember width = number of columns, and height = number of rows.
def align_images_with_opencv(path_to_desired_image: str, path_to_image_to_warp: str, output_path: str): # See https://www.learnopencv.com/image-alignment-ecc-in-opencv-c-python/ # image_sr_path = "C:/Users/Alex/Repositories/CVC-MUSCIMA/CVCMUSCIMA_SR/CvcMuscima-Distortions/ideal/w-50/image/p020.png" # image_wi_path = "C:/Users/Alex/Repositories/CVC-MUSCIMA/CVCMUSCIMA_WI/CVCMUSCIMA_WI/PNG_GT_Gray/w-50/p020.png" # Read the images to be aligned im1 = cv2.imread(path_to_desired_image) im2 = cv2.imread(path_to_image_to_warp) # Convert images to grayscale im1_gray = cv2.cvtColor(im1, cv2.COLOR_BGR2GRAY) im2_gray = cv2.cvtColor(im2, cv2.COLOR_BGR2GRAY) im1_gray = cv2.bitwise_not(im1_gray) # Find size of image1 sz = im1.shape # Define the motion model warp_mode = cv2.MOTION_AFFINE # Define 2x3 or 3x3 matrices and initialize the matrix to identity warp_matrix = np.eye(2, 3, dtype=np.float32) # Specify the number of iterations. number_of_iterations = 100 # Specify the threshold of the increment # in the correlation coefficient between two iterations termination_eps = 1e-7 # Define termination criteria criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, number_of_iterations, termination_eps) # Run the ECC algorithm. The results are stored in warp_matrix. (cc, warp_matrix) = cv2.findTransformECC(im1_gray, im2_gray, warp_matrix, warp_mode, criteria) # Warp BW-image bw_image_path = str.replace(path_to_image_to_warp, "PNG_GT_Gray", "PNG_GT_BW") bw_image = cv2.imread(bw_image_path) bw_image_gray = cv2.cvtColor(bw_image, cv2.COLOR_BGR2GRAY) bw_image_aligned = cv2.warpAffine(bw_image_gray, warp_matrix, (sz[1], sz[0]), flags=cv2.INTER_LINEAR + cv2.WARP_INVERSE_MAP, borderMode=cv2.BORDER_CONSTANT, borderValue=0) cv2.imwrite(bw_image_path, bw_image_aligned) # Warp NoStaff-image bw_no_staff_image_path = str.replace(path_to_image_to_warp, "PNG_GT_Gray", "PNG_GT_NoStaff") bw_no_staff_image = cv2.imread(bw_no_staff_image_path) bw_no_staff_image_gray = cv2.cvtColor(bw_no_staff_image, cv2.COLOR_BGR2GRAY) bw_no_staff_image_aligned = cv2.warpAffine(bw_no_staff_image_gray, warp_matrix, (sz[1], sz[0]), flags=cv2.INTER_LINEAR + cv2.WARP_INVERSE_MAP, borderMode=cv2.BORDER_CONSTANT, borderValue=0) cv2.imwrite(bw_no_staff_image_path, bw_no_staff_image_aligned) # Warp Gray-Image last, in case user interrupts, to make sure the process continues appropriately and doesn't # miss the bw and bw_no_staff images, because the process compares only on grayscale images im2_aligned = cv2.warpAffine(im2_gray, warp_matrix, (sz[1], sz[0]), flags=cv2.INTER_LINEAR + cv2.WARP_INVERSE_MAP, borderMode=cv2.BORDER_CONSTANT, borderValue=255) cv2.imwrite(output_path, im2_aligned)
def alignment(self): base_median = np.median(self.gray_base) base_bit = np.where(self.gray_base > base_median, 255, 0).astype(np.uint8) base_pyramid = [base_bit[::2**i, ::2**i] for i in range(6)] mask = np.logical_or(self.gray_base > base_median + 10, self.gray_base < base_median - 10) mask_pyramid = [mask[::2**i, ::2**i] for i in range(6)] length = 0 for i in range(len(base_pyramid)): length += base_pyramid[i].shape[1] x = np.ones((base_pyramid[0].shape[0], length)) * 255 y = np.ones((base_pyramid[0].shape[0], length)) * 255 temp = 0 for i in range(len(base_pyramid)): x[-base_pyramid[i].shape[0]:, temp:temp + base_pyramid[i].shape[1]] = base_pyramid[i] y[-mask_pyramid[i].shape[0]:, temp:temp + mask_pyramid[i].shape[1]] = mask_pyramid[i] * 255 temp += base_pyramid[i].shape[1] plt.imshow(x, cmap='gray') plt.show() plt.imshow(y, cmap='gray') plt.show() crop = [] for i in range(6): crop.append((base_pyramid[i].shape[1] // 10, base_pyramid[i].shape[0] // 10)) compare_pyramid = [] for i in range(self.P): compare_median = np.median(self.gray_picture[i]) compare_bit = np.where(self.gray_picture[i] > compare_median, 255, 0).astype(np.uint8) compare_pyramid.append( [compare_bit[::2**j, ::2**j] for j in range(10)]) for i in range(self.P): shift_x = 0 shift_y = 0 for j in range(5, -1, -1): M = np.float32([[1, 0, shift_x], [0, 1, shift_y]]) compare = cv2.warpAffine( compare_pyramid[i][j], M, compare_pyramid[i][j].shape[0:2][::-1]) base_crop = base_pyramid[j][crop[j][0]:-crop[j][0], crop[j][1]:-crop[j][1]] mask_crop = mask_pyramid[j][crop[j][0]:-crop[j][0], crop[j][1]:-crop[j][1]] xor_min = np.inf for r in range(-1, 2, 1): #上下 for c in range(-1, 2, 1): #左右 T = np.float32([[1, 0, c], [0, 1, r]]) compare_shift = cv2.warpAffine( compare, T, compare.shape[0:2][::-1])[crop[j][0]:-crop[j][0], crop[j][1]:-crop[j][1]] xor_loss = np.logical_xor( base_crop, compare_shift)[mask_crop].sum() if xor_loss < xor_min: xor_min = xor_loss temp_x, temp_y = c, r shift_x += temp_x shift_y += temp_y shift_y *= 2 shift_x *= 2 print(shift_x / 2, shift_y / 2) F = np.float32([[1, 0, shift_x / 2], [0, 1, shift_y / 2]]) write = cv2.warpAffine(self.rgb_picture[i], F, self.rgb_picture[i].shape[0:2][::-1]) cv2.imwrite('.\\alignment_result\\' + str(i) + '.jpg', write[:, :, ::-1]) # rgb -> bgr
def translate(image, x, y): #[1,0,tx],[0,10,ty] : -tx shift left, -ty shift up M = np.float32([[1, 0, x], [0, 1, y]]) shifted = cv2.warpAffine(image, M, (image.shape[1], image.shape[0])) return shifted
slope = (point1[1] - point2[1]) / ( point1[0] - point2[0] ) # determine slope of line between reference points angle = float(90) - (abs(numpy.arctan(slope)) * 180 / numpy.pi ) # determine angle of hand if ( slope > 0 ): # determine direction of desired rotation based on slope polarity angle = angle * -1 rot = cv2.getRotationMatrix2D(point1, angle, 1.0) # generate affline rotation matrix image_data_array = cv2.warpAffine( image_data_array, rot, image_data_array.shape[1::-1], flags=cv2.INTER_LINEAR) # rotate image mask = cv2.warpAffine(mask, rot, mask.shape[1::-1], flags=cv2.INTER_LINEAR) # rotate mask #---------------------------------------------------------------------------- # Median filtering and CLAHE #---------------------------------------------------------------------------- image_data_array = cv2.medianBlur(image_data_array, 5) # apply median blur clahe = cv2.createCLAHE(clipLimit=2, tileGridSize=(8, 8)) # create CLAHE filter
def rotate(image, a, s): #s is the sizing , left as 1 (h, w) = image.shape[:2] center = (w // 2, h // 2) M = cv2.getRotationMatrix2D(center, a, s) rotated = cv2.warpAffine(image, M, (w, h)) return rotated