Hướng dẫn nearest neighbor interpolation python code - mã python nội suy hàng xóm gần nhất

Tệp này chứa văn bản unicode hai chiều có thể được giải thích hoặc biên dịch khác với những gì xuất hiện dưới đây. Để xem xét, hãy mở tệp trong một trình soạn thảo cho thấy các ký tự Unicode ẩn. Tìm hiểu thêm về các ký tự unicode hai chiều

FromCollectionSimportCount collections import Counter
defnn_interpolate [a, new_size]: nn_interpolate[A, new_size]:
"" "
Nội suy hàng xóm gần nhất, từng bước
"" "
# Nhận kích thước
old_size=A.shape = A.shape
# Tính tỷ lệ hàng và cột
Row_Ratio, col_ratio = new_size [0]/old_size [0], new_size [1]/old_size [1], col_ratio = new_size[0]/old_size[0], new_size[1]/old_size[1]
# Xác định vị trí hàng pixel mới i
new_row_positions=np.array[range[new_size[0]]]+1 = np.array[range[new_size[0]]]+1
new_col_positions=np.array[range[new_size[1]]]+1 = np.array[range[new_size[1]]]+1
# bình thường hóa vị trí hàng mới và col theo tỷ lệ
new_row_positions=new_row_positions/row_ratio = new_row_positions / row_ratio
new_col_positions=new_col_positions/col_ratio = new_col_positions / col_ratio
# Áp dụng trần cho các vị trí hàng mới và col bình thường hóa
new_row_poseitions = np.ceil [new_row_poseitions] = np.ceil[new_row_positions]
new_col_poseitions = np.ceil [new_col_poseitions] = np.ceil[new_col_positions]
# Tìm bao nhiêu lần để lặp lại mỗi phần tử
ROW_REPEATS = NP.Array [Danh sách [Bộ đếm [New_Row_Poseitions] .Values ​​[]]]] = np.array[list[Counter[new_row_positions].values[]]]
col_repeats = np.array [danh sách [bộ đếm [new_col_poseitions] .values ​​[]]]] = np.array[list[Counter[new_col_positions].values[]]]
# Thực hiện phép nội suy theo cột trên các cột của ma trận
row_matrix = np.dstack [[np.repeat [a [:, i], row_repeats] = np.dstack[[np.repeat[A[:, i], row_repeats]
foriinrange [old_size [1]]]] [0] i in range[old_size[1]]]][0]
# Thực hiện phép nội suy theo cột trên các cột của ma trận
row_matrix = np.dstack [[np.repeat [a [:, i], row_repeats], ncol = row_matrix.shape
foriinrange [old_size [1]]]] [0] = np.stack[[np.repeat[row_matrix[i, :], col_repeats]
nrow, ncol = row_matrix.shape i in range[nrow]]]
returnfinal_matrix final_matrix
defnn_interpolate [a, new_size]: nn_interpolate[A, new_size]:
Final_matrix = np.stack [[np.repeat [row_matrix [i,:], col_repeats]
old_size=A.shape = A.shape
foriinrange [nrow]]]], col_ratio = np.array[new_size]/np.array[old_size]
"" "Vectorized Nội suy hàng xóm gần nhất" ""
Row_Ratio, col_ratio = np.array [new_size] /np.array [old_size] = [np.ceil[range[1, 1 + int[old_size[0]*row_ratio]]/row_ratio] - 1].astype[int]
# Nội suy hàng khôn ngoan
Row_idx = [np.ceil [phạm vi [1, 1+int [old_size [0]*row_ratio]]/row_ratio] -1] .Arype [int] = [np.ceil[range[1, 1 + int[old_size[1]*col_ratio]]/col_ratio] - 1].astype[int]
# Cột nội suy khôn ngoan = A[:, row_idx][col_idx, :]
returnfinal_matrix final_matrix

Bài Viết Liên Quan

Chủ Đề