def harris_corner_detection(img, patch_size, threshold_eigen, nms_dist): assert len(img.shape) == 2, "Image must be grayscale." dx_filter = fil.sobel_filter('hor') dx = convolve2d(img, dx_filter) dx = np.abs(dx) dx = utils.scale(dx[1:-1, 1:-1]) dy_filter = fil.sobel_filter('ver') dy = convolve2d(img, dy_filter) dy = np.abs(dy) dy = utils.scale(dy[1:-1, 1:-1]) H, W = img.shape patch_height, patch_width = patch_size feature_score = [[0 for _ in range(W // 10)] for _ in range(H // 10)] features = [[0 for _ in range(W // 10)] for _ in range(H // 10)] for i in range(patch_height - 1, H): print(i) for j in range(W - patch_width + 1): harris_matrix = np.zeros((2, 2)) harris_matrix[0, 0] = np.sum(dx[i - patch_height + 1:i, j:j + patch_width - 1]**2) harris_matrix[0, 1] = np.sum( dx[i - patch_height + 1:i, j:j + patch_width - 1] * dy[i - patch_height + 1:i, j:j + patch_width - 1]) harris_matrix[1, 0] = np.sum( dx[i - patch_height + 1:i, j:j + patch_width - 1] * dy[i - patch_height + 1:i, j:j + patch_width - 1]) harris_matrix[1, 1] = np.sum(dy[i - patch_height + 1:i, j:j + patch_width - 1]**2) eigenvalues = np.linalg.eigvals(harris_matrix) min_eig = np.min(eigenvalues) if min_eig > threshold_eigen: if min_eig > feature_score[i // 10][j // 10]: print("i // 10: ", i // 10) print("j // 10: ", j // 10) feature_score[i // 10][j // 10] = min_eig features[i // 10][j // 10] = (i, j) final_features = [] for i in range(H // 10): for j in range(W // 10): if feature_score[i][j] > 0: final_features.append(features[i][j]) return final_features
def convert(num): counter = 0 print 'Thread: ', num for folder in folders: if ".DS_Store" not in folder: if os.path.exists(outputDir + folder) == False: os.mkdir(outputDir + folder) # continue # else: files = os.listdir(inputDir + folder) print len(files) for file in files: if ".DS_Store" not in file: # print 'Thread: ', num, inputDir + folder + "/" +file if os.path.exists(outputDir + folder + "/" + file) == False: img = cv2.imread(inputDir + folder + "/" + file, 1) if img != None: # image = cv2.resize(img, (224, 224)) img = ut.resize(img) newImg, x, y = ut.scale(img, [], [], imSize=28) cv2.imwrite(outputDir + folder + "/" + file, newImg) # print 'Thread: ', num, "write to : ", outputDir + folder + "/" +file else: pass # print "skip: " + outputDir + folder + "/" +file counter += 1 print counter
def renderBoard(self, board): board_size = board.getSize() print self.special_coordinates if not self.special_coordinates: self.generateSpecialCoordinates(board_size-1) self.constructBoardImage(board_size, self.getImageSize()) for stone in board.getStones(): self.draw(self.getImageFromColor(stone.getColor()), scale(self.image_size, stone.getPosition()))
def generate_with_com(self): """ yield one line every time format: [row_i, col_j] type(row_i) = int type(col_j) = set :return: None """ # all_e = 0 if not self.has_community: return com_cnt = self.com_amount out_max_d = self.rel['out']['max-d'] # in_max_d = self.rel['in']['max-d'] param_c = self.noise_param_c over_lap = self.overlap out_threshold = round(out_max_d * self.noise_threshold) row_axis = get_community_size(com_cnt, self.node1) col_axis = get_community_size(com_cnt, self.node2) const_a = math.exp(-1 / param_c) const_b = const_a - math.exp(-out_max_d / param_c) start_i, start_j = 0, 0 ul_col, ul_row, lr_col, lr_row = 0, 0, 0, 0 for i in range(com_cnt): out_pl = get_distribution(self.rel['out'], row_axis[i], -1) in_pl = get_distribution(self.rel['in'], col_axis[i], -1) block_is_even = col_axis[i] % 2 == 1 # upper left if over_lap > 0 and i > 0: ul_col = int(col_axis[i - 1] * over_lap) ul_row = int(row_axis[i - 1] * over_lap) ul_in_pl = get_distribution(self.rel['in'], ul_col, -1) ul_out_pl = get_distribution(self.rel['out'], ul_row, -1) ul_is_even = ul_col % 2 == 1 # lower right if over_lap > 0 and i < com_cnt - 1: lr_col = int(col_axis[i + 1] * over_lap) lr_row = int(row_axis[i + 1] * over_lap) lr_in_pl = get_distribution(self.rel['in'], lr_col, -1) lr_out_pl = get_distribution(self.rel['out'], lr_row, -1) lr_is_even = lr_col % 2 == 1 for row in range(row_axis[i]): a_line_set = set() d_out = out_pl.get_d() a_i = start_i + row for _ in range(d_out): j = in_pl.get_j() j = transform(block_is_even, j, col_axis[i] - 1) a_j = start_j + j a_line_set.add(a_j) # when d_out > threshold, add noise if d_out > out_threshold: y = random.random() d_extra = int(-param_c * math.log(const_a - y * const_b)) for _ in range(d_extra): j = in_pl.get_j() j = transform(block_is_even, j, col_axis[i] - 1) j = scale(j, 0, col_axis[i] - 1, 0, self.node2 - col_axis[i] - 1) if j > start_j: j += col_axis[i] a_line_set.add(j) # add overlap community in upper left if over_lap > 0 and i > 0 and row < ul_row: d_over_ul = ul_out_pl.get_d() for _ in range(d_over_ul): j = ul_in_pl.get_j() j = transform(ul_is_even, j, ul_col - 1) a_j = start_j - j a_line_set.add(a_j) # add overlap community in lower right if over_lap > 0 and i < com_cnt - 1 and row > (row_axis[i] - lr_row): d_over_lr = lr_out_pl.get_d() for _ in range(d_over_lr): j = lr_in_pl.get_j() j = transform(lr_is_even, j, lr_col - 1) a_j = start_j + col_axis[i] + j a_line_set.add(a_j) # all_e += len(a_line_set) yield [a_i, a_line_set] start_i += row_axis[i] start_j += col_axis[i]
def constructBoardImage(self, size, image_size): for x in xrange(size): for y in xrange(size): coordinate = (x, y) self.draw(self.getImageFromCoordinate(coordinate, (size-1)), scale(image_size, coordinate))
import cv2 import utility as ut # generateFunc = ["original", "scale", "rotate", "translate", "scaleAndTranslate", "brightnessAndContrast"] img = cv2.imread("testImg/testImg.jpg", 1) print img.shape w, h, _ = img.shape newImg, x, y = ut.scale(img, [], []) print newImg.shape newImg = cv2.resize(newImg, (h, w)) print newImg.shape # newImg, x, y = ut.mirror(img, [], []) # newImg, x, y = ut.contrastBrightess(img, [], []) # newImg, x, y = ut.rotate(img, [], []) # newImg, x, y = ut.translate(img, [], []) # newImg, x, y = ut.resize(img, [], []) cv2.imshow("img", img) cv2.imshow("newImg", newImg) cv2.waitKey(0)
class GameState(object): def __init__(self, player_turn): self.player_color = player_turn def nextTurn(self): # assumes colors are 0 and 1 self.player_color = int(not self.player_color) def getTurn(self): return self.player_color pygame.init() running = True board_size = 19 game_state = GameState(Colors.black) board = Board(board_size) window_size = scale(IMAGE_SIZE, (board_size,)*2) screen = pygame.display.set_mode(window_size) renderer = Renderer(screen, IMAGE_SIZE) renderer.renderBoard(board) def debug(board): print board.ko print print len(board.groups) print for group in board.groups: print group.stones print print group.liberties print
def augmentation(imgPath, name): # generateFunc = ["original", "mirror", "rotate", "translate", "brightnessAndContrast"] # generateFunc = ["mirror", "rotate", "translate", "brightnessAndContrast", "blur"] generateFunc = ["mirror", "brightnessAndContrast", "blur"] if debug: print "imgPath + name: ", imgPath + name img = cv2.imread(imgPath + name) x, y = [], [] img = ut.resize(img) newImg, x, y = ut.scale(img, [], [], imSize=224) print "name: ", name print "type(img): ", type(img) print "img.shape: ", img.shape if img != None: if debug: print "FIND image: ", imgPath + name print "img.shape: ", img.shape derivateNum = len(generateFunc) for index in range(derivateNum): (w, h, _) = img.shape # method = random.choice(generateFunc) method = generateFunc[index] # if index == 0: # method = "original" # if method == "resize": # newImg, newX, newY = ut.resize(img, x, y, xMaxBound = w, yMaxBound = h, random = True) if method == "rotate": newImg, newX, newY = ut.rotate(img, x, y, w=w, h=h) elif method == "mirror": newImg, newX, newY = ut.mirror(img, x, y, w=w, h=h) elif method == "translate": newImg, newX, newY = ut.translate(img, x, y, w=w, h=h) elif method == "brightnessAndContrast": newImg, newX, newY = ut.contrastBrightess(img, x, y) elif method == "original": newImg, newX, newY = img, x, y elif method == "blur": newImg, newX, newY = blur = cv2.blur(img, (5, 5)), x, y # newImg, newX, newY = ut.scale(img, x, y, imSize = imSize) # elif method == "scale": # newImg, newX, newY = ut.scale(img, x, y) else: raise "not existing function" if debug: print "name: ", name print "index: ", index print "newImg.shape: ", newImg.shape print "method: ", method cv2.imshow("img", img) cv2.imshow("newImg", newImg) cv2.waitKey(0) cv2.imwrite( path + name.replace(".jpg", "") + "_" + str(index) + ".jpg", newImg) print "saving to ............" print path + name.replace(".jpg", "") + "_" + str(index) + ".jpg"